mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
fix: loggin out keeps refresh token in browser
fix: https://github.com/canove/whaticket/issues/106
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { verify } from "jsonwebtoken";
|
||||
import { Response as Res } from "express";
|
||||
|
||||
import User from "../../models/User";
|
||||
import AppError from "../../errors/AppError";
|
||||
@@ -20,25 +21,27 @@ interface Response {
|
||||
refreshToken: string;
|
||||
}
|
||||
|
||||
export const RefreshTokenService = async (token: string): Promise<Response> => {
|
||||
let decoded;
|
||||
|
||||
export const RefreshTokenService = async (
|
||||
res: Res,
|
||||
token: string
|
||||
): Promise<Response> => {
|
||||
try {
|
||||
decoded = verify(token, authConfig.refreshSecret);
|
||||
const decoded = verify(token, authConfig.refreshSecret);
|
||||
const { id, tokenVersion } = decoded as RefreshTokenPayload;
|
||||
|
||||
const user = await ShowUserService(id);
|
||||
|
||||
if (user.tokenVersion !== tokenVersion) {
|
||||
res.clearCookie("jrt");
|
||||
throw new AppError("ERR_SESSION_EXPIRED", 401);
|
||||
}
|
||||
|
||||
const newToken = createAccessToken(user);
|
||||
const refreshToken = createRefreshToken(user);
|
||||
|
||||
return { user, newToken, refreshToken };
|
||||
} catch (err) {
|
||||
res.clearCookie("jrt");
|
||||
throw new AppError("ERR_SESSION_EXPIRED", 401);
|
||||
}
|
||||
|
||||
const { id, tokenVersion } = decoded as RefreshTokenPayload;
|
||||
|
||||
const user = await ShowUserService(id);
|
||||
|
||||
if (user.tokenVersion !== tokenVersion) {
|
||||
throw new AppError("ERR_SESSION_EXPIRED", 401);
|
||||
}
|
||||
|
||||
const newToken = createAccessToken(user);
|
||||
const refreshToken = createRefreshToken(user);
|
||||
|
||||
return { user, newToken, refreshToken };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user