import { IamClient } from '../core/iam-client'; import { LocalStorage } from '../core/storage/local-storage'; import type { AuthDevice, AuthIdentity, AuthLogin, AuthRegisterRequest, AuthRegisterResponse, AuthThirdPartyLogin, UpdateProfileRequest } from '../core/types/auth.types'; import type { BaseResponse, IStorage } from '../core/types/common'; import type { IApplicaAuthProvider } from './types'; /** * ApplicaAuthProvider: thin wrapper over IamClient for React Admin compatibility. * * Maintains the same constructor signature for backward compatibility. * If apiUrl ends with '/auth', it is automatically stripped so the IamClient * can reach all service endpoints. */ class ApplicaAuthProvider implements IApplicaAuthProvider { apiUrl: string; storage: IStorage; private client: IamClient; constructor(apiUrl: string, storage: IStorage) { // Normalize: strip trailing /auth for backward compat const baseUrl = apiUrl.replace(/\/auth\/?$/, ''); this.apiUrl = apiUrl; this.storage = storage; this.client = new IamClient({ apiUrl: baseUrl, storage }); } getClient(): IamClient { return this.client; } // --- AuthProvider contract (React Admin) --- login(params: AuthLogin): Promise { return this.client.auth.login(params); } logout(): Promise { return this.client.auth.logout(); } checkAuth(): Promise { return this.client.auth.checkAuth(); } checkError(error: any): Promise { if (error.status === 401 || error.status === 403) { return Promise.reject(error?.message); } return Promise.resolve(); } getPermissions(): Promise { return this.client.auth.getPermissions(); } getIdentity(): Promise { return this.client.auth.getIdentity(); } // --- Extended methods --- getHeaders(): Promise { return this.client.auth.getHeaders(); } getToken(): Promise { return this.client.auth.getToken(); } validateToken(token: string): Promise { return this.client.auth.validateToken(token); } getRoles(): Promise { return this.client.auth.getRoles(); } impersonate(id: string | number): Promise { return this.client.auth.impersonate(id); } isImpersonating(): Promise { return this.client.auth.isImpersonating(); } stopImpersonate(): Promise { return this.client.auth.stopImpersonate(); } registerDevice(code: string): Promise { return this.client.auth.registerDevice(code); } getDevice(code: string): Promise { return this.client.auth.getDevice(code); } resetPin(userId: string, pinCode: string): Promise { return this.client.auth.resetPin(userId, pinCode); } pinLogin(deviceCode: string, pin: string): Promise { return this.client.auth.pinLogin(deviceCode, pin); } register(data: AuthRegisterRequest): Promise { return this.client.auth.register(data); } activate(activationCode: string): Promise { return this.client.auth.activate(activationCode); } changePassword(currentPassword: string, password: string): Promise { return this.client.auth.changePassword(currentPassword, password); } recover(email: string): Promise { return this.client.auth.recover(email); } updateProfile(request: UpdateProfileRequest): Promise { return this.client.auth.updateProfile(request); } thirdPartyLogin(data: AuthThirdPartyLogin): Promise { return this.client.auth.thirdPartyLogin(data); } } /** * Factory function — backward compatible. */ function createAuthProvider(config: { apiUrl: string; storage?: IStorage }): ApplicaAuthProvider { return new ApplicaAuthProvider(config.apiUrl, config.storage || new LocalStorage()); } export { ApplicaAuthProvider, createAuthProvider };