Skip to content

Commit 63646a5

Browse files
authored
Merge pull request #120 from njxue/fix-auth
Fix auth
2 parents 90c93a9 + cfb09f9 commit 63646a5

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

backend/user-service/.env.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ SMTP_PASSWORD=scdqcveqpurzzajj
4747
APP_URL=http://localhost:3000
4848

4949
# Feature flags
50-
DEFAULT_ADMIN_ON_REGISTER_FEATURE=true
50+
DEFAULT_ADMIN_ON_REGISTER_FEATURE=true
51+
52+
# Token expiry duration
53+
JWT_ACCESS_TOKEN_EXPIRY="1d"
54+
JWT_REFRESH_TOKEN_EXPIRY="7d"
55+
JWT_RESET_TOKEN_EXPIRY="15m"

backend/user-service/config/authConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ dotenv.config();
44

55
export const jwtConfig = {
66
refreshTokenOptions: {
7-
expiresIn: process.env.ENV === "production" ? "7d" : "1d", // Shorter duration in dev for testing
7+
expiresIn: process.env.JWT_REFRESH_TOKEN_EXPIRY
88
},
99
accessTokenOptions: {
10-
expiresIn: process.env.ENV === "production" ? "15m" : "30s", // Shorter duration in dev for testing
10+
expiresIn: process.env.JWT_ACCESS_TOKEN_EXPIRY
1111
},
1212
resetTokenOptions: {
13-
expiresIn: "15m",
13+
expiresIn: process.env.JWT_RESET_TOKEN_EXPIRY,
1414
},
1515
accessTokenSecret: process.env.JWT_ACCESS_TOKEN_SECRET,
1616
refreshTokenSecret: process.env.JWT_REFRESH_TOKEN_SECRET,

frontend/src/infrastructure/Api/BaseApi.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class BaseApi {
2121
baseURL: API_URL + baseUrl,
2222
timeout: 10000,
2323
headers: {
24-
"Content-Type": "application/json",
24+
"Content-Type": "application/json"
2525
},
2626
withCredentials: true
2727
});
@@ -42,21 +42,26 @@ export class BaseApi {
4242

4343
private setUpResponseInterceptors(axiosInstance: AxiosInstance): number {
4444
return axiosInstance.interceptors.response.use(
45-
(response: AxiosResponse) => response,
45+
(response: AxiosResponse) => {
46+
return response;
47+
},
4648
async (err: AxiosError) => {
4749
const prevRequest = err?.config;
4850
if (prevRequest && err?.response?.status === 401) {
4951
try {
5052
// Eject to prevent infinite loop
51-
this.protectedAxiosInstance.interceptors.response.eject(this.protectedResponseInterceptorId);
5253
const newAccessToken = await userUseCases.refreshToken();
5354
prevRequest.headers["Authorization"] = `Bearer ${newAccessToken}`;
54-
AuthClientStore.setAccessToken(newAccessToken);
55+
if (newAccessToken) {
56+
AuthClientStore.setAccessToken(newAccessToken);
57+
}
5558
return this.protectedAxiosInstance(prevRequest);
5659
} catch (error) {
5760
// Refresh token expired/invalid
5861
console.error(error);
5962
return Promise.reject(error);
63+
} finally {
64+
this.protectedAxiosInstance.interceptors.response.eject(this.protectedResponseInterceptorId);
6065
}
6166
}
6267
return Promise.reject(err);

0 commit comments

Comments
 (0)