Skip to content

Commit

Permalink
refactor token maxage const and update usage
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylli committed Jan 20, 2024
1 parent 1326859 commit bc63123
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import { ResetPasswordDto } from "./dto/reset-password.dto";
import { JwtAuthGuard } from "./guards/jwt-auth.guard";
import { JwtRefreshAuthGuard } from "./guards/jwt-rt-auth.guard";
import { Public } from "../global/decorators/public.decorator";

const AT_MAX_AGE: number = 1000 * 60 * 15;
const RT_MAX_AGE: number = 1000 * 60 * 60 * 24 * 7;
import { AT_MAX_AGE, RT_MAX_AGE } from "../global/constants";

@ApiTags("Auth")
@Controller("auth")
Expand Down Expand Up @@ -138,12 +136,12 @@ export class AuthController {
req.user,
);
res.cookie("access_token", access_token, {
maxAge: AT_MAX_AGE,
maxAge: AT_MAX_AGE * 1000,
httpOnly: true,
secure: true,
});
res.cookie("refresh_token", refresh_token, {
maxAge: RT_MAX_AGE,
maxAge: RT_MAX_AGE * 1000,
httpOnly: true,
secure: true,
});
Expand Down
5 changes: 3 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { VerifyEmailDto } from "./dto/verify-email.dto";
import { ResetPasswordRequestDto } from "./dto/reset-password-request.dto";
import { ResetPasswordDto } from "./dto/reset-password.dto";
import * as process from "process";
import { AT_MAX_AGE, RT_MAX_AGE } from "../global/constants";

@Injectable()
export class AuthService {
Expand All @@ -44,11 +45,11 @@ export class AuthService {
const [at, rt] = await Promise.all([
this.jwtService.signAsync(payload, {
secret: process.env.AT_SECRET,
expiresIn: 60 * 60,
expiresIn: AT_MAX_AGE,
}),
this.jwtService.signAsync(payload, {
secret: process.env.RT_SECRET,
expiresIn: 60 * 60 * 24 * 7,
expiresIn: RT_MAX_AGE,
}),
]);
return {
Expand Down
3 changes: 3 additions & 0 deletions src/global/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Access token and Refresh token expiry in seconds
export const AT_MAX_AGE: number = 60 * 30;
export const RT_MAX_AGE: number = 60 * 60 * 24 * 7;

0 comments on commit bc63123

Please sign in to comment.