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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| '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 { Input } from '@/components/ui/input' | ||
| 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' | ||
|
|
||
|
|
@@ -29,6 +35,11 @@ function GoogleIcon({ className = 'w-4 h-4' }: { className?: string }) { | |
| ) | ||
| } | ||
|
|
||
| const DEFAULT_VALUES: LoginInput = { | ||
| name: '', | ||
| email: '', | ||
| } | ||
|
|
||
| export default function LoginPage() { | ||
| const [state, setState] = useState<CharacterSceneState>({ | ||
| focusedField: 'none', | ||
|
|
@@ -42,18 +53,19 @@ 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: DEFAULT_VALUES, | ||
| }) | ||
|
|
||
| const failWith = (message: string) => { | ||
| setIsRedirecting(false) | ||
|
|
@@ -62,12 +74,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 +84,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 +148,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> | ||
| <input | ||
| <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" | ||
| /> | ||
| </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) | ||
| }} | ||
| 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 && ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
whats
420here? magic number, please put it in a constant, and why are we playing sounds?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.
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!
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.
yeah lets keep it silent