Más cambios para refresh token

This commit is contained in:
David Arranz 2019-07-09 12:55:45 +02:00
parent a0ec336125
commit 122b31a7ad
3 changed files with 30 additions and 2 deletions

View File

@ -98,9 +98,27 @@ async function regenerateToken(req, res, next) {
}
}
async function rejectToken(req, res, next) {
const refreshToken = req.body.refreshToken;
const user = await authService.extraMethods.findUserByRefreshToken(refreshToken);
if (user && user.token === refreshToken ) {
try {
await authService.extraMethods.deleteRefreshToken(refreshToken);
return controllerHelper.handleResultResponse(result, null, req.params, res, httpStatus.OK);
} catch (error) {
controllerHelper.handleErrorResponse(MODULE_NAME, rejectToken.name, error, res);
}
} else {
return controllerHelper.handleResultResponse(null, null, req.params, res, httpStatus.NOT_FOUND);
}
}
module.exports = {
login,
register,
regenerateToken,
rejectToken,
MODULE_NAME
}

View File

@ -33,4 +33,9 @@ routes.post('/token',
authController.regenerateToken,
);
routes.post('/token/reject',
authController.rejectToken,
);
module.exports = routes;

View File

@ -32,9 +32,14 @@ const extraMethods = {
{ token: newRefreshToken },
{ where: { id: userId }}
);
},
deleteRefreshToken: async (userId, refreshToken) => {
return await models.User.update(
{ token: null },
{ where: { id: userId } }
);
}
}
module.exports = {