UNPKG

@saas-ui/auth-provider

Version:

Authentication provider primivites

135 lines (131 loc) 5.3 kB
import * as react_jsx_runtime from 'react/jsx-runtime'; import React from 'react'; interface UsePromise { error?: Error | null; data?: any; isLoading: boolean; isResolved: boolean; isRejected: boolean; } type AuthTypeEnum = 'magiclink' | 'password'; type AuthActionEnum = 'logIn' | 'signUp'; type AuthToken = string | null | undefined; interface AuthParams { email?: string; password?: string; provider?: string; refreshToken?: string; otp?: string; [key: string]: any; } interface ResetPasswordParams { email: string; [key: string]: any; } interface UpdatePasswordParams { password: string; token?: string; [key: string]: any; } interface OtpParams { otp: string; [key: string]: any; } type ExtraAuthOptions = Record<string, unknown>; type AuthOptions<ExtraOptions extends object = ExtraAuthOptions> = { /** * The url to redirect to after social or magic link login. */ redirectTo?: string; } & ExtraOptions; /** * The user object */ interface User { [key: string]: any; } interface DefaultUser extends User { id?: string | null; email?: string | null; } type UnsubscribeHandler = () => void; type AuthStateChangeCallback<TUser extends User = DefaultUser> = (user?: TUser | null) => void; interface AuthProviderProps<TUser extends User = DefaultUser> { /** * Refetch the user data when the window regains focus */ refetchUserOnWindowFocus?: boolean; /** * Restore the authentication state, eg after redirecting */ onRestoreAuthState?: () => Promise<void>; /** * Loads user data after authentication */ onLoadUser?: () => Promise<TUser | null>; /** * The signup method */ onSignup?: <Params extends AuthParams = AuthParams>(params: Params, options?: AuthOptions) => Promise<TUser | undefined | null>; /** * The login method */ onLogin?: <Params extends AuthParams = AuthParams>(params: Params, options?: AuthOptions) => Promise<TUser | undefined | null>; /** * Request to reset a password. */ onResetPassword?: <Params extends ResetPasswordParams = ResetPasswordParams>(params: Params, options?: AuthOptions) => Promise<any>; /** * Update the password. */ onUpdatePassword?: <Params extends UpdatePasswordParams = UpdatePasswordParams>(params: Params, options?: AuthOptions) => Promise<any>; /** * Verify an one time password (2fa) */ onVerifyOtp?: <Params extends OtpParams = OtpParams>(params: Params, options?: AuthOptions) => Promise<boolean | undefined | null>; /** * The logout method */ onLogout?: (options?: AuthOptions) => Promise<unknown>; /** * Should trigger whenever the authentication state changes */ onAuthStateChange?: (callback: AuthStateChangeCallback<TUser>) => UnsubscribeHandler; /** * Return the session token */ onGetToken?: () => Promise<AuthToken>; /** * The children to render */ children?: React.ReactNode; } type AuthFunction<TParams = AuthParams, TExtraOptions extends object = Record<string, unknown>> = (params: TParams, options?: AuthOptions<TExtraOptions>) => Promise<any>; interface AuthContextValue<TUser extends User = DefaultUser> { isAuthenticated: boolean; isLoggingIn: boolean; isLoading: boolean; user?: TUser | null; signUp: AuthFunction; logIn: AuthFunction; verifyOtp: AuthFunction<OtpParams>; resetPassword: AuthFunction<ResetPasswordParams>; updatePassword: AuthFunction<UpdatePasswordParams>; logOut: (options?: AuthOptions) => Promise<unknown>; loadUser: () => void; getToken?: () => Promise<AuthToken>; error: unknown | null; } declare const AuthContext: React.Context<AuthContextValue<DefaultUser> | null>; declare const AuthProvider: <TUser extends User = DefaultUser>({ refetchUserOnWindowFocus, onRestoreAuthState, onLoadUser, onSignup, onLogin, onVerifyOtp, onLogout, onAuthStateChange, onGetToken, onResetPassword, onUpdatePassword, children, }: AuthProviderProps<TUser>) => react_jsx_runtime.JSX.Element; declare const useAuth: <TUser extends User = DefaultUser>() => AuthContextValue<TUser>; declare const useCurrentUser: <TUser extends User = DefaultUser>() => TUser | null | undefined; interface UseLoginProps { action?: AuthActionEnum; } declare const useLogin: ({ action }?: UseLoginProps) => [UsePromise, AuthFunction<AuthParams, Record<string, unknown>>]; declare const useSignUp: () => [UsePromise, AuthFunction<AuthParams, Record<string, unknown>>]; declare const useOtp: () => [UsePromise, AuthFunction<OtpParams, Record<string, unknown>>]; declare const useResetPassword: () => [UsePromise, AuthFunction<ResetPasswordParams, Record<string, unknown>>]; declare const useUpdatePassword: () => [UsePromise, AuthFunction<UpdatePasswordParams, Record<string, unknown>>]; export { type AuthActionEnum, AuthContext, type AuthContextValue, type AuthFunction, type AuthOptions, type AuthParams, AuthProvider, type AuthProviderProps, type AuthStateChangeCallback, type AuthToken, type AuthTypeEnum, type DefaultUser, type ExtraAuthOptions, type UseLoginProps, type User, useAuth, useCurrentUser, useLogin, useOtp, useResetPassword, useSignUp, useUpdatePassword };