UseBetterAuth
guarahooks/use-better-authFreeHook
Flexible authentication hook.
Install it
npx shadcn@latest add https://guarahooks.com/r/use-better-auth.jsonSource
1'use client';23import { useCallback, useEffect, useState } from 'react';45export interface StorageLike {6 getItem(key: string): string | null;7 setItem(key: string, value: string): void;8 removeItem(key: string): void;9}1011export interface BetterAuthResponse<TUser> {12 token: string;13 user: TUser;14}1516export interface DefaultCredentials17 extends Record<string, string | number | boolean> {18 email: string;19 password: string;20}2122export interface UseBetterAuthReturn<23 TUser,24 TCredentials extends Record<string, string | number | boolean>,25> {26 token: string | null;27 user: TUser | null;28 loading: boolean;29 error: Error | null;30 isAuthenticated: boolean;31 login: (credentials: TCredentials) => Promise<void>;32 logout: () => void;33 refresh: () => Promise<void>;34 updateUser: (user: TUser, token?: string) => void;35 clearError: () => void;36}3738export interface UseBetterAuthOptions<39 TUser,40 TCredentials extends Record<string, string | number | boolean>,41> {42 /** Storage key prefix for storing authentication data. Defaults to 'better-auth' */43 storageKey?: string;44 /** Custom storage implementation. Defaults to localStorage */45 storage?: StorageLike;46 /** Login endpoint URL. Defaults to '/api/login' */47 loginUrl?: string;48 /** Token refresh endpoint URL */49 refreshUrl?: string;50 /** Logout endpoint URL */51 logoutUrl?: string;52 /** Auto-refresh interval in milliseconds */53 refreshInterval?: number;54 /** Callback fired when authentication state changes */55 onAuthChange?: (token: string | null, user: TUser | null) => void;56 /** Custom fetcher function for authentication requests */57 fetcher?: (58 credentials: TCredentials,59 url: string,60 ) => Promise<BetterAuthResponse<TUser>>;61}6263/**64 * A flexible authentication hook that provides login, logout, token refresh, and user management.65 *66 * Features:67 * - Customizable storage (localStorage, sessionStorage, or custom implementation)68 * - Automatic token refresh with configurable intervals69 * - SSR-safe with proper hydration handling70 * - Type-safe with generic user and credentials types71 * - Custom fetcher support for different API implementations72 * - Cross-tab synchronization through storage events73 * - Error handling with clear error states74 *75 * @example76 * ```tsx77 * // Basic usage78 * const auth = useBetterAuth<User>({79 * loginUrl: '/api/auth/login',80 * refreshUrl: '/api/auth/refresh',81 * });82 *83 * // Login84// … truncated
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControlleruse-abort-controller | guarahooks | Hooks | Free | no deps | |
| UseArrayStateuse-array-state | guarahooks | Hooks | Free | no deps | |
| UseAxiosuse-axios | guarahooks | Hooks | Free | no deps | |
| UseBatteryStatususe-battery-status | guarahooks | Hooks | Free | no deps | |
| UseClickOutsideuse-click-outside | guarahooks | Hooks | Free | no deps | |
| UseConfirmuse-confirm | guarahooks | Hooks | Free | no deps | |
| UseCookieuse-cookie | guarahooks | Hooks | Free | no deps | |
| UseCopyToClipboarduse-copy-to-clipboard | guarahooks | Hooks | Free | no deps |
