Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

login.tsx

142 lines4.7 KB

1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import type { FormEvent } from 'react'
4import { authClient } from '@/lib/auth-client'
5​
6export const Route = createFileRoute(`/login`)({
7 component: Layout,
8 ssr: false,
9})
10​
11function Layout() {
12 const [email, setEmail] = useState(``)
13 const [password, setPassword] = useState(``)
14 const [isLoading, setIsLoading] = useState(false)
15 const [currentError, setError] = useState(``)
16​
17 const handleSubmit = async (e: FormEvent) => {
18 e.preventDefault()
19 setIsLoading(true)
20 setError(``)
21​
22 try {
23 let { data, error } = await authClient.signUp.email(
24 {
25 email,
26 password,
27 name: email,
28 },
29 {
30 onSuccess: () => {
31 console.log(`signed up, navigating`)
32 window.location.href = `/`
33 },
34 }
35 )
36​
37 if (error?.code === `USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL`) {
38 console.log(`user exists, logging in`)
39 const result = await authClient.signIn.email(
40 {
41 email,
42 password,
43 },
44 {
45 onSuccess: async () => {
46 console.log(`signed in, navigating`)
47 const { data: session, error: sessionError } =
48 await authClient.getSession()
49 console.log({ session, sessionError })
50 window.location.href = `/`
51 },
52 }
53 )
54​
55 data = result.data
56 error = result.error
57 }
58​
59 console.log({ data, error })
60​
61 if (error) {
62 console.log(`set error`)
63 setError(JSON.stringify(error, null, 4))
64 }
65 } catch {
66 setError(`An unexpected error occurred`)
67 } finally {
68 setIsLoading(false)
69 }
70 }
71​
72 return (
73 <div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
74 <div className="max-w-md w-full space-y-8">
75 <div>
76 <h2 className="mt-6 text-center text-3xl font-extrabold text-gray-900">
77 Sign in to your account
78 </h2>
79 <div className="mt-4 p-4 bg-blue-50 border border-blue-200 rounded-md">
80 <p className="text-sm text-blue-700">
81 <strong>Development Mode:</strong> Any email/password combination
82 will work for testing. Also new accounts will be automatically
83 created when you try signing in with a new combo.
84 </p>
85 </div>
86 </div>
87 <form className="mt-8 space-y-6" onSubmit={handleSubmit}>
88 <div className="rounded-md shadow-sm -space-y-px">
89 <div>
90 <label htmlFor="email" className="sr-only">
91 Email address
92 </label>
93 <input
94 id="email"
95 name="email"
96 type="email"
97 autoComplete="email"
98 required
99 value={email}
100 onChange={(e) => setEmail(e.target.value)}
101 className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
102 placeholder="Email address"
103 />
104 </div>
105 <div>
106 <label htmlFor="password" className="sr-only">
107 Password
108 </label>
109 <input
110 id="password"
111 name="password"
112 type="password"
113 autoComplete="current-password"
114 required
115 value={password}
116 onChange={(e) => setPassword(e.target.value)}
117 className="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
118 placeholder="Password"
119 />
120 </div>
121 </div>
122​
123 {currentError && (
124 <div className="rounded-md bg-red-50 p-4">
125 <div className="text-sm text-red-700">{currentError}</div>
126 </div>
127 )}
128​
129 <div>
130 <button
131 type="submit"
132 disabled={isLoading}
133 className="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50 disabled:cursor-not-allowed"
134 >
135 {isLoading ? `Signing in...` : `Sign in`}
136 </button>
137 </div>
138 </form>
139 </div>
140 </div>
141 )