-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth-example.tsx
More file actions
174 lines (162 loc) · 4.87 KB
/
auth-example.tsx
File metadata and controls
174 lines (162 loc) · 4.87 KB
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
"use client";
import React, { useEffect, useMemo, useState } from "react";
import { initializeFirebase, IUser, UserService } from "react-firebase-chat";
const firebaseConfig = {
apiKey: "your-web-api-key",
authDomain: "your-project.firebaseapp.com",
projectId: "your-project-id",
storageBucket: "your-project.appspot.com",
messagingSenderId: "123456789",
appId: "1:123456789:web:abcdef123456",
};
initializeFirebase(firebaseConfig);
export default function AuthScreen({
onAuthenticated,
}: {
onAuthenticated: (user: IUser) => void;
}) {
const userService = useMemo(() => UserService.getInstance(), []);
const [users, setUsers] = useState<IUser[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string>("");
const [newUserId, setNewUserId] = useState<string>("");
const [newUserName, setNewUserName] = useState<string>("");
useEffect(() => {
let isMounted = true;
const load = async () => {
setIsLoading(true);
setError("");
try {
const list = await userService.getAllUsers();
if (isMounted) setUsers(list as IUser[]);
} catch (e: any) {
if (isMounted) setError(e?.message || "Failed to load users");
} finally {
if (isMounted) setIsLoading(false);
}
};
load();
return () => {
isMounted = false;
};
}, [userService]);
const handleCreate = async (e: React.FormEvent) => {
e.preventDefault();
if (!newUserId.trim() || !newUserName.trim()) return;
setIsLoading(true);
setError("");
try {
await userService.createUserIfNotExists(newUserId.trim(), {
name: newUserName.trim(),
});
const created: IUser = { id: newUserId.trim(), name: newUserName.trim() };
onAuthenticated(created);
} catch (e: any) {
setError(e?.message || "Failed to create user");
} finally {
setIsLoading(false);
}
};
const handleSelect = (user: IUser) => {
onAuthenticated(user);
};
return (
<div style={{ width: "100%", maxWidth: 640 }}>
<div style={{ marginBottom: 24 }}>
<h3 style={{ fontSize: 18, fontWeight: 600 }}>Authenticate</h3>
<p style={{ color: "#555", marginTop: 4 }}>
Create a new user or pick an existing one.
</p>
</div>
{error && (
<div style={{ marginBottom: 12, color: "#b00020" }}>{error}</div>
)}
<form
onSubmit={handleCreate}
style={{
display: "flex",
gap: 8,
marginBottom: 24,
alignItems: "center",
}}
>
<input
placeholder="New user ID"
value={newUserId}
onChange={(e) => setNewUserId(e.target.value)}
style={{
flex: 2,
padding: 8,
border: "1px solid #ddd",
borderRadius: 8,
}}
/>
<input
placeholder="New user name"
value={newUserName}
onChange={(e) => setNewUserName(e.target.value)}
style={{
flex: 3,
padding: 8,
border: "1px solid #ddd",
borderRadius: 8,
}}
/>
<button
type="submit"
disabled={isLoading}
style={{
padding: "8px 12px",
borderRadius: 8,
background: "#111",
color: "#fff",
border: 0,
}}
>
{isLoading ? "Creating..." : "Create user"}
</button>
</form>
<div>
<div style={{ fontWeight: 600, marginBottom: 8 }}>
Or sign in as existing user
</div>
<div
style={{
border: "1px solid #eee",
borderRadius: 8,
overflow: "hidden",
}}
>
{isLoading && users.length === 0 ? (
<div style={{ padding: 12 }}>Loading users...</div>
) : users.length === 0 ? (
<div style={{ padding: 12, color: "#666" }}>No users found.</div>
) : (
<ul style={{ listStyle: "none", margin: 0, padding: 0 }}>
{users.map((u) => (
<li key={u.id} style={{ borderTop: "1px solid #f3f3f3" }}>
<button
onClick={() => handleSelect(u)}
style={{
display: "flex",
justifyContent: "space-between",
width: "100%",
textAlign: "left",
padding: 12,
background: "#fff",
border: 0,
cursor: "pointer",
}}
>
<span>{u.name || u.id}</span>
<span style={{ color: "#999" }}>{u.id}</span>
</button>
</li>
))}
</ul>
)}
</div>
</div>
</div>
);
}