Repository navigation
Add form validation, providers, and onboarding hooks #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| 'use client' | ||
|
|
||
| import { zodResolver } from '@hookform/resolvers/zod' | ||
| import { ArrowRight, Loader2 } from 'lucide-react' | ||
| import { useState, type ChangeEvent, type FormEvent } from 'react' | ||
| import { authClient } from '@/lib/auth/client' | ||
| import { useState } from 'react' | ||
| import { useForm } from 'react-hook-form' | ||
|
|
||
| import { FormField } from '@/components/ui/form-field' | ||
| import { useSocialSignIn } from '@/lib/hooks/use-auth' | ||
| import { loginSchema, type LoginInput } from '@/lib/schemas/auth.schema' | ||
| import { CharactersScene, type CharacterSceneState } from './CharactersScene' | ||
| import { sound } from './audio' | ||
|
|
||
|
|
@@ -42,18 +47,22 @@ export default function LoginPage() { | |
| }) | ||
|
|
||
| const [isSignUp, setIsSignUp] = useState(false) | ||
| const [name, setName] = useState('') | ||
| const [email, setEmail] = useState('') | ||
| const [isRedirecting, setIsRedirecting] = useState(false) | ||
| const [errorMsg, setErrorMsg] = useState('') | ||
|
|
||
| const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
| const val = e.target.value | ||
| setEmail(val) | ||
| setState((s) => ({ ...s, emailLength: val.length })) | ||
| sound.playKeystroke(val.length) | ||
| setErrorMsg('') | ||
| } | ||
| const socialSignInMutation = useSocialSignIn() | ||
|
|
||
| const { | ||
| register, | ||
| handleSubmit, | ||
| formState: { errors }, | ||
| } = useForm<LoginInput>({ | ||
| resolver: zodResolver(loginSchema), | ||
| defaultValues: { | ||
| name: '', | ||
| email: '', | ||
| }, | ||
| }) | ||
|
|
||
| const failWith = (message: string) => { | ||
| setIsRedirecting(false) | ||
|
|
@@ -62,12 +71,7 @@ export default function LoginPage() { | |
| sound.playError() | ||
| } | ||
|
|
||
| /** | ||
| * Email sign-in is not wired up yet — Google is the only provider. The fields | ||
| * stay in place for a later change, but submitting must never fake a session. | ||
| */ | ||
| const handleSubmit = (e: FormEvent) => { | ||
| e.preventDefault() | ||
| const onSubmit = () => { | ||
| failWith("Email sign-in isn't available yet — continue with Google.") | ||
| } | ||
|
|
||
|
|
@@ -77,15 +81,15 @@ export default function LoginPage() { | |
| setState((s) => ({ ...s, isError: false })) | ||
| sound.playPop(520) | ||
|
|
||
| const { error } = await authClient.signIn.social({ | ||
| provider: 'google', | ||
| callbackURL: '/role-select', | ||
| }) | ||
|
|
||
| // On success the browser navigates to Google, so this only runs when the | ||
| // handshake could not be started at all. | ||
| if (error) { | ||
| failWith(error.message ?? 'Could not start Google sign-in. Please try again.') | ||
| try { | ||
| await socialSignInMutation.mutateAsync({ | ||
| provider: 'google', | ||
| callbackURL: '/role-select', | ||
| }) | ||
| } catch (err: unknown) { | ||
| const message = | ||
| err instanceof Error ? err.message : 'Could not start Google sign-in. Please try again.' | ||
| failWith(message) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -141,59 +145,50 @@ export default function LoginPage() { | |
| </div> | ||
|
|
||
| {/* Form */} | ||
| <form onSubmit={handleSubmit} className="space-y-4"> | ||
| <form onSubmit={handleSubmit(onSubmit)} className="space-y-4" noValidate> | ||
| {isSignUp && ( | ||
| <div className="space-y-1.5 text-left"> | ||
| <label | ||
| htmlFor="name-input" | ||
| className="text-xs font-semibold text-zinc-700 block" | ||
| > | ||
| Name | ||
| </label> | ||
| <FormField label="Name" htmlFor="name-input" error={errors.name?.message}> | ||
| <input | ||
| id="name-input" | ||
| type="text" | ||
| value={name} | ||
| onChange={(e) => setName(e.target.value)} | ||
| {...register('name')} | ||
| onFocus={() => { | ||
| setState((s) => ({ ...s, focusedField: 'email' })) | ||
| sound.playPop(380) | ||
| }} | ||
| onBlur={() => setState((s) => ({ ...s, focusedField: 'none' }))} | ||
| placeholder="Your full name" | ||
| className="w-full px-3.5 py-2.5 rounded-xl border border-zinc-200 text-sm focus:outline-none focus:ring-2 focus:ring-zinc-900/10 focus:border-zinc-900 transition bg-zinc-50/50 hover:bg-zinc-50" | ||
| className="w-full px-3.5 py-2.5 rounded-xl border border-zinc-200 text-sm focus:outline-none focus:ring-2 focus:ring-zinc-900/10 focus:border-zinc-900 transition bg-zinc-50/50 hover:bg-zinc-50 text-zinc-900" | ||
| /> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets use the shadcn input, also ig it has inbuilt support for react-hook-form iirc |
||
| </div> | ||
| </FormField> | ||
| )} | ||
|
|
||
| {/* Email Field */} | ||
| <div className="space-y-1.5 text-left"> | ||
| <label | ||
| htmlFor="email-input" | ||
| className="text-xs font-semibold text-zinc-700 block" | ||
| > | ||
| </label> | ||
| <div className="relative"> | ||
| <input | ||
| id="email-input" | ||
| type="email" | ||
| value={email} | ||
| onChange={handleEmailChange} | ||
| onFocus={() => { | ||
| setState((s) => ({ ...s, focusedField: 'email' })) | ||
| sound.playPop(420) | ||
| }} | ||
| onBlur={() => setState((s) => ({ ...s, focusedField: 'none' }))} | ||
| placeholder="Enter your email" | ||
| className={`w-full px-3.5 py-2.5 rounded-xl border text-sm text-zinc-900 placeholder:text-zinc-400 focus:outline-none transition bg-zinc-50/40 hover:bg-zinc-50 ${ | ||
| state.focusedField === 'email' | ||
| ? 'border-zinc-900 ring-2 ring-zinc-900/10 bg-white' | ||
| : 'border-zinc-200' | ||
| }`} | ||
| /> | ||
| </div> | ||
| </div> | ||
| <FormField label="Email" htmlFor="email-input" error={errors.email?.message}> | ||
| <input | ||
| id="email-input" | ||
| type="email" | ||
| {...register('email', { | ||
| onChange: (e) => { | ||
| const val = e.target.value | ||
| setState((s) => ({ ...s, emailLength: val.length })) | ||
| sound.playKeystroke(val.length) | ||
| setErrorMsg('') | ||
| }, | ||
| })} | ||
| onFocus={() => { | ||
| setState((s) => ({ ...s, focusedField: 'email' })) | ||
| sound.playPop(420) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 420 Hz is the pitch for the playful sound effect. I can pull it into a named constant or disable the sounds if you’d rather keep it silent!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah lets keep it silent |
||
| }} | ||
| onBlur={() => setState((s) => ({ ...s, focusedField: 'none' }))} | ||
| placeholder="Enter your email" | ||
| className={`w-full px-3.5 py-2.5 rounded-xl border text-sm text-zinc-900 placeholder:text-zinc-400 focus:outline-none transition bg-zinc-50/40 hover:bg-zinc-50 ${ | ||
| state.focusedField === 'email' | ||
| ? 'border-zinc-900 ring-2 ring-zinc-900/10 bg-white' | ||
| : 'border-zinc-200' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets avoid ternaries, can u use classNames here from shadcn package? |
||
| }`} | ||
| /> | ||
| </FormField> | ||
|
|
||
| {/* Error banner */} | ||
| {errorMsg && ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default values can be a seperate object outside, to keep the code lean