login.js
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const findById=require('./findById')
const bcrypt=require('bcrypt-nodejs')
exports.Login=(req,res)=>{
const userId=req.body.userId
const password=req.body.password
const DataCheck=()=>{
return new Promise((resolve,reject)=>{
if(!userId || !password){
return reject({
code: 'request_body_error',
message: 'request body is not defined'
})
}
else resolve()
})
}
const IdCheck=()=>{
let user={}
const findUser= async ()=>{
try{
user =await findById.findById(userId)
return user
}
catch (err) {
return Promise.reject(err)
}
}
return findUser()
}
const PwCheck=(user)=>{
if (user[0]==null){
return Promise.reject({
code:'id_wrong',
message:'id wrong'
})
}
if(bcrypt.compareSync(password,user[0].password)){
req.session.sid=userId
req.session.save(()=>{
res.status(200).json({userId:userId})
})
}
else{
return Promise.reject({
code:'pw_wrong',
message:'pw wrong'
})
}
}
DataCheck()
.then(IdCheck)
.then(PwCheck)
.catch((err)=>{
console.log(err)
res.status(500).json(err.message|err)
})
}