167 lines
4.5 KiB
TypeScript
167 lines
4.5 KiB
TypeScript
"use client"
|
|
|
|
import { useState } from "react"
|
|
import { useRouter } from "next/navigation"
|
|
import Link from "next/link"
|
|
import { useAuthActions } from "@convex-dev/auth/react"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
Card,
|
|
CardContent,
|
|
CardDescription,
|
|
CardHeader,
|
|
CardTitle,
|
|
} from "@/components/ui/card"
|
|
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"
|
|
import { Input } from "@/components/ui/input"
|
|
import { Spinner } from "@/components/ui/spinner"
|
|
|
|
type AuthFlow = "signIn" | "signUp"
|
|
|
|
const copy: Record<
|
|
AuthFlow,
|
|
{
|
|
title: string
|
|
description: string
|
|
submit: string
|
|
switchPrompt: string
|
|
switchLabel: string
|
|
switchHref: string
|
|
}
|
|
> = {
|
|
signIn: {
|
|
title: "Welcome back",
|
|
description: "Sign in to your oTunes account to keep listening.",
|
|
submit: "Sign in",
|
|
switchPrompt: "Don't have an account?",
|
|
switchLabel: "Sign up",
|
|
switchHref: "/signup",
|
|
},
|
|
signUp: {
|
|
title: "Create your account",
|
|
description: "Sign up to start building your oTunes library.",
|
|
submit: "Sign up",
|
|
switchPrompt: "Already have an account?",
|
|
switchLabel: "Sign in",
|
|
switchHref: "/signin",
|
|
},
|
|
}
|
|
|
|
export function AuthForm({
|
|
flow,
|
|
accountCreationDisabled,
|
|
}: {
|
|
flow: AuthFlow
|
|
accountCreationDisabled: boolean
|
|
}) {
|
|
const { signIn } = useAuthActions()
|
|
const router = useRouter()
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [submitting, setSubmitting] = useState(false)
|
|
|
|
const text = copy[flow]
|
|
const signupsDisabled = flow === "signUp" && accountCreationDisabled
|
|
const hideSignUpLink = flow === "signIn" && accountCreationDisabled
|
|
|
|
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
event.preventDefault()
|
|
if (signupsDisabled) {
|
|
return
|
|
}
|
|
setError(null)
|
|
setSubmitting(true)
|
|
|
|
const formData = new FormData(event.currentTarget)
|
|
formData.set("flow", flow)
|
|
|
|
try {
|
|
await signIn("password", formData)
|
|
router.push("/")
|
|
} catch {
|
|
setError(
|
|
flow === "signIn"
|
|
? "Invalid email or password."
|
|
: "Could not create account. Try a different email."
|
|
)
|
|
setSubmitting(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Card className="w-full max-w-sm">
|
|
<CardHeader>
|
|
<CardTitle className="text-lg">{text.title}</CardTitle>
|
|
<CardDescription>{text.description}</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
{signupsDisabled ? (
|
|
<p className="text-sm text-muted-foreground">
|
|
New account registration is currently disabled. Contact an
|
|
administrator if you need access.
|
|
</p>
|
|
) : (
|
|
<form onSubmit={handleSubmit}>
|
|
<FieldGroup>
|
|
{flow === "signUp" && (
|
|
<Field>
|
|
<FieldLabel htmlFor="name">Name</FieldLabel>
|
|
<Input
|
|
id="name"
|
|
name="name"
|
|
type="text"
|
|
autoComplete="name"
|
|
placeholder="Ada Lovelace"
|
|
required
|
|
/>
|
|
</Field>
|
|
)}
|
|
<Field>
|
|
<FieldLabel htmlFor="email">Email</FieldLabel>
|
|
<Input
|
|
id="email"
|
|
name="email"
|
|
type="email"
|
|
autoComplete="email"
|
|
placeholder="you@example.com"
|
|
required
|
|
/>
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor="password">Password</FieldLabel>
|
|
<Input
|
|
id="password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete={
|
|
flow === "signIn" ? "current-password" : "new-password"
|
|
}
|
|
required
|
|
/>
|
|
</Field>
|
|
{error && <FieldError>{error}</FieldError>}
|
|
<Field>
|
|
<Button type="submit" disabled={submitting}>
|
|
{submitting && <Spinner />}
|
|
{text.submit}
|
|
</Button>
|
|
</Field>
|
|
{!hideSignUpLink ? (
|
|
<p className="text-center text-sm text-muted-foreground">
|
|
{text.switchPrompt}{" "}
|
|
<Link
|
|
href={text.switchHref}
|
|
className="text-primary underline-offset-4 hover:underline"
|
|
>
|
|
{text.switchLabel}
|
|
</Link>
|
|
</p>
|
|
) : null}
|
|
</FieldGroup>
|
|
</form>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
)
|
|
}
|