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

login.tsx

140 lines4.6 KB

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