Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit e5b50f8

Browse files
committed
fix: refresh token gets deleted on session end
1 parent b731442 commit e5b50f8

File tree

5 files changed

+23
-23
lines changed

5 files changed

+23
-23
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { PickType } from "@nestjs/mapped-types";
2-
import { IsEmail, IsOptional, IsString } from "class-validator";
32
import { UserDTO } from "src/user/dto/user.dto";
43

54
export class EnableTotpDTO extends PickType(UserDTO, ["password"] as const) {}

backend/src/auth/dto/verifyTotp.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PickType } from "@nestjs/mapped-types";
2-
import { IsEmail, IsOptional, IsString } from "class-validator";
2+
import { IsString } from "class-validator";
33
import { UserDTO } from "src/user/dto/user.dto";
44

55
export class VerifyTotpDTO extends PickType(UserDTO, ["password"] as const) {

frontend/src/components/auth/SignInForm.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from "@mantine/core";
1111
import { useForm, yupResolver } from "@mantine/form";
1212
import { showNotification } from "@mantine/notifications";
13-
import { setCookie } from "cookies-next";
1413
import Link from "next/link";
1514
import React from "react";
1615
import { TbInfoCircle } from "react-icons/tb";
@@ -59,8 +58,6 @@ const SignInForm = () => {
5958
});
6059
setLoginToken(response.data["loginToken"]);
6160
} else {
62-
setCookie("access_token", response.data.accessToken);
63-
setCookie("refresh_token", response.data.refreshToken);
6461
window.location.replace("/");
6562
}
6663
})
@@ -70,11 +67,7 @@ const SignInForm = () => {
7067
const signInTotp = (email: string, password: string, totp: string) => {
7168
authService
7269
.signInTotp(email, password, totp, loginToken)
73-
.then((response) => {
74-
setCookie("access_token", response.data.accessToken);
75-
setCookie("refresh_token", response.data.refreshToken);
76-
window.location.replace("/");
77-
})
70+
.then(() => window.location.replace("/"))
7871
.catch((error) => {
7972
if (error?.response?.data?.message == "Login token expired") {
8073
toast.error("Login token expired");

frontend/src/components/auth/SignUpForm.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
Title,
1010
} from "@mantine/core";
1111
import { useForm, yupResolver } from "@mantine/form";
12-
import { setCookie } from "cookies-next";
1312
import Link from "next/link";
1413
import * as yup from "yup";
1514
import useConfig from "../../hooks/config.hook";
@@ -37,11 +36,7 @@ const SignUpForm = () => {
3736
const signUp = (email: string, username: string, password: string) => {
3837
authService
3938
.signUp(email, username, password)
40-
.then((response) => {
41-
setCookie("access_token", response.data.accessToken);
42-
setCookie("refresh_token", response.data.refreshToken);
43-
window.location.replace("/");
44-
})
39+
.then(() => window.location.replace("/"))
4540
.catch(toast.axiosError);
4641
};
4742

frontend/src/services/auth.service.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ const signIn = async (emailOrUsername: string, password: string) => {
1111
...emailOrUsernameBody,
1212
password,
1313
});
14+
15+
setCookie("access_token", response.data.accessToken);
16+
setCookie("refresh_token", response.data.refreshToken, {
17+
maxAge: 60 * 60 * 24 * 30 * 3,
18+
});
19+
1420
return response;
1521
};
1622

@@ -34,7 +40,14 @@ const signInTotp = async (
3440
};
3541

3642
const signUp = async (email: string, username: string, password: string) => {
37-
return await api.post("auth/signUp", { email, username, password });
43+
const response = await api.post("auth/signUp", { email, username, password });
44+
45+
setCookie("access_token", response.data.accessToken);
46+
setCookie("refresh_token", response.data.refreshToken, {
47+
maxAge: 60 * 60 * 24 * 30 * 3,
48+
});
49+
50+
return response;
3851
};
3952

4053
const signOut = () => {
@@ -45,14 +58,14 @@ const signOut = () => {
4558

4659
const refreshAccessToken = async () => {
4760
try {
48-
const currentAccessToken = getCookie("access_token") as string;
61+
const accessToken = getCookie("access_token") as string;
62+
const refreshToken = getCookie("refresh_token");
4963
if (
50-
currentAccessToken &&
51-
(jose.decodeJwt(currentAccessToken).exp ?? 0) * 1000 <
52-
Date.now() + 2 * 60 * 1000
64+
(accessToken &&
65+
(jose.decodeJwt(accessToken).exp ?? 0) * 1000 <
66+
Date.now() + 2 * 60 * 1000) ||
67+
(refreshToken && !accessToken)
5368
) {
54-
const refreshToken = getCookie("refresh_token");
55-
5669
const response = await api.post("auth/token", { refreshToken });
5770
setCookie("access_token", response.data.accessToken);
5871
}

0 commit comments

Comments
 (0)