import '@/index.css' import { useState } from 'react' import { navigate } from 'vike/client/router' import { getCsrfToken } from '@rudderjs/middleware/client' // URL this view is served at — MUST match the controller route registered // by registerAuthRoutes() in the consumer project. If you override // `opts.paths.login` when calling registerAuthRoutes, update this too so // Vike's client router can SPA-navigate here instead of full-reloading. export const route = '/login' export interface LoginProps { submitUrl?: string registerUrl?: string forgotPasswordUrl?: string homeUrl?: string } export default function Login(props: LoginProps) { const submitUrl = props.submitUrl ?? '/auth/sign-in/email' const registerUrl = props.registerUrl ?? '/register' const forgotPasswordUrl = props.forgotPasswordUrl ?? '/forgot-password' const homeUrl = props.homeUrl ?? '/' 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('') setLoading(true) const res = await fetch(submitUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': getCsrfToken(), }, body: JSON.stringify({ email, password }), }) if (res.ok) { const params = new URLSearchParams(window.location.search) const redirect = params.get('redirect') await navigate(redirect && redirect.startsWith('/') ? redirect : homeUrl) } else { const body = await res.json().catch(() => ({})) as { message?: string } setError(body.message ?? 'Invalid email or password.') } setLoading(false) } return (

Welcome back

Sign in to your account

{error &&

{error}

}
setEmail(e.currentTarget.value)} required autoComplete="email" className="form-input" />
setPassword(e.currentTarget.value)} required autoComplete="current-password" className="form-input" />
Forgot password? Register
) }