UNPKG

@narangcia-oss/cryptic-auth-client-react

Version:

React hooks and components for cryptic-auth authentication with seamless OAuth2 integration

258 lines (247 loc) 7.43 kB
import * as react_jsx_runtime from 'react/jsx-runtime'; import React, { ReactNode } from 'react'; import * as _narangcia_oss_cryptic_auth_client_plain_ts from '@narangcia-oss/cryptic-auth-client-plain-ts'; import { AuthConfig, AuthUser, AuthTokens, UserCredentials } from '@narangcia-oss/cryptic-auth-client-plain-ts'; export { AuthClient, AuthConfig, AuthTokens, AuthUser, LoginResponse, OAuthAuthResponse, OAuthCallbackParams, OAuthSignupResponse, SignupResponse, TokenValidationResponse, UserCredentials } from '@narangcia-oss/cryptic-auth-client-plain-ts'; interface AuthState { isAuthenticated: boolean; isLoading: boolean; user: AuthUser | null; tokens: AuthTokens | null; error: string | null; } interface AuthContextValue extends AuthState { login: (credentials: UserCredentials) => Promise<void>; signup: (credentials: UserCredentials) => Promise<void>; logout: () => void; oauthLogin: (provider: string, scopes?: string[]) => Promise<void>; refreshToken: () => Promise<void>; clearError: () => void; } interface AuthProviderProps { children: ReactNode; config: AuthConfig; /** * Whether to automatically handle OAuth callbacks on mount * @default true */ autoHandleOAuthCallback?: boolean; /** * Custom error handler for authentication errors */ onError?: (error: Error) => void; /** * Custom success handler for authentication success */ onAuthSuccess?: (user: AuthUser, tokens: AuthTokens) => void; } declare function AuthProvider({ children, config, autoHandleOAuthCallback, onError, onAuthSuccess, }: AuthProviderProps): react_jsx_runtime.JSX.Element; declare function useAuth(): AuthContextValue; /** * Hook for handling OAuth authentication flows */ declare function useOAuth(): { /** * Initiate OAuth login with a provider * @param provider OAuth provider name (e.g., 'google', 'github', 'discord') * @param scopes Optional array of OAuth scopes to request */ login: (provider: string, scopes?: string[]) => Promise<void>; isLoading: boolean; error: string | null; }; /** * Hook for handling traditional username/password authentication */ declare function useCredentialAuth(): { /** * Login with username and password */ login: (credentials: _narangcia_oss_cryptic_auth_client_plain_ts.UserCredentials) => Promise<void>; /** * Sign up with username and password */ signup: (credentials: _narangcia_oss_cryptic_auth_client_plain_ts.UserCredentials) => Promise<void>; isLoading: boolean; error: string | null; }; /** * Hook for accessing current user information and authentication state */ declare function useUser(): { /** * Current authenticated user, null if not authenticated */ user: _narangcia_oss_cryptic_auth_client_plain_ts.AuthUser | null; /** * Whether the user is currently authenticated */ isAuthenticated: boolean; /** * Whether authentication state is currently being determined */ isLoading: boolean; /** * User's display name (fallback to identifier if no name available) */ displayName: string | null; /** * User's email (from OAuth info if available) */ email: string | null; /** * OAuth provider used for authentication (if any) */ oauthProvider: string | null; }; interface UseAuthGuardOptions { /** * Redirect URL when user is not authenticated * @default '/login' */ redirectTo?: string; /** * Whether to redirect immediately or just return loading state * @default true */ redirect?: boolean; /** * Custom redirect function */ onUnauthenticated?: () => void; } /** * Hook for protecting routes that require authentication */ declare function useAuthGuard(options?: UseAuthGuardOptions): { isAuthenticated: boolean; isLoading: boolean; /** * Whether the current route should be accessible */ canAccess: boolean; /** * Whether we're still checking authentication status */ isChecking: boolean; }; interface AuthGuardProps { /** * Content to render when user is authenticated */ children: ReactNode; /** * Content to render when user is not authenticated */ fallback?: ReactNode; /** * Content to render while checking authentication status */ loading?: ReactNode; /** * Whether to redirect to login page when not authenticated * @default false */ redirect?: boolean; /** * URL to redirect to when not authenticated * @default '/login' */ redirectTo?: string; } /** * Component that conditionally renders content based on authentication status */ declare function AuthGuard({ children, fallback, loading, redirect, redirectTo, }: AuthGuardProps): react_jsx_runtime.JSX.Element | null; interface OAuthButtonProps { /** * OAuth provider name (e.g., 'google', 'github', 'discord') */ provider: string; /** * OAuth scopes to request * @default ['profile', 'email'] */ scopes?: string[]; /** * Button content - can be text or custom JSX */ children?: ReactNode; /** * Additional CSS classes */ className?: string; /** * Custom button styles */ style?: React.CSSProperties; /** * Whether the button is disabled */ disabled?: boolean; /** * Callback called before initiating OAuth flow */ onBeforeLogin?: () => void; } /** * Pre-built OAuth login button component */ declare function OAuthButton({ provider, scopes, children, className, style, disabled, onBeforeLogin, }: OAuthButtonProps): react_jsx_runtime.JSX.Element; interface LoginFormProps { /** * Callback called on successful login */ onSuccess?: () => void; /** * Callback called on login error */ onError?: (error: string) => void; /** * Custom CSS classes for the form */ className?: string; /** * Custom styles for the form */ style?: React.CSSProperties; /** * Whether to show the signup link * @default true */ showSignupLink?: boolean; /** * Custom submit button text * @default 'Login' */ submitText?: string; } /** * Pre-built login form component */ declare function LoginForm({ onSuccess, onError, className, style, showSignupLink, submitText, }: LoginFormProps): react_jsx_runtime.JSX.Element; /** * Utility functions for OAuth callback handling in React applications */ /** * Check if the current URL is an OAuth callback */ declare function isOAuthCallbackURL(url: string): boolean; /** * Check if the current URL has OAuth error parameters */ declare function hasOAuthError(): boolean; /** * Get OAuth error details from URL */ declare function getOAuthError(): { error: string | null; error_description: string | null; error_uri: string | null; }; /** * Clean OAuth parameters from the current URL without triggering a page reload */ declare function cleanOAuthURL(): void; export { AuthGuard, AuthProvider, LoginForm, OAuthButton, cleanOAuthURL, getOAuthError, hasOAuthError, isOAuthCallbackURL, useAuth, useAuthGuard, useCredentialAuth, useOAuth, useUser }; export type { AuthContextValue, AuthProviderProps, AuthState };