chore: add change password

This commit is contained in:
2026-03-01 16:32:43 +01:00
parent 7b23f82355
commit e4020c6388
8 changed files with 261 additions and 7 deletions
+16 -3
View File
@@ -17,12 +17,14 @@ interface AuthState {
role: UserRole;
isLoading: boolean;
isAuthenticated: boolean;
mustChangePassword: boolean;
}
interface AuthContextType extends AuthState {
loginClient: (token: string) => Promise<void>;
loginClient: (token: string, mustChangePassword?: boolean) => Promise<void>;
loginAdmin: (token: string, role: 'admin' | 'cabine' | 'livreur') => Promise<void>;
logout: () => Promise<void>;
setMustChangePassword: (value: boolean) => void;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
@@ -34,6 +36,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: null,
isLoading: true,
isAuthenticated: false,
mustChangePassword: false,
});
// Restore session on mount
@@ -52,6 +55,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: 'client',
isLoading: false,
isAuthenticated: true,
mustChangePassword: false,
});
return;
}
@@ -65,6 +69,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: savedRole as UserRole,
isLoading: false,
isAuthenticated: true,
mustChangePassword: false,
});
return;
}
@@ -78,6 +83,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
} catch {
await clearAllAuth();
@@ -87,6 +93,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
}
};
@@ -94,7 +101,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
restore();
}, []);
const loginClient = async (token: string) => {
const loginClient = async (token: string, mustChangePassword = false) => {
const uname = extractUsernameFromToken(token);
await storeToken(token);
if (uname) await setUsername(uname);
@@ -105,9 +112,14 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: 'client',
isLoading: false,
isAuthenticated: true,
mustChangePassword,
});
};
const setMustChangePassword = (value: boolean) => {
setState(prev => ({ ...prev, mustChangePassword: value }));
};
const loginAdmin = async (token: string, role: 'admin' | 'cabine' | 'livreur') => {
const uname = extractUsernameFromToken(token);
await storeAdminToken(token);
@@ -130,11 +142,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
role: null,
isLoading: false,
isAuthenticated: false,
mustChangePassword: false,
});
};
return (
<AuthContext.Provider value={{ ...state, loginClient, loginAdmin, logout }}>
<AuthContext.Provider value={{ ...state, loginClient, loginAdmin, logout, setMustChangePassword }}>
{children}
</AuthContext.Provider>
);