@pagamio/frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
66 lines (65 loc) • 2.07 kB
TypeScript
import React, { type ReactNode } from 'react';
import { type AuthService } from '../services';
import type { AuthResponse, CustomAuthConfig } from '../types';
/**
* Base auth configuration interface that can be extended
*/
export interface AppAuthConfig extends CustomAuthConfig {
UserInfo: {
id: string | number;
userName: string;
roles: string[];
[key: string]: unknown;
};
TokenInfo: {
token: string;
expiresIn: number;
};
Credentials: {
username: string;
password: string;
};
Error: {
code: string;
message: string;
};
}
/**
* Authentication context state with generic type parameter
*/
type AuthContextState<T extends AppAuthConfig> = {
user: T['UserInfo'] | null;
isAuthenticated: boolean;
isLoading: boolean;
error: T['Error'] | null;
};
/**
* Authentication context actions with generic type parameter
*/
interface AuthContextActions<T extends AppAuthConfig> {
login: (response: T['Credentials'], rememberMe?: boolean) => Promise<AuthResponse<T>>;
logout: () => Promise<void>;
updateUser: (userData: Partial<T['UserInfo']>) => void;
clearError: () => void;
}
type AuthContextValue<T extends AppAuthConfig> = AuthContextState<T> & AuthContextActions<T> & {
authService: AuthService<T>;
};
/**
* Authentication context with generic type parameter
*/
export declare const AuthContext: React.Context<AuthContextValue<AppAuthConfig>>;
interface AuthProviderProps<T extends AppAuthConfig> {
children: ReactNode;
authService: AuthService<T>;
validateOnInit?: boolean;
}
/**
* Authentication context provider component with generic type parameter
*/
export declare function AuthProvider<T extends AppAuthConfig>({ children, authService, validateOnInit, }: Readonly<AuthProviderProps<T>>): import("react/jsx-runtime").JSX.Element;
/**
* Custom hook for using auth context with generic type parameter
*/
export declare function useAuth<T extends AppAuthConfig>(): AuthContextValue<T>;
export {};