-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.tsx
218 lines (190 loc) · 7.19 KB
/
popup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import type { Provider, User } from "@supabase/supabase-js"
import { useEffect, useState } from "react"
import { Storage } from "@plasmohq/storage"
import { useStorage } from "@plasmohq/storage/hook"
import { supabase } from "~core/supabase"
import "./style.css"
import ChatbotWholePage from "~components/ChatbotWholePage"
function IndexPopup() {
const [user, setUser] = useStorage<User>({
key: "user",
instance: new Storage({
area: "local"
})
})
const [activeTab, setActiveTab] = useState('login')
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
useEffect(() => {
async function init() {
const { data, error } = await supabase.auth.getSession()
if (error) {
console.error(error)
return
}
if (!!data.session) {
setUser(data.session.user)
}
}
init()
}, [])
const handleEmailLogin = async (
type: "LOGIN" | "SIGNUP",
username: string,
password: string,
e: React.FormEvent
) => {
e.preventDefault()
try {
const {
error,
data: { user }
} =
type === "LOGIN"
? await supabase.auth.signInWithPassword({
email: username,
password
})
: await supabase.auth.signUp({ email: username, password })
if (error) {
alert("Error with auth: " + error.message)
} else if (!user) {
alert("Signup successful, confirmation mail should be sent soon!")
} else {
console.log(user)
setUser(user)
}
} catch (error) {
console.log("error", error)
alert(error.error_description || error)
}
}
return (
<>
{user && (
<>
<div className="container p-2">
<ChatbotWholePage user={user} setUser={setUser}/>
{/* <h3>
{user.email} - {user.id}
</h3>
<button
onClick={() => {
supabase.auth.signOut()
setUser(null)
}}>
Logout
</button> */}
</div>
</>
)}
{
!user && (
<div className="font-sans min-w-[20rem] mx-auto mt-10 bg-white rounded-lg shadow-md overflow-hidden">
<div className="flex border-b">
<button
className={`flex-1 py-2 px-4 text-center focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 ${activeTab === 'login'
? 'bg-white text-blue-600 border-b-2 border-blue-500 font-semibold'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200'
}`}
onClick={() => setActiveTab('login')}
>
Login
</button>
<button
className={`flex-1 py-2 px-4 text-center focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 ${activeTab === 'signup'
? 'bg-white text-blue-600 border-b-2 border-blue-500 font-semibold'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200'
}`}
onClick={() => setActiveTab('signup')}
>
Sign Up
</button>
</div>
<div className="p-6">
{activeTab === 'login' ? (
<form className="space-y-4"
onSubmit={(e) => {
handleEmailLogin("LOGIN", username, password,e)
}}
>
<div>
<label htmlFor="login-email" className="block text-sm font-medium text-gray-700">
Email
</label>
<input
type="email"
placeholder="Your Email"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
/>
</div>
<div>
<label htmlFor="login-password" className="block text-sm font-medium text-gray-700">
Password
</label>
<input
type="password"
placeholder="Your password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
/>
</div>
<button
type="submit"
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
Login
</button>
</form>
) : (
<form className="space-y-4"
onSubmit={(e) => {
handleEmailLogin("SIGNUP", username, password,e)
}}
>
<div>
<label htmlFor="signup-email" className="block text-sm font-medium text-gray-700">
Email
</label>
<input
type="email"
placeholder="Your Email"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
/>
</div>
<div>
<label htmlFor="signup-password" className="block text-sm font-medium text-gray-700">
Password
</label>
<input
type="password"
placeholder="Your password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
/>
</div>
<button
type="submit"
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
Sign Up
</button>
</form>
)}
</div>
</div>
)}
</>
)
}
export default IndexPopup