Skip to content

Commit

Permalink
Add logout endpoint
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
hagopj13 committed Aug 21, 2020
1 parent 57bd26d commit 750feb5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const login = catchAsync(async (req, res) => {
res.send({ user, tokens });
});

const logout = catchAsync(async (req, res) => {
await authService.logout(req.body.refreshToken);
res.status(httpStatus.NO_CONTENT).send();
});

const refreshTokens = catchAsync(async (req, res) => {
const tokens = await authService.refreshAuth(req.body.refreshToken);
res.send({ ...tokens });
Expand All @@ -34,6 +39,7 @@ const resetPassword = catchAsync(async (req, res) => {
module.exports = {
register,
login,
logout,
refreshTokens,
forgotPassword,
resetPassword,
Expand Down
28 changes: 28 additions & 0 deletions src/routes/v1/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const router = express.Router();

router.post('/register', validate(authValidation.register), authController.register);
router.post('/login', validate(authValidation.login), authController.login);
router.post('/logout', validate(authValidation.logout), authController.logout);
router.post('/refresh-tokens', validate(authValidation.refreshTokens), authController.refreshTokens);
router.post('/forgot-password', validate(authValidation.forgotPassword), authController.forgotPassword);
router.post('/reset-password', validate(authValidation.resetPassword), authController.resetPassword);
Expand Down Expand Up @@ -118,6 +119,33 @@ module.exports = router;
* message: Invalid email or password
*/

/**
* @swagger
* path:
* /auth/logout:
* post:
* summary: Logout
* tags: [Auth]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - refreshToken
* properties:
* refreshToken:
* type: string
* example:
* refreshToken: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1ZWJhYzUzNDk1NGI1NDEzOTgwNmMxMTIiLCJpYXQiOjE1ODkyOTg0ODQsImV4cCI6MTU4OTMwMDI4NH0.m1U63blB0MLej_WfB7yC2FTMnCziif9X8yzwDEfJXAg
* responses:
* "204":
* description: No content
* "401":
* $ref: '#/components/responses/Unauthorized'
*/

/**
* @swagger
* path:
Expand Down
15 changes: 15 additions & 0 deletions src/services/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ const loginUserWithEmailAndPassword = async (email, password) => {
return user;
};

/**
* Logout
* @param {string} refreshToken
* @returns {Promise}
*/
const logout = async (refreshToken) => {
try {
const refreshTokenDoc = await tokenService.verifyToken(refreshToken, 'refresh');
await refreshTokenDoc.remove();
} catch (error) {
throw new ApiError(httpStatus.UNAUTHORIZED, 'Please authenticate');
}
};

/**
* Refresh auth tokens
* @param {string} refreshToken
Expand Down Expand Up @@ -59,6 +73,7 @@ const resetPassword = async (resetPasswordToken, newPassword) => {

module.exports = {
loginUserWithEmailAndPassword,
logout,
refreshAuth,
resetPassword,
};
7 changes: 7 additions & 0 deletions src/validations/auth.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const login = {
}),
};

const logout = {
body: Joi.object().keys({
refreshToken: Joi.string().required(),
}),
};

const refreshTokens = {
body: Joi.object().keys({
refreshToken: Joi.string().required(),
Expand All @@ -40,6 +46,7 @@ const resetPassword = {
module.exports = {
register,
login,
logout,
refreshTokens,
forgotPassword,
resetPassword,
Expand Down

0 comments on commit 750feb5

Please sign in to comment.