UseNextAuth
guarahooks/use-next-authFreeHook
Enhanced wrapper for NextAuth.js session management.
Install it
npx shadcn@latest add https://guarahooks.com/r/use-next-auth.jsonSource
1'use client';23import { useCallback, useEffect, useRef } from 'react';45import type { Session } from 'next-auth';6import { signIn, signOut, useSession } from 'next-auth/react';78export interface UseNextAuthReturn<T extends Session = Session> {9 session: T | null;10 status: 'authenticated' | 'unauthenticated' | 'loading';11 isAuthenticated: boolean;12 signIn: typeof signIn;13 signOut: typeof signOut;14 refresh: () => Promise<void>;15}1617export interface UseNextAuthOptions<T extends Session = Session> {18 onSessionChange?: (19 session: T | null,20 status: 'authenticated' | 'unauthenticated' | 'loading',21 ) => void;22 /** Interval in milliseconds to refresh the session automatically */23 refreshInterval?: number;24 onError?: (error: Error) => void;25}2627/**28 * Enhanced hook for NextAuth.js session management with additional features29 *30 * @template T - Custom session interface extending NextAuth Session31 * @param options - Configuration options for the hook32 * @returns Enhanced session object with additional utilities33 */34export function useNextAuth<T extends Session = Session>(35 options: UseNextAuthOptions<T> = {},36): UseNextAuthReturn<T> {37 const { onSessionChange, refreshInterval, onError } = options;38 const { data, status, update } = useSession();39 const previousRef = useRef<T | null>(null);4041 const safeSignIn = useCallback<typeof signIn>(42 async (...args) => {43 try {44 return await signIn(...args);45 } catch (err) {46 onError?.(err as Error);47 throw err;48 }49 },50 [onError],51 );5253 const safeSignOut = useCallback<typeof signOut>(54 async (...args) => {55 try {56 return await signOut(...args);57 } catch (err) {58 onError?.(err as Error);59 throw err;60 }61 },62 [onError],63 );6465 const refresh = useCallback(async () => {66 await update();67 }, [update]);6869 // Auto-refresh session at specified intervals70 useEffect(() => {71 if (!refreshInterval) return;72 const id = setInterval(refresh, refreshInterval);73 return () => clearInterval(id);74 }, [refreshInterval, refresh]);7576 // Session change detection and callbacks77 useEffect(() => {78 if (onSessionChange && previousRef.current !== data) {79 onSessionChange(data as T | null, status);80 previousRef.current = data as T | null;81 }82 }, [data, status, onSessionChange]);8384 // Debug logging in development mode85 useEffect(() => {86 if (process.env.NODE_ENV === 'development') {87// … 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 | |
| UseBetterAuthuse-better-auth | 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 |
