| 1 | "use client"; |
| 2 | |
| 3 | import type { AuthView } from "@better-auth-ui/core"; |
| 4 | import { useAuth } |
| 5 | import { type ComponentType, |
| 6 | |
| 7 | import { AuthRedirect } |
| 8 | import { AuthCallback, AuthError |
| 9 | import { ForgotPassword } |
| 10 | import type { SocialLayout |
| 11 | import { ResetLinkSent } |
| 12 | import { ResetPassword } |
| 13 | import { SignIn } |
| 14 | import { SignOut } |
| 15 | import { SignUp } |
| 16 | import { VerifyEmail } |
| 17 | |
| 18 | export type AuthProps |
| 19 | className?: string |
| 20 | path?: string |
| 21 | socialLayout?: SocialLayout |
| 22 | socialPosition?: " |
| 23 | /** @ |
| 24 | view?: AuthView |
| 25 | }; |
| 26 | |
| 27 | /** Built-in views that only make sense when email + password auth is enabled. When it's disabled, the `<Auth>` router redirects these to `signIn` so a plugin's `fallbackViews.auth.signIn` (e.g. magic link) takes over. */ |
| 28 | const PASSWORD_ONLY_VIEWS = [ |
| 29 | "signUp", |
| 30 | "forgotPassword", |
| 31 | "resetPassword", |
| 32 | "resetLinkSent", |
| 33 | ]; |
| 34 | |
| 35 | const AUTH_VIEWS: Partial |
| 36 | callback: AuthCallback, |
| 37 | error: AuthError, |
| 38 | redirect: AuthRedirect, |
| 39 | signIn: SignIn, |
| 40 | signOut: SignOut, |
| 41 | signUp: SignUp, |
| 42 | forgotPassword: ForgotPassword, |
| 43 | resetPassword: ResetPassword, |
| 44 | resetLinkSent: ResetLinkSent, |
| 45 | verifyEmail: VerifyEmail, |
| 46 | }; |
| 47 | |
| 48 | /** Render the appropriate authentication view based on the provided `view` or `path`. */ |
| 49 | export function Auth({ |
| 50 | className, |
| 51 | path, |
| 52 | socialLayout, |
| 53 | socialPosition, |
| 54 | view, |
| 55 | }: AuthProps) |
| 56 | const { basePaths, |
| 57 | useAuth(); |
| 58 | |
| 59 | if (!view && |
| 60 | throw new Error( |
| 61 | "[Better Auth UI] Either `view` or `path` must be provided", |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const authView = |
| 66 | view || |
| 67 | (Object.keys(viewPaths |
| 68 | (key) => |
| 69 | ); |
| 70 | |
| 71 | // Without email + password auth the password-only views have nothing to do, so they fall back to signIn. |
| 72 | const shouldRedirectToSignIn = |
| 73 | !emailAndPassword?.enabled && |
| 74 | authView && |
| 75 | PASSWORD_ONLY_VIEWS.includes(authView |
| 76 | |
| 77 | useEffect(() => |
| 78 | if (shouldRedirectToSignIn) { |
| 79 | navigate({ |
| 80 | to: `${basePaths. |
| 81 | replace: true, |
| 82 | }); |
| 83 | } |
| 84 | }, [shouldRedirectToSignIn, |
| 85 | |
| 86 | if (shouldRedirectToSignIn) { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | // 1. Plugin overrides (`views.auth[currentView]`) - first plugin wins, |
| 91 | // including over built-in views. Resolves the view key from `view`, |
| 92 | // then `authView` (built-in path match), then plugin-introduced paths |
| 93 | // (e.g. `magicLink` → `/auth/magic-link`). |
| 94 | for (const plugin |
| 95 | const pluginAuthPaths = |
| 96 | |
| 97 | const pluginView = |
| 98 | view ?? |
| 99 | authView ?? |
| 100 | (pluginAuthPaths && |
| 101 | Object.keys(pluginAuthPaths |
| 102 | (key) => |
| 103 | )); |
| 104 | if (!pluginView) |
| 105 | |
| 106 | const PluginView = |
| 107 | if (!PluginView) |
| 108 | |
| 109 | return ( |
| 110 | <PluginView |
| 111 | className={className} |
| 112 | socialLayout={socialLayout} |
| 113 | socialPosition={socialPosition} |
| 114 | /> |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | // 2. Plugin fallbacks - only when the built-in `signIn` isn't viable |
| 119 | // (password auth is off). Used by `magicLinkPlugin` to render the |
| 120 | // magic-link form as the primary passwordless sign-in surface. |
| 121 | if (authView === |
| 122 | const Fallback = |
| 123 | (plugin) => |
| 124 | )?.fallbackViews?.auth?. |
| 125 | |
| 126 | if (Fallback) { |
| 127 | return ( |
| 128 | <Fallback |
| 129 | className={className} |
| 130 | socialLayout={socialLayout} |
| 131 | socialPosition={socialPosition} |
| 132 | /> |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | const AuthView = |
| 138 | |
| 139 | if (!AuthView) |
| 140 | throw new Error( |
| 141 | `[Better Auth UI] Unknown view "${authView}". Valid views are: ${ |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | return ( |
| 146 | <AuthView |
| 147 | className={className} |
| 148 | socialLayout={socialLayout} |
| 149 | socialPosition={socialPosition} |
| 150 | /> |
| 151 | ); |
| 152 | } |