Auth Redirect
better-auth-ui/auth-redirectFreeComponent
A session-aware redirect view that safely continues authenticated users to a same-origin target or sends them through sign in first.
Live preview
live · rendered by the registry
Rendered by better-auth-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://better-auth-ui.com/r/auth-redirect.jsonSource
1"use client"23import { getAuthRedirectAction } from "@better-auth-ui/core"4import { useAuth, useSession } from "@better-auth-ui/react"5import { useEffect, useRef } from "react"6import { Spinner } from "@/components/ui/spinner"7import { cn } from "@/lib/utils"89export type AuthRedirectProps = {10 className?: string11}1213/**14 * Redirects authenticated users to a validated same-origin target.15 *16 * Signed-out users are sent through the sign-in view first. The redirect view17 * is preserved as the post-authentication destination so API callbacks receive18 * a full-page request after the session is established.19 *20 * @param className - Optional additional class names applied to the spinner21 * @returns The centered spinner shown while the session and redirect resolve22 */23export function AuthRedirect({ className }: AuthRedirectProps) {24 const { authClient, basePaths, viewPaths } = useAuth()25 const { data: session, isPending } = useSession(authClient)26 const hasRedirected = useRef(false)2728 useEffect(() => {29 if (isPending || hasRedirected.current) return30 hasRedirected.current = true3132 const action = getAuthRedirectAction(33 new URL(window.location.href),34 Boolean(session),35 `${basePaths.auth}/${viewPaths.auth.signIn}`36 )3738 window.location.replace(action.to)39 }, [basePaths.auth, isPending, session, viewPaths.auth.signIn])4041 return <Spinner className={cn("mx-auto my-auto", className)} />42}
What it pulls in
npm packages
Other registry items
Files it writes
More from better-auth-ui
All 49 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Account Settingsaccount-settings | better-auth-ui | Components | Free | 2 deps | |
| Active Sessionsactive-sessions | better-auth-ui | Components | Free | 5 deps · 2 files | |
| Additional Fieldadditional-field | better-auth-ui | Components | Free | 5 deps | |
| Adminadmin | better-auth-ui | Components | Free | 4 deps · 3 files | |
| Anonymousanonymous | better-auth-ui | Components | Free | 4 deps · 3 files | |
| API Keyapi-key | better-auth-ui | Components | Free | 4 deps · 10 files | |
| Authauth | better-auth-ui | Components | Free | 2 deps | |
| Auth Providerauth-provider | better-auth-ui | Components | Free | 4 deps · 2 files |
