Skip to content

Commit

Permalink
feature(signin): implement in-app signin
Browse files Browse the repository at this point in the history
[Finishes #167727454, Delivers #167727454]
  • Loading branch information
Qausim committed Aug 27, 2019
1 parent 1a1020e commit 0440d19
Show file tree
Hide file tree
Showing 21 changed files with 627 additions and 179 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"new-cap": 0,
"consistent-return": 0,
"no-param-reassign": 0,
"linebreak-style": 0,
"comma-dangle": 0,
"curly": ["error", "multi-line"],
"import/no-unresolved": [2, { "commonjs": true }],
Expand Down
62 changes: 27 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"dependencies": {
"@sendgrid/mail": "^6.4.0",
"bcrypt": "^3.0.6",
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"chai-subset": "^1.6.0",
Expand All @@ -40,6 +39,7 @@
"express": "^4.16.3",
"express-jwt": "^5.3.1",
"express-session": "^1.15.6",
"express-validator": "^6.1.1",
"jsonwebtoken": "^8.3.0",
"morgan": "^1.9.1",
"passport": "^0.4.0",
Expand Down
69 changes: 69 additions & 0 deletions src/controllers/user.controller.js
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);
});
}
}
48 changes: 0 additions & 48 deletions src/controllers/users.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import express from 'express';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from '../swagger.json';
import router from './routes';
import router from './routes/api';

// Create global app object
const app = express();


app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
Expand Down
Loading

0 comments on commit 0440d19

Please sign in to comment.