-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(signin): implement in-app signin
[Finishes #167727454, Delivers #167727454]
- Loading branch information
Showing
21 changed files
with
627 additions
and
179 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import bcrypt from 'bcrypt'; | ||
|
||
import models from '../db/models'; | ||
import sender from '../services/email'; | ||
import Response from '../utils/response.utils'; | ||
import UserUtils from '../utils/user.utils'; | ||
import JWTService from '../services/jwt.service'; | ||
|
||
const { User } = models; | ||
|
||
/** | ||
* This class creates the user controller | ||
*/ | ||
export default class UserController { | ||
/** | ||
* @param {object} req The user's signup details | ||
* @param {object} res The user's details returned after signup | ||
* @returns {object} A signed up user | ||
*/ | ||
static async signup(req, res) { | ||
try { | ||
const hash = await bcrypt.hash(req.body.password, 10); | ||
const userData = UserUtils.getUserSignupData(req.body); | ||
const user = await User.create({ | ||
...userData, | ||
password: hash | ||
}, { returning: true }); | ||
|
||
// parameter(s) to be passed to the sendgrid email template | ||
const payload = { user }; | ||
await sender.sendEmail(process.env.SENDER_EMAIL, user.email, 'signup_template', payload); | ||
|
||
const userToken = JWTService.generateToken(user); | ||
return Response.Success(res, { | ||
id: user.id, | ||
...userData, | ||
role: user.role, | ||
is_verified: user.is_verified, | ||
token: userToken | ||
}, 201); | ||
} catch (error) { | ||
return Response.InternalServerError(res, 'Could not signup user'); | ||
} | ||
} | ||
|
||
/** | ||
* Handles signin requests | ||
* @param {ServerRequest} req | ||
* @param {ServerResponse} res | ||
* @returns {ServerResponse} response | ||
*/ | ||
static signin(req, res) { | ||
const signinError = { message: 'Incorrect email or password' }; | ||
UserUtils.findUserByEmail(req.body.email) | ||
.then((user) => { | ||
if (!user) return Response.UnauthorizedError(res, signinError); | ||
return UserUtils.validateUserPassword(user, req.body.password) | ||
.then((matches) => { | ||
if (!matches) return Response.UnauthorizedError(res, signinError); | ||
const data = UserUtils.getPublicUserFields(user.dataValues); | ||
data.token = JWTService.generateToken(data); | ||
return Response.Success(res, data); | ||
}); | ||
}) | ||
.catch(() => { | ||
Response.UnauthorizedError(res, signinError); | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.