//A recovery hash means this is an external login. Try seeing if it matches.
db.query('select * from users where recovery_hash=$1 limit 1',[req.body.recoveryhash])
.then((data)=>{
if(data.rows.length>0){
db.query('update users set password_hash=$2 where id=$1',[data.rows[0].id,req.body.password])
res.status(200).json({verified:true})
db.query('select * from users where email=$1',[req.body.email])
.then((data)=>{
if(data.rows.length==0){
returndb.query('select * from users where recovery_hash=$1 limit 1',[req.body.recoveryhash])
.then((data)=>{
if(data.rows.length>0){
db.query('update users set username=$3,password_hash=$2 where id=$1',[data.rows[0].id,req.body.password,req.body.username])
db2.query('update users set username=$3,password_hash=$2 where id=$1',[data.rows[0].id,req.body.password,req.body.username])
res.status(200).json({verified:true})
}else{
res.status(200).json({verified:true})
//This doesn't exist. At this time we will register them since this is external.
db.query('insert into users(username,email,password_hash,created_on,roles_id,avatar,recovery_hash) values($1,$2,$3,$4,(select id from roles where name=\'Guest\'),$5,$6) returning id',[req.body.username,req.body.email,req.body.password,newDate(),req.body.avatar,req.body.userID])
.then((data)=>{
if(data.rows.length>0){
db2.query('insert into users(username,email,password_hash,created_on,roles_id,avatar,recovery_hash,id) values($1,$2,$3,$4,(select id from roles where name=\'Guest\'),$5,$6,$7) returning id',[req.body.username,req.body.email,req.body.password,newDate(),req.body.avatar,req.body.userID,data.rows[0].id])
}
})
.catch((err)=>{
console.log(err.message)
})
}
})
.catch((err)=>{
console.log(err.message)
res.status(500).send(err.message)
})
}else{
console.log("User with email '"+req.body.email+"' already exists assume it's not a google account. Overwriting...")
//console.log(req.body.password)
db.query('update users set password_hash=$1,avatar=$2,recovery_hash=$3,username=$5 where id=$4 returning id',[req.body.password,req.body.avatar,req.body.userID,data.rows[0].id,req.body.username])
.then((data)=>{
if(data.rows.length>0){
db2.query('update users set password_hash=$1,avatar=$2,recovery_hash=$3,username=$5 where id=$4 returning id',[req.body.password,req.body.avatar,req.body.userID,data.rows[0].id,req.body.username])
}
})
res.status(200).json({verified:true})
//This doesn't exist. At this time we will register them since this is external.
db.query('insert into users(username,email,password_hash,created_on,roles_id,avatar,recovery_hash) values($1,$2,$3,$4,(select id from roles where name=\'Guest\'),$5,$6)',[req.body.username,req.body.email,req.body.password,newDate(),req.body.avatar,req.body.userID])
returndb.query('update builds set creator=$1,build_name=$2,class1=(SELECT id from class WHERE name=$3 limit 1),class2=(SELECT id from class WHERE name=$4 limit 1),last_modified=$5,data=$6 where id=$7 returning id',[req.body.creator,req.body.build_name,req.body.class1,req.body.class2,newDate(),req.body.data,req.body.id])
.then((data)=>{
if(send){
res.status(200).send(data.rows[0])
res.status(200).json(data.rows[0])
}
if(db2){
db2.query('update builds set creator=$1,build_name=$2,class1=(SELECT id from class WHERE name=$3 limit 1),class2=(SELECT id from class WHERE name=$4 limit 1),last_modified=$5,data=$6 where id=$7 returning id',[req.body.creator,req.body.build_name,req.body.class1,req.body.class2,newDate(),req.body.data,req.body.id])
}
})
.catch((err)=>{
@ -898,7 +973,10 @@ function submitBuild(req,res,db,send) {
returndb.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,newDate(),newDate(),0,req.body.data,0])
.then((data)=>{
if(send){
res.status(200).send(data.rows[0])
res.status(200).json(data.rows[0])
}
if(db2){
db2.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice,id) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10,$11) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,newDate(),newDate(),0,req.body.data,0,data.rows[0].id])
}
})
.catch((err)=>{
@ -919,7 +997,10 @@ function submitBuild(req,res,db,send) {
db.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,newDate(),newDate(),0,req.body.data,0])
.then((data)=>{
if(send){
res.status(200).send(data.rows[0])
res.status(200).json(data.rows[0])
}
if(db2){
db2.query('insert into builds(users_id,creator,build_name,class1,class2,created_on,last_modified,likes,data,editors_choice,id) values((SELECT id from users WHERE username=$1 limit 1),$2,$3,(SELECT id from class WHERE name=$4 limit 1),(SELECT id from class WHERE name=$5 limit 1),$6,$7,$8,$9,$10,$11) returning id',[req.body.username,req.body.creator,req.body.build_name,req.body.class1,req.body.class2,newDate(),newDate(),0,req.body.data,0,data.rows[0].id])
}
})
.catch((err)=>{
@ -933,12 +1014,30 @@ function submitBuild(req,res,db,send) {
app.post(PREFIX+"/submitBuild",(req,res)=>{
submitBuild(req,res,db,true)
submitBuild(req,res,db2,false)
})
app.post(PREFIX+"/test/submitBuild",(req,res)=>{
submitBuild(req,res,db,true)
submitBuild(req,res,db2,false)
})
app.get(PREFIX+"/getBuild",(req,res)=>{
db.query('select * from builds where id=$1 limit 1',[req.query.id])
.then((data)=>{
res.status(200).json(data.rows[0])
})
.catch((err)=>{
res.status(500).send(err.message)
})
})
app.get(PREFIX+"/test/getBuild",(req,res)=>{
db2.query('select * from builds where id=$1 limit 1',[req.query.id])