mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 23:24:21 +00:00
* feat(mcp): add OAuth 2.1 support with dynamic client registration * feat(api): alias bare oauth-authorization-server discovery endpoint * fix(auth): preserve user session on OAuth sign-in redirect to consent
303 lines
10 KiB
TypeScript
303 lines
10 KiB
TypeScript
import { client } from '@/ui/api/client'
|
|
import { AuthLayout } from '@/ui/components/auth-layout'
|
|
import { Button } from '@/ui/components/ui/button'
|
|
import { ShumaiLogo } from '@/ui/components/ui/icons'
|
|
import { Input } from '@/ui/components/ui/input'
|
|
import { Label } from '@/ui/components/ui/label'
|
|
import { getAuthSession, signIn } from '@/ui/lib/auth-client'
|
|
import { useAuthStore } from '@/ui/stores/auth'
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { useQuery } from '@tanstack/react-query'
|
|
import { createFileRoute, redirect, useNavigate } from '@tanstack/react-router'
|
|
import { ArrowRight, Loader2, Lock, LogIn, Mail, UserCheck } from 'lucide-react'
|
|
import { useEffect, useState } from 'react'
|
|
import { useForm } from 'react-hook-form'
|
|
import { z } from 'zod'
|
|
import { m } from '@/ui/paraglide/messages.js'
|
|
|
|
const loginSchema = z.object({
|
|
email: z.string().email(m.invalid_email_address()),
|
|
password: z.string().min(1, m.password_is_required()),
|
|
})
|
|
|
|
type LoginSchema = z.infer<typeof loginSchema>
|
|
|
|
function LoginPage() {
|
|
const navigate = useNavigate()
|
|
const setUser = useAuthStore((state) => state.setUser)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [loading, setLoading] = useState(false)
|
|
const [demoLoading, setDemoLoading] = useState(false)
|
|
|
|
const { data: signupInfo, isLoading: isSignupInfoLoading } = useQuery({
|
|
queryKey: ['/signup-info'],
|
|
queryFn: async () => {
|
|
const res = await client.api['signup-info'].$get()
|
|
if (!res.ok) throw new Error('Failed to fetch signup info')
|
|
return await res.json()
|
|
},
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (signupInfo && !signupInfo.initialized) {
|
|
navigate({ to: '/signup', search: { inviteCode: '' } })
|
|
}
|
|
}, [signupInfo, navigate])
|
|
|
|
useEffect(() => {
|
|
const user = useAuthStore.getState().user
|
|
if (user && typeof window !== 'undefined') {
|
|
const searchParams = new URLSearchParams(window.location.search)
|
|
if (searchParams.has('client_id')) {
|
|
window.location.href = `/api/auth/oauth2/authorize?${searchParams.toString()}`
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
const isDemoEnv = signupInfo?.demoMode === true
|
|
|
|
const form = useForm<LoginSchema>({
|
|
resolver: zodResolver(loginSchema),
|
|
})
|
|
|
|
const onSubmit = async (data: LoginSchema) => {
|
|
setLoading(true)
|
|
setError(null)
|
|
const { data: session, error: signInError } = await signIn.email({
|
|
email: data.email,
|
|
password: data.password,
|
|
})
|
|
|
|
setLoading(false)
|
|
if (signInError) {
|
|
setError(signInError.message || m.invalid_credentials())
|
|
return
|
|
}
|
|
|
|
if (session) {
|
|
const redirectUrl = (session as { url?: string }).url
|
|
if (redirectUrl) {
|
|
try {
|
|
const sessionData = await getAuthSession()
|
|
if (sessionData?.data?.user) {
|
|
setUser(sessionData.data.user)
|
|
}
|
|
} catch {
|
|
// Ignore and continue redirect
|
|
}
|
|
window.location.href = redirectUrl
|
|
return
|
|
}
|
|
|
|
if ('user' in session && session.user) {
|
|
setUser(session.user)
|
|
}
|
|
navigate({ to: '/' })
|
|
}
|
|
}
|
|
|
|
const onDemoLogin = async () => {
|
|
setDemoLoading(true)
|
|
setError(null)
|
|
form.clearErrors()
|
|
form.setValue('email', 'foo@bar.com')
|
|
form.setValue('password', 'foo')
|
|
|
|
const { data: session, error: signInError } = await signIn.email({
|
|
email: 'foo@bar.com',
|
|
password: 'foo',
|
|
})
|
|
|
|
setDemoLoading(false)
|
|
if (signInError) {
|
|
setError(signInError.message || m.invalid_credentials())
|
|
return
|
|
}
|
|
|
|
if (session) {
|
|
const redirectUrl = (session as { url?: string }).url
|
|
if (redirectUrl) {
|
|
try {
|
|
const sessionData = await getAuthSession()
|
|
if (sessionData?.data?.user) {
|
|
setUser(sessionData.data.user)
|
|
}
|
|
} catch {
|
|
// Ignore and continue redirect
|
|
}
|
|
window.location.href = redirectUrl
|
|
return
|
|
}
|
|
|
|
if ('user' in session && session.user) {
|
|
setUser(session.user)
|
|
}
|
|
navigate({ to: '/' })
|
|
}
|
|
}
|
|
|
|
if (isSignupInfoLoading) {
|
|
return (
|
|
<div className="flex items-center justify-center min-h-screen bg-zinc-100/80 dark:bg-black">
|
|
<div className="relative flex flex-col items-center">
|
|
<ShumaiLogo className="h-16 w-16 animate-pulse mb-4 text-rose-500 shadow-xl rounded-2xl" />
|
|
<Loader2 className="h-6 w-6 animate-spin text-rose-500/70" />
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<AuthLayout>
|
|
<div className="mb-8">
|
|
<h1 className="text-2xl font-bold tracking-tight text-zinc-900 dark:text-zinc-50">
|
|
{m.welcome_back()}
|
|
</h1>
|
|
<p className="text-zinc-500 dark:text-zinc-400 text-sm mt-1">{m.login_subtitle()}</p>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="mb-6 p-3 bg-red-500/10 border border-red-500/20 text-red-600 dark:text-red-400 rounded-xl text-sm text-center font-medium">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-5">
|
|
<div className="space-y-2">
|
|
<Label
|
|
htmlFor="email"
|
|
className="text-xs font-semibold text-zinc-600 dark:text-zinc-400 tracking-wide uppercase"
|
|
>
|
|
{m.email_address()}
|
|
</Label>
|
|
<div className="relative group">
|
|
<Mail className="absolute left-3.5 top-1/2 -translate-y-1/2 w-4 h-4 text-zinc-400 dark:text-zinc-600 transition-colors group-focus-within:text-rose-500" />
|
|
<Input
|
|
id="email"
|
|
{...form.register('email')}
|
|
placeholder="you@example.com"
|
|
type="email"
|
|
className="pl-10.5 h-11 border-zinc-200 dark:border-zinc-800 focus-visible:ring-rose-500/50 focus-visible:border-rose-500 rounded-xl transition-all shadow-sm"
|
|
/>
|
|
</div>
|
|
{form.formState.errors.email && (
|
|
<p className="text-red-500 text-xs font-medium">
|
|
{form.formState.errors.email.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label
|
|
htmlFor="password"
|
|
className="text-xs font-semibold text-zinc-600 dark:text-zinc-400 tracking-wide uppercase"
|
|
>
|
|
{m.password()}
|
|
</Label>
|
|
<div className="relative group">
|
|
<Lock className="absolute left-3.5 top-1/2 -translate-y-1/2 w-4 h-4 text-zinc-400 dark:text-zinc-600 transition-colors group-focus-within:text-rose-500" />
|
|
<Input
|
|
id="password"
|
|
type="password"
|
|
{...form.register('password')}
|
|
placeholder={m.password_placeholder()}
|
|
className="pl-10.5 h-11 border-zinc-200 dark:border-zinc-800 focus-visible:ring-rose-500/50 focus-visible:border-rose-500 rounded-xl transition-all shadow-sm"
|
|
/>
|
|
</div>
|
|
{form.formState.errors.password && (
|
|
<p className="text-red-500 text-xs font-medium">
|
|
{form.formState.errors.password.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
<Button
|
|
type="submit"
|
|
className="w-full h-11 bg-gradient-to-r from-rose-500 to-orange-500 hover:from-rose-600 hover:to-orange-600 text-white font-semibold rounded-xl shadow-md shadow-rose-500/10 hover:shadow-rose-500/20 active:scale-[0.98] transition-all flex items-center justify-center gap-2 border-0"
|
|
disabled={loading || demoLoading}
|
|
>
|
|
{loading ? (
|
|
<>
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
{m.logging_in()}
|
|
</>
|
|
) : (
|
|
<>
|
|
<LogIn className="w-4 h-4" />
|
|
{m.login()}
|
|
</>
|
|
)}
|
|
</Button>
|
|
</form>
|
|
|
|
{isDemoEnv && (
|
|
<div className="mt-4 space-y-4">
|
|
<div className="relative">
|
|
<div className="absolute inset-0 flex items-center">
|
|
<span className="w-full border-t border-zinc-200/80 dark:border-zinc-800" />
|
|
</div>
|
|
<div className="relative flex justify-center text-xs uppercase">
|
|
<span className="bg-white dark:bg-zinc-900 px-2 text-zinc-400 dark:text-zinc-500 font-medium">
|
|
{m.or()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
onClick={onDemoLogin}
|
|
disabled={loading || demoLoading}
|
|
className="w-full h-11 border-zinc-200 dark:border-zinc-800 hover:bg-zinc-100 dark:hover:bg-zinc-800 font-semibold rounded-xl transition-all flex items-center justify-center gap-2 shadow-xs cursor-pointer"
|
|
>
|
|
{demoLoading ? (
|
|
<>
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
{m.logging_in()}
|
|
</>
|
|
) : (
|
|
<>
|
|
<UserCheck className="w-4 h-4 text-zinc-500 dark:text-zinc-400" />
|
|
{m.login_as_demo_user()}
|
|
</>
|
|
)}
|
|
</Button>
|
|
|
|
<div className="p-3 bg-blue-500/10 border border-blue-500/20 text-blue-700 dark:text-blue-400 rounded-xl text-xs leading-relaxed text-center">
|
|
<p>
|
|
<strong>{m.demo_access()}</strong> {m.demo_access_instructions()}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<div className="mt-8 pt-6 border-t border-zinc-200/30 dark:border-zinc-800/30 text-center text-sm">
|
|
<span className="text-zinc-500 dark:text-zinc-400">{m.no_account_prompt()}</span>
|
|
<a
|
|
href="/signup"
|
|
className="font-semibold text-rose-500 hover:text-rose-600 dark:text-rose-400 dark:hover:text-rose-300 transition-colors hover:underline inline-flex items-center gap-0.5"
|
|
>
|
|
{m.sign_up()} <ArrowRight className="w-3.5 h-3.5" />
|
|
</a>
|
|
</div>
|
|
</AuthLayout>
|
|
)
|
|
}
|
|
|
|
export const Route = createFileRoute('/login')({
|
|
component: LoginPage,
|
|
beforeLoad: () => {
|
|
if (typeof window !== 'undefined') {
|
|
const searchParams = new URLSearchParams(window.location.search)
|
|
if (searchParams.has('client_id')) {
|
|
return
|
|
}
|
|
}
|
|
if (useAuthStore.getState().user) {
|
|
throw redirect({
|
|
to: '/',
|
|
})
|
|
}
|
|
},
|
|
})
|