Skip to content

Commit

Permalink
fix: refresh token expired in (#196)
Browse files Browse the repository at this point in the history
Co-authored-by: ZoanhLuong <[email protected]>
  • Loading branch information
hoangday185 and LuongDangDoanh authored Jul 5, 2024
1 parent 5b4d534 commit 26814bc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const whitelist = [

// THIS IS FOR TESTING ONLY
const corsOptions = {
origin: "*",
origin: whitelist,
credentials: true, // access-control-allow-credentials:true
allowedHeaders: ["Content-Type", "Authorization"], // access-control-allow-headers
optionSuccessStatus: 200,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/user.messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export const USER_MESSAGES = {
WRONG_PASS_5_TIMES: "Entered wrong password over 5 times!",

//token

REFRESH_TOKEN_IS_REQUIRED: "Refresh token is required",
OTP_IS_INCORRECT: "OTP is incorrect",

// block
USER_HAS_BEEN_BLOCKED: "user has been blocked",
USER_UNBLOCK_SUCCESSFULLY: "user unblock successfully",
Expand Down
33 changes: 33 additions & 0 deletions src/modules/user/user.middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { PROTECT_MESSAGES } from "../protectRouting/protect.messages";
import { checkRole, routesConfig } from "../protectRouting/protect.utils";
import { LoginRequestBody, TokenPayload } from "./user.requests";
import usersService from "./user.services";
import jwt from "jsonwebtoken";
//! Prevent db injection, XSS attack
export const paramSchema: ParamSchema = {
customSanitizer: {
Expand Down Expand Up @@ -1244,9 +1245,41 @@ export const refreshTokenCookieValidator = async (
});
}

const access_token = req.headers.authorization?.split(" ")[1];
const decoded_access_token = jwt.verify(
access_token as string,
process.env.JWT_SECRET_ACCESS_TOKEN as string,
{
ignoreExpiration: true,
},
) as TokenPayload;

if (decoded_access_token.user_id !== decoded_refresh_token.user_id) {
next(
new ErrorWithStatus({
message: USER_MESSAGES.REFRESH_TOKEN_NOT_VALID,
status: HTTP_STATUS.UNAUTHORIZED,
}),
);
}

req.decoded_refresh_token = decoded_refresh_token;
} catch (error) {
if (error instanceof JsonWebTokenError) {
if (error.message === "jwt expired") {
res.clearCookie("refresh_token");
await databaseService.refreshTokens.deleteOne({
token: value,
});
next(
new ErrorWithStatus({
message: capitalize(
(error as JsonWebTokenError).message,
),
status: HTTP_STATUS.UNAUTHORIZED,
}),
);
}
next(
new ErrorWithStatus({
message: capitalize((error as JsonWebTokenError).message),
Expand Down
5 changes: 2 additions & 3 deletions src/modules/user/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,15 @@ usersRouter.post(
usersRouter.post(
"/logout",
accessTokenValidator,
refreshTokenCookieValidator,
wrapAsync(refreshTokenCookieValidator),
wrapAsync(logoutController),
);

usersRouter.post(
"/refresh-token",
refreshTokenCookieValidator,
wrapAsync(refreshTokenCookieValidator),
wrapAsync(refreshTokenController),
);

usersRouter.post(
"/block",
accessTokenValidator,
Expand Down

0 comments on commit 26814bc

Please sign in to comment.