Created
February 19, 2019 07:16
-
-
Save tarusharora/0fa2def9ebe7573f151d82036c9462f1 to your computer and use it in GitHub Desktop.
auth controller for sign-up and sign-in
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
const nconf = require('nconf'); | |
const User = require('../models/User'); | |
const { SignUpResponse } = require('../models/Auth'); | |
const { INVALID_PASSWORD, USER_DOESNT_EXISTS, USER_EXISTS } = require('../models/Errors'); | |
const postSignup = async (req, res) => { | |
const { email, password } = req.body; | |
try { | |
const existingUser = await User.findOne({ email: req.body.email }); | |
if (existingUser) { | |
res.code(409); | |
res.send(new Error(USER_EXISTS)); | |
return; | |
} | |
const user = new User({ | |
email, | |
password, | |
}); | |
const newUser = await user.save(); | |
const { id, email: userEmail } = newUser; | |
const token = await res.jwtSign({ id }, { expiresIn: nconf.get('app.userJwtExpiry') }); | |
res.send(new SignUpResponse({ email: userEmail, token, id })); | |
} catch (error) { | |
res.send(error); | |
} | |
}; | |
const postLogin = async (req, res) => { | |
const { email, password } = req.body; | |
try { | |
const user = await User.findOne({ email: email.toLowerCase() }).exec(); | |
if (!user) { | |
res.send(new Error(USER_DOESNT_EXISTS)); | |
} | |
const isMatch = await user.comparePassword(password); | |
if (isMatch) { | |
const { id } = user; | |
const token = await res.jwtSign({ id }, { expiresIn: nconf.get('app.userJwtExpiry') }); | |
return res.send(new SignUpResponse({ email, token, id })); | |
} | |
return res.send(new Error(INVALID_PASSWORD)); | |
} catch (err) { | |
throw err; | |
} | |
}; | |
module.exports = { | |
postLogin, | |
postSignup, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment