UNPKG

@deskree/atomic-auth-sdk

Version:

Unified Ory Kratos authentication SDK for frontend projects

188 lines (181 loc) 5.03 kB
import * as _ory_client from '@ory/client'; import { Session } from '@ory/client'; import React from 'react'; import * as next from 'next'; declare class OryAuthClient { private client; private basePath; constructor(config: { basePath: string; withCredentials?: boolean; }); getSession(): Promise<{ session: Session; error: null; } | { session: null; error: unknown; }>; refreshSession(): Promise<{ session: Session; error: null; } | { session: null; error: unknown; }>; logout(): Promise<{ logoutUrl: string; error: null; } | { logoutUrl: null; error: unknown; }>; login(returnTo?: string): Promise<{ loginUrl: string; error: null; } | { loginUrl: null; error: unknown; }>; register(returnTo?: string): Promise<{ registrationUrl: string; error: null; } | { registrationUrl: null; error: unknown; }>; createVerificationFlow(returnTo?: string): Promise<{ verificationUrl: string | undefined; error: null; flowId: string; } | { verificationUrl: null; error: unknown; flowId: null; }>; sendVerificationCode(email: string, flowId: string): Promise<{ success: boolean; error: null; flow: _ory_client.VerificationFlow; } | { success: boolean; error: unknown; flow: null; }>; verifyCode(flowId: string, code: string): Promise<{ success: boolean; error: null; flow: _ory_client.VerificationFlow; } | { success: boolean; error: unknown; flow: null; }>; getVerificationFlow(flowId: string): Promise<{ flow: _ory_client.VerificationFlow; error: null; } | { flow: null; error: unknown; }>; isIdentityVerified(session: Session): boolean; } interface SessionUser { id: string; email: string; verified: boolean; name?: { first?: string; last?: string; }; } interface AuthState { isLoading: boolean; isAuthenticated: boolean; user: SessionUser | null; session: Session | null; error: Error | null; isVerified: boolean; } interface AuthContextType extends AuthState { login: (returnTo?: string) => Promise<void>; logout: () => Promise<void>; refresh: () => Promise<void>; refreshSession: () => Promise<boolean>; startVerification: (options?: { returnTo?: string; }) => Promise<{ verificationUrl: string | null; error: Error | null; flowId: string | null; }>; sendVerificationCode: (email: string, flowId: string) => Promise<{ success: boolean; error: Error | null; flow: any; }>; verifyCode: (flowId: string, code: string) => Promise<{ success: boolean; error: Error | null; flow: any; }>; getVerificationFlow: (flowId: string) => Promise<{ flow: any; error: Error | null; }>; checkVerification: () => Promise<boolean>; } interface ClientAuthProviderProps { children: React.ReactNode; basePath?: string; } declare function AuthProvider({ children, basePath, }: ClientAuthProviderProps): React.JSX.Element; declare function useAuth(): AuthContextType; interface OryAuthConfig { oryUrl: string; loginPath?: string; storageKey?: string; autoRefresh?: boolean; refreshThreshold?: number; } declare function createOryAuth(config: OryAuthConfig): { AuthProvider: ({ children }: { children: React.ReactNode; }) => React.JSX.Element; useAuth: typeof useAuth; refreshSession: () => Promise<{ session: _ory_client.Session | null; error: unknown; }>; sendVerificationCode: (email: string, flowId: string) => Promise<{ success: boolean; error: null; flow: _ory_client.VerificationFlow; } | { success: boolean; error: unknown; flow: null; }>; verifyCode: (flowId: string, code: string) => Promise<{ success: boolean; error: null; flow: _ory_client.VerificationFlow; } | { success: boolean; error: unknown; flow: null; }>; getVerificationFlow: (flowId: string) => Promise<{ flow: _ory_client.VerificationFlow; error: null; } | { flow: null; error: unknown; }>; withAuth: (Component: any, options?: {}) => { (props: object): string | number | bigint | true | Iterable<React.ReactNode> | Promise<React.AwaitedReactNode> | React.JSX.Element | null; displayName: string; }; createAuthApi: (handler: any) => (req: next.NextApiRequest, res: next.NextApiResponse) => Promise<void>; }; export { type AuthContextType, AuthProvider, OryAuthClient, type OryAuthConfig, type SessionUser, createOryAuth, useAuth };