Skip to content

Commit fe15e65

Browse files
committed
Change refresh tokens to expire in Redis and to delete based on session id
1 parent 2ef0d9a commit fe15e65

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

apps/api/src/app/auth/auth.controller.ts

-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { RedisService } from "@liaoliaots/nestjs-redis";
3333
import Redis from "ioredis";
3434
import { DeleteApiKeyDto } from "./dto/deleteApiKey.dto";
3535
import { CreateApiKeyDto } from "./dto/createApiKey.dto";
36-
import { AccessTokenAuthenticatedGuard } from "./guards/accessTokenAuthenticated.guard";
3736
import { ResetEmailDto } from "./dto/resetEmail.dto";
3837

3938
@ApiTags("Authentication")
@@ -484,7 +483,6 @@ export class AuthController {
484483
*
485484
* @returns Void
486485
*/
487-
@UseGuards(AccessTokenAuthenticatedGuard)
488486
@Post("logout")
489487
logout(
490488
@Req() req: ExpressRequest,

apps/api/src/app/auth/auth.service.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ export class AuthService {
117117
},
118118
{ expiresIn: "182d" }
119119
);
120+
const refreshTokenExpiry = new Date(new Date().setDate(new Date().getDate() + 182));
120121

121-
res.cookie("refresh_token", refreshToken, { httpOnly: true, expires: new Date(new Date().setDate(new Date().getDate() + 182)) });
122+
res.cookie("refresh_token", refreshToken, { httpOnly: true, expires: refreshTokenExpiry });
122123
this.refreshTokenRedis.set(sessionId, refreshToken);
124+
this.refreshTokenRedis.expire(sessionId, (refreshTokenExpiry.getTime() - new Date().getTime()) / 1000);
123125

124126
res.cookie("access_token", this.jwtService.sign(
125127
{
@@ -143,7 +145,7 @@ export class AuthService {
143145

144146
const user = jwt.decode(req.cookies.access_token);
145147
if (user && "email" in (user as jwt.JwtPayload)) {
146-
this.refreshTokenRedis.del(user["email"]);
148+
this.refreshTokenRedis.del(user["sessionId"]);
147149
}
148150
}
149151
}

apps/api/src/app/providers/token-refresh.middleware.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,16 @@ export class TokenRefreshMiddleware implements NestMiddleware {
5656
}
5757

5858
renewAccessToken(req: Request, res: Response): boolean {
59-
let refreshToken: { id: string; email: string; type: "refresh" };
59+
let refreshToken: { id: string; sessionId: string; email: string; type: "refresh" };
6060

61-
if (!req.cookies["refresh_token"] || !req.cookies["refresh_token"].sessionId) {
62-
this.authService.logout(req, res);
63-
return true;
64-
}
65-
66-
if (!this.redis.get(req.cookies["refresh_token"].sessionId)) {
61+
try {
62+
refreshToken = jwt.verify(req.cookies["refresh_token"], this.configService.get<string>("JWT_SECRET")) as { id: string; sessionId: string; email: string; type: "refresh" };
63+
} catch (e) {
6764
this.authService.logout(req, res);
6865
return true;
6966
}
7067

71-
try {
72-
refreshToken = jwt.verify(req.cookies["refresh_token"], this.configService.get<string>("JWT_SECRET")) as { id: string; email: string; type: "refresh" };
73-
} catch (e) {
68+
if (!refreshToken.sessionId || !this.redis.get(req.cookies["refresh_token"].sessionId)) {
7469
this.authService.logout(req, res);
7570
return true;
7671
}

0 commit comments

Comments
 (0)