refactor: migrate betterauth to local install

also added a login page

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
2025-10-05 23:25:20 +00:00
parent f7bc5fd958
commit bf2087cded
23 changed files with 1951 additions and 2376 deletions

View File

@@ -7,6 +7,12 @@ import { createContext, useContext } from "react"
export type AuthErrorCode = keyof typeof authClient.$ERROR_CODES
export class BetterAuthError extends Error {
constructor(public readonly errorCode: AuthErrorCode) {
super(`better-auth error: ${errorCode}`)
}
}
export const authClient = createAuthClient({
baseURL: process.env.BUN_PUBLIC_CONVEX_SITE_URL,
plugins: [convexClient(), crossDomainClient()],

View File

@@ -13,7 +13,9 @@ export const Route = createRootRoute({
component: RootLayout,
})
const convexClient = new ConvexReactClient(process.env.BUN_PUBLIC_CONVEX_URL!)
const convexClient = new ConvexReactClient(process.env.BUN_PUBLIC_CONVEX_URL!, {
verbose: true,
})
const queryClient = new QueryClient({
defaultOptions: {
mutations: {

View File

@@ -20,7 +20,7 @@ export const Route = createFileRoute("/_authenticated")({
function AuthenticatedLayout() {
const { search } = useLocation()
const { isLoading } = useConvexAuth()
const { isLoading, isAuthenticated } = useConvexAuth()
const { data: session, isPending: sessionLoading } = authClient.useSession()
const [hasProcessedAuth, setHasProcessedAuth] = useState(false)
@@ -33,7 +33,7 @@ function AuthenticatedLayout() {
// Delay to ensure auth state is fully synchronized
const timer = setTimeout(() => {
setHasProcessedAuth(true)
}, 500)
}, 0)
return () => clearTimeout(timer)
}
}, [sessionLoading, isLoading])
@@ -58,7 +58,7 @@ function AuthenticatedLayout() {
<Outlet />
</Authenticated>
<Unauthenticated>
<Navigate replace to="/login" />
{/* <Navigate replace to="/login" /> */}
</Unauthenticated>
<AuthLoading>
<div className="flex h-screen w-full items-center justify-center">

View File

@@ -1,5 +1,24 @@
import { useMutation } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router"
import { Button } from "../components/ui/button"
import { GalleryVerticalEnd } from "lucide-react"
import { Button } from "@/components/ui/button"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
Field,
FieldDescription,
FieldGroup,
FieldLabel,
FieldSeparator,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { cn } from "@/lib/utils"
import { type AuthErrorCode, authClient, BetterAuthError } from "../auth"
export const Route = createFileRoute("/login")({
component: RouteComponent,
@@ -7,8 +26,159 @@ export const Route = createFileRoute("/login")({
function RouteComponent() {
return (
<div className="flex h-screen w-full items-center justify-center">
<Button onClick={() => {}}>Login</Button>
<div className="bg-background flex min-h-svh flex-col items-center justify-center gap-6 p-6 md:p-10">
<div className="flex w-full max-w-lg flex-col gap-6">
<a
href="#"
className="flex items-center gap-2 self-center font-medium text-xl"
>
<div className="bg-primary text-primary-foreground flex size-6 items-center justify-center rounded-md">
<GalleryVerticalEnd className="size-4" />
</div>
Drexa
</a>
<LoginFormCard />
</div>
</div>
)
}
function LoginFormCard({ className, ...props }: React.ComponentProps<"div">) {
return (
<div className={cn("flex flex-col gap-6", className)} {...props}>
<Card>
<CardHeader className="text-center">
<CardTitle className="text-xl">Welcome back</CardTitle>
<CardDescription>
Login with your Apple or Google account
</CardDescription>
</CardHeader>
<CardContent>
<LoginForm />
</CardContent>
</Card>
<FieldDescription className="px-6 text-center">
By clicking continue, you agree to our{" "}
<a href="#">Terms of Service</a> and{" "}
<a href="#">Privacy Policy</a>.
</FieldDescription>
</div>
)
}
function LoginForm() {
const {
mutate: signIn,
isPending,
error: signInError,
} = useMutation({
mutationFn: async ({
email,
password,
}: {
email: string
password: string
}) => {
const { data: signInData, error } = await authClient.signIn.email({
email,
password,
callbackURL: "/home",
rememberMe: true,
})
if (error) {
throw new BetterAuthError(error.code as AuthErrorCode)
}
return signInData
},
})
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
const formData = new FormData(event.currentTarget)
signIn({
email: formData.get("email") as string,
password: formData.get("password") as string,
})
}
return (
<form onSubmit={handleSubmit}>
<FieldGroup>
<Field>
<Button
disabled={isPending}
variant="outline"
type="button"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
>
<title>Apple logo</title>
<path
d="M12.152 6.896c-.948 0-2.415-1.078-3.96-1.04-2.04.027-3.91 1.183-4.961 3.014-2.117 3.675-.546 9.103 1.519 12.09 1.013 1.454 2.208 3.09 3.792 3.039 1.52-.065 2.09-.987 3.935-.987 1.831 0 2.35.987 3.96.948 1.637-.026 2.676-1.48 3.676-2.948 1.156-1.688 1.636-3.325 1.662-3.415-.039-.013-3.182-1.221-3.22-4.857-.026-3.04 2.48-4.494 2.597-4.559-1.429-2.09-3.623-2.324-4.39-2.376-2-.156-3.675 1.09-4.61 1.09zM15.53 3.83c.843-1.012 1.4-2.427 1.245-3.83-1.207.052-2.662.805-3.532 1.818-.78.896-1.454 2.338-1.273 3.714 1.338.104 2.715-.688 3.559-1.701"
fill="currentColor"
/>
</svg>
Login with Apple
</Button>
<Button
disabled={isPending}
variant="outline"
type="button"
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
>
<title>Google logo</title>
<path
d="M12.48 10.92v3.28h7.84c-.24 1.84-.853 3.187-1.787 4.133-1.147 1.147-2.933 2.4-6.053 2.4-4.827 0-8.6-3.893-8.6-8.72s3.773-8.72 8.6-8.72c2.6 0 4.507 1.027 5.907 2.347l2.307-2.307C18.747 1.44 16.133 0 12.48 0 5.867 0 .307 5.387.307 12s5.56 12 12.173 12c3.573 0 6.267-1.173 8.373-3.36 2.16-2.16 2.84-5.213 2.84-7.667 0-.76-.053-1.467-.173-2.053H12.48z"
fill="currentColor"
/>
</svg>
Login with Google
</Button>
</Field>
<FieldSeparator className="*:data-[slot=field-separator-content]:bg-card">
Or continue with
</FieldSeparator>
<Field>
<FieldLabel htmlFor="email">Email</FieldLabel>
<Input
disabled={isPending}
name="email"
type="email"
placeholder="m@example.com"
required
/>
</Field>
<Field>
<div className="flex items-center">
<FieldLabel htmlFor="password">Password</FieldLabel>
<a
href="#"
className="ml-auto text-sm underline-offset-4 hover:underline"
>
Forgot your password?
</a>
</div>
<Input
disabled={isPending}
name="password"
type="password"
required
/>
</Field>
<Field>
<Button disabled={isPending} type="submit">
{isPending ? "Logging in…" : "Login"}
</Button>
<FieldDescription className="text-center">
Don&apos;t have an account? <a href="#">Sign up</a>
</FieldDescription>
</Field>
</FieldGroup>
</form>
)
}

View File

@@ -19,7 +19,7 @@ import {
FieldLabel,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { type AuthErrorCode, authClient } from "../auth"
import { type AuthErrorCode, authClient, BetterAuthError } from "../auth"
export const Route = createFileRoute("/sign-up")({
component: SignupPage,
@@ -38,12 +38,6 @@ class PasswordMismatchError extends Error {
}
}
class BetterAuthError extends Error {
constructor(public readonly errorCode: AuthErrorCode) {
super(`better-auth error: ${errorCode}`)
}
}
function SignupPage() {
return (
<div className="bg-background flex min-h-svh flex-col items-center justify-center gap-6 p-6 md:p-10">