Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/account/login', (req, res, next) => {
- if(!req.body['username'] || !req.body['password']) {
- return res.status(400).json({message:'Please fill out all fields'});
- }
- Account.findOne({username: req.body['username']}, (err, account) => {
- if(account != null) {
- bcrypt.compare(req.body['password'], account.password, function(err, isCorrect) {
- // res == true
- if(isCorrect) {
- account.save();
- res.status(200).send(account)
- }else {
- res.status(404).send("The account doesn't exists")
- }
- });
- }else {
- res.send("The account doesn't exists");
- }
- })
- })
- router.post('/account/new', (req, res, next) => {
- var duplicate = checkForDuplicates(req.body)
- if (!duplicate) {
- var encryptedPassword;
- var clientIP;
- var clientRegisterDate;
- bcrypt.hash(req.body['password'], saltRounds, function(err, hash) {
- if(err) {
- res.status(400).send(err)
- }
- encryptedPassword = hash;
- Account.findOne({username: req.body['username']}, (err, account) => {
- if(account != null) {
- res.send("The account already exists")
- }else {
- Account.create({username: req.body['username'], email: req.body['email'], password: encryptedPassword}, (err, createdAccount) => {
- if(err) {
- res.status(400).send(err)
- return
- }
- var informations = {
- username: createdAccount.username,
- id: createdAccount.id
- }
- res.status(201).send(informations)
- })
- }
- })
- })
- } else {
- res.status(400).send()
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement