Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
ft(logout): add log out functionality (#20)
Browse files Browse the repository at this point in the history
- add a log-out route
- add a logout function
- add logout tests
- change authController to userController
- change authRoute to userRoutes

[Finishes #170947550]
  • Loading branch information
bbaime98 committed Feb 17, 2020
1 parent 52087b4 commit b50ff29
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 164 deletions.
33 changes: 31 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"express-validator": "^6.4.0",
"i18n": "^0.8.5",
"jsonwebtoken": "^8.5.1",
"localStorage": "^1.0.4",
"path": "^0.12.7",
"pg": "^7.18.1",
"pg-hstore": "^2.3.3",
Expand Down
21 changes: 18 additions & 3 deletions src/controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import uuid from 'uuid/v4';
import bcrypt from 'bcrypt';
import dotenv from 'dotenv';
import localStorage from 'localStorage';
import db from '../models';
import sendMsg from '../utils/user-created-email';
import provideToken from '../utils/provideToken';
import Response from '../utils/ResponseHandler';


dotenv.config();
/**
*
* @description Authentication Controller
* @description AuthController Controller
* @class AuthController
*/
export default class AuthController {
Expand Down Expand Up @@ -46,6 +46,7 @@ export default class AuthController {
password: hashedPassword,
});
const token = provideToken(user.id, user.isVerified, email);
localStorage.setItem('token', token);
sendMsg(email, token, firstName);
return Response.signupResponse(res, 201, 'User successfully registered', token);
} catch (error) {
Expand All @@ -59,7 +60,7 @@ export default class AuthController {
* @param {Object} req
* @param {Object} res
* @returns {Object} User
* @memberof authController
* @memberof AuthController
*/
static async login(req, res) {
try {
Expand All @@ -76,11 +77,25 @@ export default class AuthController {
}
if (bcrypt.compareSync(password, user.password)) {
const token = provideToken(user.dataValues.id, user.dataValues.isVerified);
localStorage.setItem('token', token);
return Response.login(res, 200, 'User is successfully logged in', token);
}
return Response.errorResponse(res, 401, 'Incorrect email or password');
} catch (error) {
return Response.errorResponse(res, 500, error.message);
}
}

/**
* @description logout method
* @static
* @param {Object} req
* @param {Object} res
* @returns {Object} User
* @memberof AuthController
*/
static async logout(req, res) {
localStorage.removeItem('token');
return Response.login(res, 200, res.__('User is successfully logged out'));
}
}
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const app = express();
app.use(i18n.init);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const port = process.env.PORT || 3000;

app.use('/api', welcome);
Expand Down
6 changes: 3 additions & 3 deletions src/routes/authRoutes.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import express from 'express';
import AuthController from '../controllers/authController';
import verificationController from '../controllers/verificationController';
import validateParams from '../validation/validateParams';
import validationResult from '../validation/validationResult';
import signupInputRules from '../validation/validationRules';

import verificationController from '../controllers/verificationController';
import validateParams from '../validation/validateParams';

const authRouter = express.Router();

authRouter.post('/register', signupInputRules, validationResult, AuthController.registerUser);
authRouter.get('/verification', validateParams.validateToken, verificationController.verifyAccount);
authRouter.post('/login', AuthController.login);
authRouter.get('/logout', AuthController.logout);

export default authRouter;
11 changes: 0 additions & 11 deletions src/routes/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ import { Router } from 'express';

const router = Router();

/**
* @swagger
* /api:
* get:
* description: Display welcome message
* produces:
* - application/json
* responses:
* 200:
* description: Welcome to devRepublic Barefoot Nomad API
*/
router.get('/', (req, res) => {
res.status(200).json({
message: res.__('Welcome to devRepublic Barefoot Nomad API')
Expand Down
12 changes: 7 additions & 5 deletions src/services/localesServices/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"Welcome to devRepublic Barefoot Nomad API": "Welcome to devRepublic Barefoot Nomad API",
"register": "register",
"signup": "signup",
"login": "login",
"logout": "logout"
"Welcome to devRepublic Barefoot Nomad API": "Welcome to devRepublic Barefoot Nomad API",
"register": "register",
"signup": "signup",
"login": "login",
"logout": "logout",
"User is successfully logged out": "User is successfully logged out",
"Login first or create an account if you do not have one": "Login first or create an account if you do not have one"
}
11 changes: 6 additions & 5 deletions src/services/localesServices/locales/fr.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"Welcome to devRepublic Barefoot Nomad API": "Bienvenue au devRepublic Barefoot Nomad API",
"register": "enregistrer",
"signup": "enregistrer",
"login": "connecter",
"logout": "déconnecter"
"Welcome to devRepublic Barefoot Nomad API": "Bienvenue au devRepublic Barefoot Nomad API",
"register": "enregistrer",
"signup": "enregistrer",
"login": "connecter",
"logout": "déconnecter",
"User is successfully logged out": "l'utilisateur s'est déconnecté avec succès"
}
149 changes: 149 additions & 0 deletions src/swagger/auth.swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,131 @@
* description: Incorrect email or password.
* */

/**
* @swagger
* /:
* get:
* description: Display welcome message
* produces:
* - application/json
* responses:
* 200:
* description: Welcome to devRepublic Barefoot Nomad API
*/
/**
* @swagger
* definitions:
* register:
* type: object
* properties:
* firstName:
* type: string
* lastName:
* type: string
* email:
* type: string
* format: email
* password:
* type: string
* format: password
* required:
* - firstName
* - lastName
* - email
* - password
*/
/**
* @swagger
* /api/v1/auth/register:
* post:
* tags:
* - User
* name: Signup
* summary: Signup a user in a system
* produces:
* - application/json
* consumes:
* - application/json
* parameters:
* - name: body
* in: body
* schema:
* $ref: '#/definitions/register'
* type: object
* properties:
* firstName:
* type: string
* lastName:
* type: string
* email:
* type: string
* password:
* type: string
* format: password
* required:
* - firstName
* - lastName
* - email
* - password
* responses:
* '201':
* description: User created.
* '400':
* description: Bad request.
* '409':
* description: User already exist.
* */
/**
* @swagger
* definitions:
* login:
* type: object
* properties:
* email:
* type: string
* format: email
* password:
* type: string
* format: password
* required:
* - email
* - password
*/
/**
* @swagger
* /api/v1/auth/login:
* post:
* tags:
* - User
* name: login
* summary: login a user in a system
* produces:
* - application/json
* consumes:
* - application/json
* parameters:
* - name: body
* in: body
* schema:
* $ref: '#/definitions/login'
* type: object
* properties:
* email:
* type: string
* password:
* type: string
* format: password
* required:
* - email
* - password
* responses:
* '200':
* description: User logged in.
* '400':
* description: Bad request.
* '401':
* description: Incorrect email or password.
* */
/**
* @swagger
* /api/v1/auth/verification/token={token}&email={email}:
Expand Down Expand Up @@ -155,3 +280,27 @@
* '401':
* description: Sorry, you are not authorized to access this page.
* */

/**
* @swagger
* definitions:
* logout:
* type: object
*/
/**
* @swagger
* /api/v1/auth/logout:
* get:
* tags:
* - User
* name: logout
* summary: Logs a user out
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: User is successfully logged out
* */

Loading

0 comments on commit b50ff29

Please sign in to comment.