refactor: migrate authentication to NextAuth and clean up related services
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { signIn, signOut } from "next-auth/react";
|
||||
import { BaseHttpClient } from "../base/http-client";
|
||||
import { UserProfile } from "../../lib/types";
|
||||
|
||||
export interface LoginCredentials {
|
||||
email: string;
|
||||
@@ -24,27 +24,54 @@ export interface AuthUser {
|
||||
|
||||
export class AuthClient extends BaseHttpClient {
|
||||
/**
|
||||
* Connecte un utilisateur avec email/password
|
||||
* Connecte un utilisateur avec email/password via NextAuth
|
||||
*/
|
||||
async login(
|
||||
credentials: LoginCredentials
|
||||
): Promise<{ user: AuthUser; message: string }> {
|
||||
return await this.post("/auth/login", credentials);
|
||||
async login(credentials: LoginCredentials): Promise<{ success: boolean; error?: string }> {
|
||||
const result = await signIn("credentials", {
|
||||
email: credentials.email,
|
||||
password: credentials.password,
|
||||
redirect: false,
|
||||
});
|
||||
|
||||
if (result?.error) {
|
||||
return { success: false, error: "Email ou mot de passe incorrect" };
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Crée un nouveau compte utilisateur
|
||||
* Crée un nouveau compte utilisateur puis se connecte automatiquement
|
||||
*/
|
||||
async register(
|
||||
data: RegisterData
|
||||
): Promise<{ user: AuthUser; message: string }> {
|
||||
return await this.post("/auth/register", data);
|
||||
async register(data: RegisterData): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
// Créer l'utilisateur via l'API
|
||||
const response = await this.post<{ user: AuthUser; message: string }>("/auth/register", data);
|
||||
|
||||
// Se connecter automatiquement après création
|
||||
const loginResult = await signIn("credentials", {
|
||||
email: data.email,
|
||||
password: data.password,
|
||||
redirect: false,
|
||||
});
|
||||
|
||||
if (loginResult?.error) {
|
||||
return { success: false, error: "Compte créé mais erreur de connexion" };
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message || "Erreur lors de la création du compte"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Déconnecte l'utilisateur
|
||||
* Déconnecte l'utilisateur via NextAuth
|
||||
*/
|
||||
async logout(): Promise<{ message: string }> {
|
||||
return await this.post("/auth/logout");
|
||||
async logout(): Promise<void> {
|
||||
await signOut({ redirect: false });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user