chore: build
Frontend Client - EAS Build / build (push) Has been cancelled

This commit is contained in:
2026-06-20 08:16:25 +02:00
parent 4f6d26c14c
commit f791801956
+172 -137
View File
@@ -1,163 +1,198 @@
import React, { createContext, useContext, useState, useEffect, type ReactNode } from 'react'; import React, {
createContext,
useContext,
useState,
useEffect,
type ReactNode,
} from "react";
import { import {
getToken, setToken as storeToken, removeToken, getToken,
getAdminToken, setAdminToken as storeAdminToken, removeAdminToken, setToken as storeToken,
setUsername, removeUsername, removeToken,
setAdminUsername, removeAdminUsername, getAdminToken,
setRole as storeRole, getRole, removeRole, setAdminToken as storeAdminToken,
clearAllAuth, removeAdminToken,
} from './tokenStorage'; setUsername,
import { extractUsernameFromToken, extractRoleFromToken, isTokenExpired } from './jwtUtils'; removeUsername,
setAdminUsername,
removeAdminUsername,
setRole as storeRole,
getRole,
removeRole,
clearAllAuth,
} from "./tokenStorage";
import {
extractUsernameFromToken,
extractRoleFromToken,
isTokenExpired,
} from "./jwtUtils";
export type UserRole = 'client' | 'admin' | 'cabine' | 'livreur' | null; export type UserRole = "client" | "admin" | "cabine" | "livreur" | null;
interface AuthState { interface AuthState {
token: string | null; token: string | null;
username: string | null; username: string | null;
role: UserRole; role: UserRole;
isLoading: boolean; isLoading: boolean;
isAuthenticated: boolean; isAuthenticated: boolean;
mustChangePassword: boolean; mustChangePassword: boolean;
} }
interface AuthContextType extends AuthState { interface AuthContextType extends AuthState {
loginClient: (token: string, mustChangePassword?: boolean) => Promise<void>; loginClient: (token: string, mustChangePassword?: boolean) => Promise<void>;
loginAdmin: (token: string, role: 'admin' | 'cabine' | 'livreur') => Promise<void>; loginAdmin: (
logout: () => Promise<void>; token: string,
setMustChangePassword: (value: boolean) => void; role: "admin" | "cabine" | "livreur",
) => Promise<void>;
logout: () => Promise<void>;
setMustChangePassword: (value: boolean) => void;
} }
const AuthContext = createContext<AuthContextType | undefined>(undefined); const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: ReactNode }) { export function AuthProvider({ children }: { children: ReactNode }) {
const [state, setState] = useState<AuthState>({ const [state, setState] = useState<AuthState>({
token: null, token: null,
username: null, username: null,
role: null, role: null,
isLoading: true, isLoading: true,
isAuthenticated: false, isAuthenticated: false,
mustChangePassword: false, mustChangePassword: false,
}); });
// Restore session on mount useEffect(() => {
useEffect(() => { const restore = async () => {
const restore = async () => { try {
try { const savedRole = await getRole();
const savedRole = await getRole();
if (savedRole === 'client') { if (savedRole === "client") {
const token = await getToken(); const token = await getToken();
if (token && !isTokenExpired(token)) { if (token && !isTokenExpired(token)) {
const uname = extractUsernameFromToken(token); const uname = extractUsernameFromToken(token);
setState({ setState({
token, token,
username: uname, username: uname,
role: 'client', role: "client",
isLoading: false, isLoading: false,
isAuthenticated: true, isAuthenticated: true,
mustChangePassword: false, mustChangePassword: false,
}); });
return; return;
} }
} else if (savedRole === 'admin' || savedRole === 'cabine' || savedRole === 'livreur') { } else if (
const token = await getAdminToken(); savedRole === "admin" ||
if (token && !isTokenExpired(token)) { savedRole === "cabine" ||
const uname = extractUsernameFromToken(token); savedRole === "livreur"
setState({ ) {
token, const token = await getAdminToken();
username: uname, if (token && !isTokenExpired(token)) {
role: savedRole as UserRole, const uname = extractUsernameFromToken(token);
isLoading: false, setState({
isAuthenticated: true, token,
mustChangePassword: false, username: uname,
}); role: savedRole as UserRole,
return; isLoading: false,
} isAuthenticated: true,
} mustChangePassword: false,
});
return;
}
}
// No valid session // No valid session
await clearAllAuth(); await clearAllAuth();
setState({
token: null,
username: null,
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
} catch {
await clearAllAuth();
setState({
token: null,
username: null,
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
}
};
restore();
}, []);
const loginClient = async (token: string, mustChangePassword = false) => {
const uname = extractUsernameFromToken(token);
await storeToken(token);
if (uname) await setUsername(uname);
await storeRole("client");
setState({ setState({
token: null, token,
username: null, username: uname,
role: null, role: "client",
isLoading: false, isLoading: false,
isAuthenticated: false, isAuthenticated: true,
mustChangePassword: false, mustChangePassword,
}); });
} catch {
await clearAllAuth();
setState({
token: null,
username: null,
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
}
}; };
restore(); const setMustChangePassword = (value: boolean) => {
}, []); setState((prev) => ({ ...prev, mustChangePassword: value }));
};
const loginClient = async (token: string, mustChangePassword = false) => { const loginAdmin = async (
const uname = extractUsernameFromToken(token); token: string,
await storeToken(token); role: "admin" | "cabine" | "livreur",
if (uname) await setUsername(uname); ) => {
await storeRole('client'); const uname = extractUsernameFromToken(token);
setState({ await storeAdminToken(token);
token, if (uname) await setAdminUsername(uname);
username: uname, await storeRole(role);
role: 'client', setState({
isLoading: false, token,
isAuthenticated: true, username: uname,
mustChangePassword, role,
}); isLoading: false,
}; isAuthenticated: true,
mustChangePassword: false,
});
};
const setMustChangePassword = (value: boolean) => { const logout = async () => {
setState(prev => ({ ...prev, mustChangePassword: value })); await clearAllAuth();
}; setState({
token: null,
username: null,
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
};
const loginAdmin = async (token: string, role: 'admin' | 'cabine' | 'livreur') => { return (
const uname = extractUsernameFromToken(token); <AuthContext.Provider
await storeAdminToken(token); value={{
if (uname) await setAdminUsername(uname); ...state,
await storeRole(role); loginClient,
setState({ loginAdmin,
token, logout,
username: uname, setMustChangePassword,
role, }}
isLoading: false, >
isAuthenticated: true, {children}
mustChangePassword: false, </AuthContext.Provider>
}); );
};
const logout = async () => {
await clearAllAuth();
setState({
token: null,
username: null,
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
};
return (
<AuthContext.Provider value={{ ...state, loginClient, loginAdmin, logout, setMustChangePassword }}>
{children}
</AuthContext.Provider>
);
} }
export function useAuth() { export function useAuth() {
const context = useContext(AuthContext); const context = useContext(AuthContext);
if (!context) { if (!context) {
throw new Error('useAuth must be used within an AuthProvider'); throw new Error("useAuth must be used within an AuthProvider");
} }
return context; return context;
} }