Skip to content

Commit

Permalink
feature(logout):add user logout router
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveFunso committed Sep 6, 2019
1 parent 525211b commit 8ebc81d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/controllers/logoutController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { respondWithSuccess } from '../helpers/responseHandler';


/**
* Creates a new user
* @param {Object} req
* @param {Object} res
* @returns {Function} CreateUser
*/

const logout = (req, res) => respondWithSuccess(res, 200, 'Successfully Logged out');

export default logout;
16 changes: 15 additions & 1 deletion src/docs/swagger/definitions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ const Signin = {
}
};

export { SigninCreate, Signin };
const Logout = {
type: 'object',
properties: {
success: {
type: 'boolean',
example: true
},
message: {
type: 'string',
example: 'logout successful'
}
}
};

export { SigninCreate, Signin, Logout };
19 changes: 16 additions & 3 deletions src/docs/swagger/paths/auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export const signInPath = {
post: {
tags: [
'auth'
],
tags: ['auth'],
summary: 'Sign a user in',
description: 'Allows registered user to signin',
parameters: [
Expand Down Expand Up @@ -44,3 +42,18 @@ export const signInPath = {
}
}
};
export const logoutPath = {
post: {
tags: ['auth'],
summary: 'Logout a user',
description: 'Allows logged in user to logout',
responses: {
200: {
description: 'User logout successfully',
schema: {
$ref: '#/definitions/Logout'
}
}
}
}
};
8 changes: 5 additions & 3 deletions src/docs/swagger/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
badRequest, notAuthorized, accessForbidden, notFound, conflict,
serverError
} from './definitions/errorResponse';
import { SigninCreate, Signin } from './definitions/auth';
import { SigninCreate, Signin, Logout } from './definitions/auth';
import {
createUser, updateRoleReq, updateRoleRes,
resetPassword, resetPasswordResponse, forgotPassword, resetUserPassword, verifyUser
Expand All @@ -18,7 +18,7 @@ import {
} from './definitions/accommodation';


import { signInPath } from './paths/auth';
import { signInPath, logoutPath } from './paths/auth';
import {
userRolePath, createUserPath, resetPasswordPath, forgotPasswordPath, resetUserPasswordPath,
verifyUserPath
Expand Down Expand Up @@ -84,6 +84,7 @@ const swaggerDocument = {
],
paths: {
'/auth/signin': signInPath,
'/auth/logout': logoutPath,
'/users': createUserPath,
'/users/verify': verifyUserPath,
'/users/roles/{roleId}': userRolePath,
Expand Down Expand Up @@ -131,7 +132,8 @@ const swaggerDocument = {
notFound, // 404
conflict, // 409
serverError, // 503
socialMediaAuthentication // social media login response
socialMediaAuthentication, // social media login response
Logout
}
};

Expand Down
6 changes: 5 additions & 1 deletion src/routes/api/auth.routes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Router } from 'express';
import passport from 'passport';
import { validateSigninFormData } from '../../middlewares/validateAuth';
import { validUser } from '../../middlewares/authentication';
import { validUser, isUserExist } from '../../middlewares/authentication';
import { signin, google, facebook } from '../../controllers/authController';
import logout from '../../controllers/logoutController';


const router = Router();

Expand All @@ -13,5 +15,7 @@ router.get('/google/redirect', google);

router.get('/facebook', passport.authenticate('facebook', { scope: ['email'] }));
router.get('/facebook/redirect', facebook);
router.get('/logout', isUserExist, logout);


export default router;
23 changes: 23 additions & 0 deletions src/test/logout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../index';

const { expect } = chai;
chai.use(chaiHttp);

const logoutUrl = '/api/v1/auth/logout';

describe('USER CONTROLLER', () => {
describe('GET LOG OUT', () => {
it('it should logout a user with a response indication successful logout', done => {
chai
.request(app)
.get(logoutUrl)
.end((error, res) => {
expect(res).to.have.status(200);
expect(res.body.message).to.be.equal('Successfully Logged out');
done();
});
});
});
});

0 comments on commit 8ebc81d

Please sign in to comment.