parent
c9a405a554
commit
a49cc06aa3
@ -0,0 +1,45 @@
|
|||||||
|
// AuthContext.js
|
||||||
|
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
||||||
|
import AuthService from '../services/AuthService';
|
||||||
|
|
||||||
|
interface AuthContextProps {
|
||||||
|
isLoggedIn: boolean;
|
||||||
|
login: () => void;
|
||||||
|
logout: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AuthContext = createContext<AuthContextProps | undefined>(undefined);
|
||||||
|
|
||||||
|
const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
|
||||||
|
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false);
|
||||||
|
|
||||||
|
const login = () => {
|
||||||
|
setIsLoggedIn(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const logout = async() => {
|
||||||
|
try {
|
||||||
|
await AuthService.logout();
|
||||||
|
setIsLoggedIn(false);
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AuthContext.Provider value={{ isLoggedIn, login, logout }}>
|
||||||
|
{children}
|
||||||
|
</AuthContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const useAuth = (): AuthContextProps => {
|
||||||
|
const context = useContext(AuthContext);
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('useAuth must be used within an AuthProvider');
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { AuthProvider, useAuth };
|
Binary file not shown.
@ -0,0 +1,18 @@
|
|||||||
|
export interface PlayerSoloStats {
|
||||||
|
nbGames: number;
|
||||||
|
bestScore: number;
|
||||||
|
avgNbTry: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface PlayerOnlineStats {
|
||||||
|
nbGames: number;
|
||||||
|
nbWins: number;
|
||||||
|
ratio: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface PlayerProps {
|
||||||
|
profilePicture: string;
|
||||||
|
pseudo: string;
|
||||||
|
soloStats: PlayerSoloStats;
|
||||||
|
onlineStats: PlayerOnlineStats;
|
||||||
|
};
|
Loading…
Reference in new issue