refactor: apply consistent formatting to improve code quality
Ensure uniform code formatting across components by aligning with the established code style. Adjust imports, indentation, and spacing to enhance readability and maintainability.
This commit is contained in:
@@ -1,99 +1,132 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { UserService } from "@/services/user.service";
|
||||
import { AuthService } from "@/services/auth.service";
|
||||
import type { User } from "@/types/user";
|
||||
import { useRouter } from "next/navigation";
|
||||
import * as React from "react";
|
||||
import { toast } from "sonner";
|
||||
import { AuthService } from "@/services/auth.service";
|
||||
import { UserService } from "@/services/user.service";
|
||||
import type { RegisterPayload } from "@/types/auth";
|
||||
import type { User } from "@/types/user";
|
||||
|
||||
interface AuthContextType {
|
||||
user: User | null;
|
||||
isLoading: boolean;
|
||||
isAuthenticated: boolean;
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
register: (payload: any) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
refreshUser: () => Promise<void>;
|
||||
user: User | null;
|
||||
isLoading: boolean;
|
||||
isAuthenticated: boolean;
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
register: (payload: RegisterPayload) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
refreshUser: () => Promise<void>;
|
||||
}
|
||||
|
||||
const AuthContext = React.createContext<AuthContextType | null>(null);
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [user, setUser] = React.useState<User | null>(null);
|
||||
const [isLoading, setIsLoading] = React.useState(true);
|
||||
const router = useRouter();
|
||||
const [user, setUser] = React.useState<User | null>(null);
|
||||
const [isLoading, setIsLoading] = React.useState(true);
|
||||
const router = useRouter();
|
||||
|
||||
const refreshUser = React.useCallback(async () => {
|
||||
try {
|
||||
const userData = await UserService.getMe();
|
||||
setUser(userData);
|
||||
} catch (error) {
|
||||
setUser(null);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
const refreshUser = React.useCallback(async () => {
|
||||
try {
|
||||
const userData = await UserService.getMe();
|
||||
setUser(userData);
|
||||
} catch (_error) {
|
||||
setUser(null);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
refreshUser();
|
||||
}, [refreshUser]);
|
||||
React.useEffect(() => {
|
||||
refreshUser();
|
||||
}, [refreshUser]);
|
||||
|
||||
const login = async (email: string, password: string) => {
|
||||
try {
|
||||
await AuthService.login(email, password);
|
||||
await refreshUser();
|
||||
toast.success("Connexion réussie !");
|
||||
router.push("/");
|
||||
} catch (error: any) {
|
||||
toast.error(error.response?.data?.message || "Erreur de connexion");
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
const login = async (email: string, password: string) => {
|
||||
try {
|
||||
await AuthService.login(email, password);
|
||||
await refreshUser();
|
||||
toast.success("Connexion réussie !");
|
||||
router.push("/");
|
||||
} catch (error: unknown) {
|
||||
let errorMessage = "Erreur de connexion";
|
||||
if (
|
||||
error &&
|
||||
typeof error === "object" &&
|
||||
"response" in error &&
|
||||
error.response &&
|
||||
typeof error.response === "object" &&
|
||||
"data" in error.response &&
|
||||
error.response.data &&
|
||||
typeof error.response.data === "object" &&
|
||||
"message" in error.response.data &&
|
||||
typeof error.response.data.message === "string"
|
||||
) {
|
||||
errorMessage = error.response.data.message;
|
||||
}
|
||||
toast.error(errorMessage);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const register = async (payload: any) => {
|
||||
try {
|
||||
await AuthService.register(payload);
|
||||
toast.success("Inscription réussie ! Vous pouvez maintenant vous connecter.");
|
||||
router.push("/login");
|
||||
} catch (error: any) {
|
||||
toast.error(error.response?.data?.message || "Erreur d'inscription");
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
const register = async (payload: RegisterPayload) => {
|
||||
try {
|
||||
await AuthService.register(payload);
|
||||
toast.success(
|
||||
"Inscription réussie ! Vous pouvez maintenant vous connecter.",
|
||||
);
|
||||
router.push("/login");
|
||||
} catch (error: unknown) {
|
||||
let errorMessage = "Erreur d'inscription";
|
||||
if (
|
||||
error &&
|
||||
typeof error === "object" &&
|
||||
"response" in error &&
|
||||
error.response &&
|
||||
typeof error.response === "object" &&
|
||||
"data" in error.response &&
|
||||
error.response.data &&
|
||||
typeof error.response.data === "object" &&
|
||||
"message" in error.response.data &&
|
||||
typeof error.response.data.message === "string"
|
||||
) {
|
||||
errorMessage = error.response.data.message;
|
||||
}
|
||||
toast.error(errorMessage);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
try {
|
||||
await AuthService.logout();
|
||||
setUser(null);
|
||||
toast.success("Déconnexion réussie");
|
||||
router.push("/");
|
||||
} catch (error) {
|
||||
toast.error("Erreur lors de la déconnexion");
|
||||
}
|
||||
};
|
||||
const logout = async () => {
|
||||
try {
|
||||
await AuthService.logout();
|
||||
setUser(null);
|
||||
toast.success("Déconnexion réussie");
|
||||
router.push("/");
|
||||
} catch (_error) {
|
||||
toast.error("Erreur lors de la déconnexion");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
user,
|
||||
isLoading,
|
||||
isAuthenticated: !!user,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
refreshUser,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
user,
|
||||
isLoading,
|
||||
isAuthenticated: !!user,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
refreshUser,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
const context = React.useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
return context;
|
||||
const context = React.useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user