Make authentication more reliable
All checks were successful
Test - Build & Deploy to Scaleway / build-and-push-image-lecoffre (push) Successful in 1m15s
Test - Build & Deploy to Scaleway / deploy-to-scaleway-lecoffre (push) Successful in 3s

This commit is contained in:
Sosthene 2025-08-03 18:00:28 +02:00
parent b63973a9ba
commit e87b1ddde5
4 changed files with 181 additions and 30 deletions

View File

@ -30,6 +30,15 @@ export default abstract class BaseApiService {
}
protected buildHeaders(contentType: ContentType) {
// Don't try to access cookies during server-side rendering
if (typeof window === 'undefined') {
const headers = new Headers();
if (contentType === ContentType.JSON || contentType === ContentType.PDF) {
headers.set("Content-Type", contentType);
}
return headers;
}
const token = CookieService.getInstance().getCookie("leCoffreAccessToken");
const headers = new Headers();
@ -37,7 +46,14 @@ export default abstract class BaseApiService {
if (contentType === ContentType.JSON || contentType === ContentType.PDF) {
headers.set("Content-Type", contentType);
}
headers.set("Authorization", `Bearer ${token}`);
// Only set Authorization header if token exists
if (token) {
headers.set("Authorization", `Bearer ${token}`);
} else {
console.warn("No access token found in cookies when building headers");
}
return headers;
}
@ -134,38 +150,66 @@ export default abstract class BaseApiService {
}
private async checkJwtToken() {
// Don't check tokens during server-side rendering
if (typeof window === 'undefined') {
return;
}
const accessToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
if (!accessToken) return;
const userDecodedToken = jwt_decode(accessToken) as IUserJwtPayload;
const customerDecodedToken = jwt_decode(accessToken) as ICustomerJwtPayload;
if (!userDecodedToken && !customerDecodedToken) return;
const now = Math.floor(Date.now() / 1000);
if (userDecodedToken.userId && userDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
return;
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
return;
}
await JwtService.getInstance().refreshToken(refreshToken);
if (!accessToken) {
console.warn("No access token found during JWT check");
return;
}
if (customerDecodedToken.customerId && customerDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
try {
const userDecodedToken = jwt_decode(accessToken) as IUserJwtPayload;
const customerDecodedToken = jwt_decode(accessToken) as ICustomerJwtPayload;
if (!userDecodedToken && !customerDecodedToken) {
console.warn("Invalid token format during JWT check");
return;
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
return;
const now = Math.floor(Date.now() / 1000);
if (userDecodedToken.userId && userDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
console.warn("Access token expired but no refresh token found");
return;
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
console.warn("Both access and refresh tokens are expired");
return;
}
const refreshSuccess = await JwtService.getInstance().refreshToken(refreshToken);
if (!refreshSuccess) {
console.error("Failed to refresh token");
return;
}
}
await JwtService.getInstance().refreshToken(refreshToken);
if (customerDecodedToken.customerId && customerDecodedToken.exp < now) {
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (!refreshToken) {
console.warn("Access token expired but no refresh token found");
return;
}
const decodedRefreshToken = jwt_decode(refreshToken) as IUserJwtPayload | ICustomerJwtPayload;
if (decodedRefreshToken.exp < now) {
console.warn("Both access and refresh tokens are expired");
return;
}
const refreshSuccess = await JwtService.getInstance().refreshToken(refreshToken);
if (!refreshSuccess) {
console.error("Failed to refresh token");
return;
}
}
} catch (error) {
console.error("Error during JWT token check:", error);
}
return;
}
protected async processResponse<T>(response: Response, request: () => Promise<Response>, ref?: IRef, fileName?: string): Promise<T> {

View File

@ -1,5 +1,6 @@
import Users from "@Front/Api/LeCoffreApi/Notary/Users/Users";
import JwtService from "@Front/Services/JwtService/JwtService";
import UserStore from "@Front/Stores/UserStore";
import User from "le-coffre-resources/dist/Notary";
import { useEffect, useState } from "react";
@ -7,8 +8,23 @@ export default function useUser() {
const [user, setUser] = useState<User | null>();
useEffect(() => {
// Don't run on server-side
if (typeof window === 'undefined') {
return;
}
// Check if user is connected before making API calls
if (!UserStore.instance.isConnected()) {
console.warn("User not connected, skipping API call");
return;
}
const decodedJwt = JwtService.getInstance().decodeJwt();
if (!decodedJwt) return;
if (!decodedJwt) {
console.warn("No valid JWT found, skipping API call");
return;
}
Users.getInstance()
.getByUid(decodedJwt.userId, {
q: {
@ -17,6 +33,9 @@ export default function useUser() {
})
.then((user) => {
setUser(user);
})
.catch((error) => {
console.error("Failed to fetch user:", error);
});
}, []);

View File

@ -9,10 +9,46 @@ export default class UserStore {
protected readonly event = new EventEmitter();
public accessToken: string | null = null;
public refreshToken: string | null = null;
private initialized = false;
private constructor() {}
private constructor() {
// Don't initialize tokens during server-side rendering
if (typeof window !== 'undefined') {
this.initializeFromCookies();
}
}
private initializeFromCookies() {
if (this.initialized) return;
try {
const accessToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (accessToken) {
this.accessToken = accessToken;
}
if (refreshToken) {
this.refreshToken = refreshToken;
}
this.initialized = true;
} catch (error) {
console.warn("Failed to initialize tokens from cookies:", error);
}
}
public isConnected(): boolean {
// Ensure initialization on client side
if (typeof window !== 'undefined' && !this.initialized) {
this.initializeFromCookies();
}
// Check both instance variable and cookie to ensure consistency
if (typeof window !== 'undefined') {
const cookieToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
return !!(this.accessToken || cookieToken);
}
return !!this.accessToken;
}
@ -27,6 +63,10 @@ export default class UserStore {
CookieService.getInstance().setCookie("leCoffreAccessToken", accessToken);
CookieService.getInstance().setCookie("leCoffreRefreshToken", refreshToken);
// Update instance variables
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.event.emit("connection", this.accessToken);
} catch (error) {
console.error(error);
@ -41,6 +81,10 @@ export default class UserStore {
CookieService.getInstance().deleteCookie("leCoffreAccessToken");
CookieService.getInstance().deleteCookie("leCoffreRefreshToken");
// Clear instance variables
this.accessToken = null;
this.refreshToken = null;
this.event.emit("disconnection", this.accessToken);
} catch (error) {
console.error(error);

View File

@ -9,10 +9,46 @@ export default class UserStore {
protected readonly event = new EventEmitter();
public accessToken: string | null = null;
public refreshToken: string | null = null;
private initialized = false;
private constructor() {}
private constructor() {
// Don't initialize tokens during server-side rendering
if (typeof window !== 'undefined') {
this.initializeFromCookies();
}
}
private initializeFromCookies() {
if (this.initialized) return;
try {
const accessToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
if (accessToken) {
this.accessToken = accessToken;
}
if (refreshToken) {
this.refreshToken = refreshToken;
}
this.initialized = true;
} catch (error) {
console.warn("Failed to initialize tokens from cookies:", error);
}
}
public isConnected(): boolean {
// Ensure initialization on client side
if (typeof window !== 'undefined' && !this.initialized) {
this.initializeFromCookies();
}
// Check both instance variable and cookie to ensure consistency
if (typeof window !== 'undefined') {
const cookieToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
return !!(this.accessToken || cookieToken);
}
return !!this.accessToken;
}
@ -27,6 +63,10 @@ export default class UserStore {
CookieService.getInstance().setCookie("leCoffreAccessToken", accessToken);
CookieService.getInstance().setCookie("leCoffreRefreshToken", refreshToken);
// Update instance variables
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.event.emit("connection", this.accessToken);
} catch (error) {
console.error(error);
@ -41,6 +81,10 @@ export default class UserStore {
CookieService.getInstance().deleteCookie("leCoffreAccessToken");
CookieService.getInstance().deleteCookie("leCoffreRefreshToken");
// Clear instance variables
this.accessToken = null;
this.refreshToken = null;
this.event.emit("disconnection", this.accessToken);
} catch (error) {
console.error(error);