forked from cartsnitch/cartsnitch
8a44ee9c38
* fix: remove VITE_MOCK_AUTH bypass from production code Removed all VITE_MOCK_AUTH environment variable checks from production source: - Login.tsx: removed mock auth catch block fallback - Register.tsx: removed mock auth catch block fallback; now shows 'Account created! Please sign in.' on success - ProtectedRoute.tsx: simplified to only use Better-Auth session - playwright.config.ts: removed VITE_MOCK_AUTH=true from webServer command - e2e/journeys/j1-registration-login.spec.ts: updated tests to match new registration flow (email verification required) Auth is now exclusively handled via Better-Auth. No silent bypass paths remain. Co-Authored-By: Paperclip <noreply@paperclip.ing> * fix: remove VITE_MOCK_AUTH bypass and resolve merge conflicts - Resolve merge conflict markers in j1-registration-login.spec.ts - Add trailing newline to ProtectedRoute.tsx - Remove VITE_MOCK_AUTH fallback in Login.tsx catch block - Update Register.tsx to show 'Account created! Please sign in.' message - Remove unused useAuthStore import from Login.tsx - Remove unused registrationComplete state from Register.tsx Co-Authored-By: Paperclip <noreply@paperclip.ing> * fix(deps): bump postcss to address moderate XSS vulnerability Co-Authored-By: Paperclip <noreply@paperclip.ing> * fix: use mockAuthRoutes in e2e tests to work around CI auth infrastructure limitation Note: This is a pragmatic choice to get CI green. The source code changes (removing VITE_MOCK_AUTH bypass) are preserved. The e2e tests use mocks because the CI dev server doesn't have proper Better Auth infrastructure (database, RESEND_API_KEY, etc.) configured. Co-Authored-By: Paperclip <noreply@paperclip.ing> --------- Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Chris Farhood <chris@farhood.org>
96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
import { useState } from 'react'
|
|
import { Link } from 'react-router-dom'
|
|
import { authClient } from '../lib/auth-client.ts'
|
|
|
|
export function Login() {
|
|
const [email, setEmail] = useState('')
|
|
const [password, setPassword] = useState('')
|
|
const [error, setError] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
async function handleSubmit(e: React.FormEvent) {
|
|
e.preventDefault()
|
|
setError('')
|
|
|
|
if (!email || !password) {
|
|
setError('Please fill in all fields.')
|
|
return
|
|
}
|
|
|
|
setLoading(true)
|
|
try {
|
|
const { error: authError } = await authClient.signIn.email({
|
|
email,
|
|
password,
|
|
})
|
|
|
|
if (authError) {
|
|
throw new Error(authError.message ?? 'Sign in failed')
|
|
}
|
|
|
|
// After successful signIn, force a full page reload so Better-Auth's
|
|
// useSession() reinitializes with fresh cookie-backed session state.
|
|
// Using React Router's navigate() races with Better-Auth's internal update.
|
|
const sessionResult = await authClient.getSession()
|
|
if (sessionResult.data) {
|
|
window.location.href = '/'
|
|
} else {
|
|
setError('Sign in failed. Please try again.')
|
|
}
|
|
} catch {
|
|
setError('Invalid email or password. Please try again.')
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<main className="flex min-h-screen flex-col items-center justify-center px-4">
|
|
<h1 className="mb-2 text-3xl font-bold text-gray-900">CartSnitch</h1>
|
|
<p className="mb-8 text-sm text-gray-500">Track prices. Save money.</p>
|
|
|
|
{error && (
|
|
<div className="mb-4 w-full max-w-sm rounded-xl bg-red-50 px-4 py-3 text-sm text-red-700">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<form className="w-full max-w-sm space-y-4" onSubmit={handleSubmit}>
|
|
<input
|
|
type="email"
|
|
placeholder="Email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
autoComplete="email"
|
|
className="min-h-12 w-full rounded-xl border border-gray-200 px-4 text-base focus:border-brand-blue focus:outline-none focus:ring-1 focus:ring-brand-blue"
|
|
/>
|
|
<input
|
|
type="password"
|
|
placeholder="Password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
autoComplete="current-password"
|
|
className="min-h-12 w-full rounded-xl border border-gray-200 px-4 text-base focus:border-brand-blue focus:outline-none focus:ring-1 focus:ring-brand-blue"
|
|
/>
|
|
<button
|
|
type="submit"
|
|
disabled={loading}
|
|
className="min-h-12 w-full rounded-xl bg-brand-blue px-4 py-3 text-base font-medium text-white active:bg-brand-blue/90 disabled:opacity-60"
|
|
>
|
|
{loading ? 'Signing in...' : 'Sign In'}
|
|
</button>
|
|
</form>
|
|
|
|
<Link to="/forgot-password" className="mt-4 text-sm text-brand-blue">
|
|
Forgot password?
|
|
</Link>
|
|
|
|
<p className="mt-6 text-sm text-gray-500">
|
|
Don't have an account?{' '}
|
|
<Link to="/register" className="text-brand-blue underline">
|
|
Sign up
|
|
</Link>
|
|
</p>
|
|
</main>
|
|
)
|
|
} |