Created
April 17, 2019 12:21
-
-
Save bayraktugrul/3a39a9de4f2fd35424f4d6dc9c9f54de to your computer and use it in GitHub Desktop.
express login auth jwt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
login(req, res) { | |
if (!req.body.memail || !req.body.mpassword) { | |
return res.status(404).send({ | |
message: 'Email and password can not be empty!', | |
}); | |
} else { | |
const email = req.body.memail; | |
const password = crypto.createHash('md5').update(req.body.mpassword).digest("hex"); | |
const potentialUser = { | |
where: { | |
memail: email, | |
mpassword: password | |
}, | |
attributes: ['memail', 'muuid', 'cid'] | |
}; | |
Member.findOne(potentialUser) | |
.then(user => { | |
if(!user) { | |
return res.status(404).send({ | |
message: 'fail', | |
error: 'User not found. Authentication failed.' | |
}); | |
} | |
const token = jwt.sign({ | |
muuid: user.muuid, | |
memail: user.memail, | |
cid: user.cid | |
}, | |
'secret_key', | |
{ | |
expiresIn :"2h" | |
} | |
) | |
return res.status(200).send({ message: 'success', token: token}); | |
}) | |
.catch((error) => res.status(400).send(error)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment