Skip to content

Commit

Permalink
Merge d9de9f4 into fd70f0f
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-abz committed Dec 20, 2019
2 parents fd70f0f + d9de9f4 commit dbcbde4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ class AuthController {
ResponseService.setSuccess(200, 'Successfully logged in', token);
ResponseService.send(res);
}

/**
* @description logs out a user
*
* @param {object} req request from body to log out
* @param {object} res response to the body
* @returns {object} @memberof AuthController
*/
static async logout(req, res) {
const token = null;
await UserService.updateUser({ id: req.userData.id }, { token });
ResponseService.setSuccess(200, 'Successful logout');
return ResponseService.send(res);
}
}

export default AuthController;
1 change: 1 addition & 0 deletions src/routes/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const router = express.Router();

router.post('/signup', validateSignup, authController.signUp); // API route for user to signup
router.post('/login', validateLogin, checkUserExist, authController.login); // API route for user to login
router.post('/logout', checkUserLoggedIn, authController.logout); // API for logout
router.get('/protected', checkUserLoggedIn, (req, res) => {
res.status(200).send('Welcome to the protected route');
});
Expand Down
2 changes: 1 addition & 1 deletion src/services/response.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class ResponseService {
*/
static send(res) {
const result = {
status: this.type,
status: this.statusCode,
message: this.message,
data: this.data,
};
Expand Down
47 changes: 47 additions & 0 deletions src/swagger/auth.swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,50 @@
* '401':
* description: Incorrect credentials.
*/
/**
* @swagger
* /api/auth/logout:
* post:
* tags:
* - User
* name: Logout
* summary: Logs out a user from system
* parameters:
* - name: authorization
* in: header
* schema:
* type: string
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: Succesful
* '401':
* description: No Token supplied
*/
/**
* @swagger
* /api/auth/protected:
* get:
* tags:
* - User
* name: Protected
* summary: Protected route that can be accessed only by logged in user
* parameters:
* - name: authorization
* in: header
* schema:
* type: string
* produces:
* - application/json
* consumes:
* - application/json
* responses:
* '200':
* description: Succesful
* '401':
* description: No Token supplied
*
*/
34 changes: 34 additions & 0 deletions src/tests/auth/logout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import chaiHttp from 'chai-http';
import chai, { expect } from 'chai';
import app from '../../app';
import { loggedInToken, createUsers } from '../fixtures/users.fixture';

chai.use(chaiHttp);
describe('Tests for user logout', () => {
before(async () => {
await createUsers();
});
it('Should logout a user', (done) => {
chai.request(app)
.post('/api/auth/logout')
.set('Authorization', loggedInToken)
.end((error, response) => {
expect(response).to.have.status(200);
expect(response.body).to.have.property('message')
.that.contain('Successful logout');
done(error);
});
});
it('Should not allow access to protected route after log out', (done) => {
chai.request(app)
.get('/api/auth/protected')
.set('Authorization', loggedInToken)
.end((err, res) => {
expect(res).to.have.status(401);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('message')
.that.contain('Unauthorized access. Invalid token for this user');
done(err);
});
});
});
1 change: 1 addition & 0 deletions src/tests/fixtures/users.fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const loggedInToken = JwtService.generateToken({
});

export const createUsers = async () => {
await Users.destroy({ where: {} });
await Users.create(activeUser);
await Users.create({ ...loggedInUser, token: loggedInToken });
};
Expand Down

0 comments on commit dbcbde4

Please sign in to comment.