@lastbrain/module-auth 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,381 @@
1
+ "use client";
2
+
3
+ import { useEffect, useState } from "react";
4
+ import {
5
+ Card,
6
+ CardBody,
7
+ CardHeader,
8
+ Input,
9
+ Textarea,
10
+ Button,
11
+ Spinner,
12
+ Divider,
13
+ addToast,
14
+ AvatarUploader,
15
+ } from "@lastbrain/ui";
16
+ import { Save, User } from "lucide-react";
17
+ import { uploadFile, deleteFilesWithPrefix } from "../../api/storage.js";
18
+ import { supabaseBrowserClient } from "@lastbrain/core";
19
+
20
+ interface ProfileData {
21
+ first_name?: string;
22
+ last_name?: string;
23
+ avatar_url?: string;
24
+ bio?: string;
25
+ phone?: string;
26
+ company?: string;
27
+ website?: string;
28
+ location?: string;
29
+ language?: string;
30
+ timezone?: string;
31
+ }
32
+
33
+ interface UserMetadata {
34
+ avatar?: string | null;
35
+ avatar_sizes?: {
36
+ small?: string | null;
37
+ medium?: string | null;
38
+ large?: string | null;
39
+ } | null;
40
+ }
41
+
1
42
  export function ProfilePage() {
2
- return <div className="pt-12">Welcome to your Profile!</div>;
43
+ const [profile, setProfile] = useState<ProfileData>({});
44
+ const [isLoading, setIsLoading] = useState(true);
45
+ const [isSaving, setIsSaving] = useState(false);
46
+ const [error, setError] = useState<string | null>(null);
47
+ const [currentUser, setCurrentUser] = useState<any>(null);
48
+
49
+ useEffect(() => {
50
+ fetchProfile();
51
+ fetchCurrentUser();
52
+ }, []);
53
+
54
+ const fetchCurrentUser = async () => {
55
+ try {
56
+ const {
57
+ data: { user },
58
+ } = await supabaseBrowserClient.auth.getUser();
59
+ setCurrentUser(user);
60
+ } catch (err) {
61
+ console.error("Error fetching current user:", err);
62
+ }
63
+ };
64
+
65
+ const fetchProfile = async () => {
66
+ try {
67
+ setIsLoading(true);
68
+ const response = await fetch("/api/auth/profile");
69
+
70
+ if (!response.ok) {
71
+ throw new Error("Failed to fetch profile");
72
+ }
73
+
74
+ const result = await response.json();
75
+ if (result.data) {
76
+ setProfile(result.data);
77
+ }
78
+ } catch (err) {
79
+ setError(err instanceof Error ? err.message : "An error occurred");
80
+ addToast({
81
+ title: "Error",
82
+ description: "Failed to load profile",
83
+ color: "danger",
84
+ });
85
+ } finally {
86
+ setIsLoading(false);
87
+ }
88
+ };
89
+
90
+ const handleSubmit = async (e: React.FormEvent) => {
91
+ e.preventDefault();
92
+ setIsSaving(true);
93
+
94
+ try {
95
+ const response = await fetch("/api/auth/profile", {
96
+ method: "PUT",
97
+ headers: {
98
+ "Content-Type": "application/json",
99
+ },
100
+ body: JSON.stringify(profile),
101
+ });
102
+
103
+ if (!response.ok) {
104
+ throw new Error("Failed to update profile");
105
+ }
106
+
107
+ addToast({
108
+ title: "Success",
109
+ description: "Profile updated successfully",
110
+ color: "success",
111
+ });
112
+ } catch (err) {
113
+ console.error("Error updating profile:", err);
114
+ setError(err instanceof Error ? err.message : "An error occurred");
115
+ addToast({
116
+ title: "Error",
117
+ description: "Failed to update profile",
118
+ color: "danger",
119
+ });
120
+ } finally {
121
+ setIsSaving(false);
122
+ }
123
+ };
124
+
125
+ const handleChange = (field: keyof ProfileData, value: string) => {
126
+ setProfile((prev) => ({ ...prev, [field]: value }));
127
+ };
128
+
129
+ const handleAvatarUpload = async (files: {
130
+ small: Blob;
131
+ medium: Blob;
132
+ large: Blob;
133
+ }) => {
134
+ if (!currentUser) throw new Error("User not authenticated");
135
+
136
+ const version = Date.now();
137
+ const urls = {
138
+ small: "",
139
+ medium: "",
140
+ large: "",
141
+ };
142
+
143
+ // Upload all three sizes
144
+ urls.small = await uploadFile(
145
+ "avatar",
146
+ `${currentUser.id}_32_${version}.webp`,
147
+ files.small,
148
+ "image/webp"
149
+ );
150
+ urls.medium = await uploadFile(
151
+ "avatar",
152
+ `${currentUser.id}_64_${version}.webp`,
153
+ files.medium,
154
+ "image/webp"
155
+ );
156
+ urls.large = await uploadFile(
157
+ "avatar",
158
+ `${currentUser.id}_128_${version}.webp`,
159
+ files.large,
160
+ "image/webp"
161
+ );
162
+
163
+ // Update user metadata
164
+ await supabaseBrowserClient.auth.updateUser({
165
+ data: {
166
+ avatar: `avatar/${currentUser.id}_128_${version}.webp`,
167
+ avatar_sizes: {
168
+ small: `avatar/${currentUser.id}_32_${version}.webp`,
169
+ medium: `avatar/${currentUser.id}_64_${version}.webp`,
170
+ large: `avatar/${currentUser.id}_128_${version}.webp`,
171
+ },
172
+ },
173
+ });
174
+
175
+ // Update profile avatar_url
176
+ setProfile((prev) => ({ ...prev, avatar_url: urls.large }));
177
+
178
+ return urls;
179
+ };
180
+
181
+ const handleAvatarDelete = async () => {
182
+ if (!currentUser) throw new Error("User not authenticated");
183
+
184
+ // Delete old files
185
+ await deleteFilesWithPrefix("avatar", currentUser.id);
186
+
187
+ // Update user metadata
188
+ await supabaseBrowserClient.auth.updateUser({
189
+ data: {
190
+ avatar: null,
191
+ avatar_sizes: {
192
+ small: null,
193
+ medium: null,
194
+ large: null,
195
+ },
196
+ },
197
+ });
198
+
199
+ // Update profile
200
+ setProfile((prev) => ({ ...prev, avatar_url: "" }));
201
+ };
202
+
203
+ if (isLoading) {
204
+ return (
205
+ <div className="flex justify-center items-center min-h-[400px]">
206
+ <Spinner size="lg" label="Loading profile..." />
207
+ </div>
208
+ );
209
+ }
210
+
211
+ return (
212
+ <div className="pt-12 pb-12 max-w-4xl mx-auto px-4">
213
+ <div className="flex items-center gap-2 mb-8">
214
+ <User className="w-8 h-8" />
215
+ <h1 className="text-3xl font-bold">Edit Profile</h1>
216
+ </div>
217
+
218
+ <form onSubmit={handleSubmit}>
219
+ <div className="space-y-6">
220
+ {/* Avatar Section */}
221
+ <Card>
222
+ <CardHeader>
223
+ <h3 className="text-lg font-semibold">Photo de profil</h3>
224
+ </CardHeader>
225
+ <Divider />
226
+ <CardBody>
227
+ <div className="flex justify-center">
228
+ <AvatarUploader
229
+ userId={currentUser?.id}
230
+ bucket="avatar"
231
+ shape="circle"
232
+ onUpload={handleAvatarUpload}
233
+ onDelete={handleAvatarDelete}
234
+ initialAvatarPath={
235
+ (currentUser?.user_metadata as UserMetadata)?.avatar ||
236
+ profile.avatar_url ||
237
+ null
238
+ }
239
+ initialAvatarSizes={(() => {
240
+ const sizes = (currentUser?.user_metadata as UserMetadata)
241
+ ?.avatar_sizes;
242
+ if (!sizes) return null;
243
+ return {
244
+ small: sizes.small ?? null,
245
+ medium: sizes.medium ?? null,
246
+ large: sizes.large ?? null,
247
+ };
248
+ })()}
249
+ onUploaded={(urls) => {
250
+ setProfile((prev) => ({ ...prev, avatar_url: urls.large }));
251
+ }}
252
+ onDeleted={() => {
253
+ setProfile((prev) => ({ ...prev, avatar_url: "" }));
254
+ }}
255
+ />
256
+ </div>
257
+ </CardBody>
258
+ </Card>
259
+
260
+ {/* Personal Information */}
261
+ <Card>
262
+ <CardHeader>
263
+ <h3 className="text-lg font-semibold">Personal Information</h3>
264
+ </CardHeader>
265
+ <Divider />
266
+ <CardBody>
267
+ <div className="grid gap-4 md:grid-cols-2">
268
+ <Input
269
+ label="First Name"
270
+ placeholder="Enter your first name"
271
+ value={profile.first_name || ""}
272
+ onChange={(e) => handleChange("first_name", e.target.value)}
273
+ />
274
+ <Input
275
+ label="Last Name"
276
+ placeholder="Enter your last name"
277
+ value={profile.last_name || ""}
278
+ onChange={(e) => handleChange("last_name", e.target.value)}
279
+ />
280
+ <Input
281
+ label="Phone"
282
+ placeholder="Enter your phone number"
283
+ type="tel"
284
+ value={profile.phone || ""}
285
+ onChange={(e) => handleChange("phone", e.target.value)}
286
+ className="md:col-span-2"
287
+ />
288
+ <Textarea
289
+ label="Bio"
290
+ placeholder="Tell us about yourself"
291
+ value={profile.bio || ""}
292
+ onChange={(e) => handleChange("bio", e.target.value)}
293
+ minRows={3}
294
+ className="md:col-span-2"
295
+ />
296
+ </div>
297
+ </CardBody>
298
+ </Card>
299
+
300
+ {/* Professional Information */}
301
+ <Card>
302
+ <CardHeader>
303
+ <h3 className="text-lg font-semibold">
304
+ Professional Information
305
+ </h3>
306
+ </CardHeader>
307
+ <Divider />
308
+ <CardBody>
309
+ <div className="grid gap-4 md:grid-cols-2">
310
+ <Input
311
+ label="Company"
312
+ placeholder="Enter your company name"
313
+ value={profile.company || ""}
314
+ onChange={(e) => handleChange("company", e.target.value)}
315
+ />
316
+ <Input
317
+ label="Website"
318
+ placeholder="https://example.com"
319
+ type="url"
320
+ value={profile.website || ""}
321
+ onChange={(e) => handleChange("website", e.target.value)}
322
+ />
323
+ <Input
324
+ label="Location"
325
+ placeholder="City, Country"
326
+ value={profile.location || ""}
327
+ onChange={(e) => handleChange("location", e.target.value)}
328
+ className="md:col-span-2"
329
+ />
330
+ </div>
331
+ </CardBody>
332
+ </Card>
333
+
334
+ {/* Preferences */}
335
+ <Card>
336
+ <CardHeader>
337
+ <h3 className="text-lg font-semibold">Preferences</h3>
338
+ </CardHeader>
339
+ <Divider />
340
+ <CardBody>
341
+ <div className="grid gap-4 md:grid-cols-2">
342
+ <Input
343
+ label="Language"
344
+ placeholder="en, fr, es..."
345
+ value={profile.language || ""}
346
+ onChange={(e) => handleChange("language", e.target.value)}
347
+ />
348
+ <Input
349
+ label="Timezone"
350
+ placeholder="Europe/Paris, America/New_York..."
351
+ value={profile.timezone || ""}
352
+ onChange={(e) => handleChange("timezone", e.target.value)}
353
+ />
354
+ </div>
355
+ </CardBody>
356
+ </Card>
357
+
358
+ {/* Actions */}
359
+ <div className="flex justify-end gap-3">
360
+ <Button
361
+ type="button"
362
+ variant="flat"
363
+ onPress={() => fetchProfile()}
364
+ isDisabled={isSaving}
365
+ >
366
+ Cancel
367
+ </Button>
368
+ <Button
369
+ type="submit"
370
+ color="primary"
371
+ isLoading={isSaving}
372
+ startContent={!isSaving && <Save className="w-4 h-4" />}
373
+ >
374
+ {isSaving ? "Saving..." : "Save Changes"}
375
+ </Button>
376
+ </div>
377
+ </div>
378
+ </form>
379
+ </div>
380
+ );
3
381
  }
@@ -1,3 +1,304 @@
1
+ "use client";
2
+
3
+ import { useEffect, useState } from "react";
4
+ import {
5
+ Card,
6
+ CardBody,
7
+ CardHeader,
8
+ Switch,
9
+ Button,
10
+ Spinner,
11
+ Divider,
12
+ Select,
13
+ SelectItem,
14
+ addToast,
15
+ } from "@lastbrain/ui";
16
+ import { Settings, Save } from "lucide-react";
17
+
18
+ interface UserPreferences {
19
+ email_notifications?: boolean;
20
+ push_notifications?: boolean;
21
+ marketing_emails?: boolean;
22
+ theme?: string;
23
+ language?: string;
24
+ timezone?: string;
25
+ }
26
+
27
+ interface ProfileData {
28
+ language?: string;
29
+ timezone?: string;
30
+ preferences?: UserPreferences;
31
+ }
32
+
1
33
  export function ReglagePage() {
2
- return <div className="pt-12">Welcome to your Reglage!</div>;
34
+ const [preferences, setPreferences] = useState<UserPreferences>({
35
+ email_notifications: true,
36
+ push_notifications: false,
37
+ marketing_emails: false,
38
+ theme: "system",
39
+ language: "en",
40
+ timezone: "UTC",
41
+ });
42
+ const [isLoading, setIsLoading] = useState(true);
43
+ const [isSaving, setIsSaving] = useState(false);
44
+
45
+ useEffect(() => {
46
+ fetchSettings();
47
+ }, []);
48
+
49
+ const fetchSettings = async () => {
50
+ try {
51
+ setIsLoading(true);
52
+ const response = await fetch("/api/auth/profile");
53
+
54
+ if (!response.ok) {
55
+ throw new Error("Failed to fetch settings");
56
+ }
57
+
58
+ const result = await response.json();
59
+ if (result.data) {
60
+ const profile: ProfileData = result.data;
61
+ setPreferences((prev) => ({
62
+ ...prev,
63
+ language: profile.language || prev.language,
64
+ timezone: profile.timezone || prev.timezone,
65
+ ...(profile.preferences || {}),
66
+ }));
67
+ }
68
+ } catch (err) {
69
+ console.error("Error loading settings:", err);
70
+ addToast({
71
+ title: "Error",
72
+ description: "Failed to load settings",
73
+ color: "danger",
74
+ });
75
+ } finally {
76
+ setIsLoading(false);
77
+ }
78
+ };
79
+
80
+ const handleSave = async () => {
81
+ setIsSaving(true);
82
+
83
+ try {
84
+ const response = await fetch("/api/auth/profile", {
85
+ method: "PUT",
86
+ headers: {
87
+ "Content-Type": "application/json",
88
+ },
89
+ body: JSON.stringify({
90
+ language: preferences.language,
91
+ timezone: preferences.timezone,
92
+ preferences: {
93
+ email_notifications: preferences.email_notifications,
94
+ push_notifications: preferences.push_notifications,
95
+ marketing_emails: preferences.marketing_emails,
96
+ theme: preferences.theme,
97
+ },
98
+ }),
99
+ });
100
+
101
+ if (!response.ok) {
102
+ throw new Error("Failed to update settings");
103
+ }
104
+
105
+ addToast({
106
+ title: "Success",
107
+ description: "Settings updated successfully",
108
+ color: "success",
109
+ });
110
+ } catch (err) {
111
+ console.error("Error updating settings:", err);
112
+ addToast({
113
+ title: "Error",
114
+ description: "Failed to update settings",
115
+ color: "danger",
116
+ });
117
+ } finally {
118
+ setIsSaving(false);
119
+ }
120
+ };
121
+
122
+ const handleToggle = (key: keyof UserPreferences, value: boolean) => {
123
+ setPreferences((prev) => ({ ...prev, [key]: value }));
124
+ };
125
+
126
+ const handleSelect = (key: keyof UserPreferences, value: string) => {
127
+ setPreferences((prev) => ({ ...prev, [key]: value }));
128
+ };
129
+
130
+ if (isLoading) {
131
+ return (
132
+ <div className="flex justify-center items-center min-h-[400px]">
133
+ <Spinner size="lg" label="Loading settings..." />
134
+ </div>
135
+ );
136
+ }
137
+
138
+ return (
139
+ <div className="pt-12 pb-12 max-w-4xl mx-auto px-4">
140
+ <div className="flex items-center gap-2 mb-8">
141
+ <Settings className="w-8 h-8" />
142
+ <h1 className="text-3xl font-bold">Account Settings</h1>
143
+ </div>
144
+
145
+ <div className="space-y-6">
146
+ {/* Notifications */}
147
+ <Card>
148
+ <CardHeader>
149
+ <h3 className="text-lg font-semibold">Notifications</h3>
150
+ </CardHeader>
151
+ <Divider />
152
+ <CardBody>
153
+ <div className="space-y-4">
154
+ <div className="flex justify-between items-center">
155
+ <div>
156
+ <p className="font-medium">Email Notifications</p>
157
+ <p className="text-small text-default-500">
158
+ Receive email notifications for important updates
159
+ </p>
160
+ </div>
161
+ <Switch
162
+ isSelected={preferences.email_notifications}
163
+ onValueChange={(value) =>
164
+ handleToggle("email_notifications", value)
165
+ }
166
+ />
167
+ </div>
168
+ <Divider />
169
+ <div className="flex justify-between items-center">
170
+ <div>
171
+ <p className="font-medium">Push Notifications</p>
172
+ <p className="text-small text-default-500">
173
+ Receive push notifications in your browser
174
+ </p>
175
+ </div>
176
+ <Switch
177
+ isSelected={preferences.push_notifications}
178
+ onValueChange={(value) =>
179
+ handleToggle("push_notifications", value)
180
+ }
181
+ />
182
+ </div>
183
+ <Divider />
184
+ <div className="flex justify-between items-center">
185
+ <div>
186
+ <p className="font-medium">Marketing Emails</p>
187
+ <p className="text-small text-default-500">
188
+ Receive emails about new features and updates
189
+ </p>
190
+ </div>
191
+ <Switch
192
+ isSelected={preferences.marketing_emails}
193
+ onValueChange={(value) =>
194
+ handleToggle("marketing_emails", value)
195
+ }
196
+ />
197
+ </div>
198
+ </div>
199
+ </CardBody>
200
+ </Card>
201
+
202
+ {/* Appearance */}
203
+ <Card>
204
+ <CardHeader>
205
+ <h3 className="text-lg font-semibold">Appearance</h3>
206
+ </CardHeader>
207
+ <Divider />
208
+ <CardBody>
209
+ <Select
210
+ label="Theme"
211
+ placeholder="Select a theme"
212
+ selectedKeys={preferences.theme ? [preferences.theme] : []}
213
+ onChange={(e) => handleSelect("theme", e.target.value)}
214
+ >
215
+ <SelectItem key="light">
216
+ Light
217
+ </SelectItem>
218
+ <SelectItem key="dark">
219
+ Dark
220
+ </SelectItem>
221
+ <SelectItem key="system">
222
+ System
223
+ </SelectItem>
224
+ </Select>
225
+ </CardBody>
226
+ </Card>
227
+
228
+ {/* Language & Region */}
229
+ <Card>
230
+ <CardHeader>
231
+ <h3 className="text-lg font-semibold">Language & Region</h3>
232
+ </CardHeader>
233
+ <Divider />
234
+ <CardBody>
235
+ <div className="grid gap-4 md:grid-cols-2">
236
+ <Select
237
+ label="Language"
238
+ placeholder="Select a language"
239
+ selectedKeys={preferences.language ? [preferences.language] : []}
240
+ onChange={(e) => handleSelect("language", e.target.value)}
241
+ >
242
+ <SelectItem key="en">
243
+ English
244
+ </SelectItem>
245
+ <SelectItem key="fr">
246
+ Français
247
+ </SelectItem>
248
+ <SelectItem key="es">
249
+ Español
250
+ </SelectItem>
251
+ <SelectItem key="de">
252
+ Deutsch
253
+ </SelectItem>
254
+ </Select>
255
+ <Select
256
+ label="Timezone"
257
+ placeholder="Select a timezone"
258
+ selectedKeys={preferences.timezone ? [preferences.timezone] : []}
259
+ onChange={(e) => handleSelect("timezone", e.target.value)}
260
+ >
261
+ <SelectItem key="UTC">
262
+ UTC
263
+ </SelectItem>
264
+ <SelectItem key="Europe/Paris">
265
+ Europe/Paris
266
+ </SelectItem>
267
+ <SelectItem key="America/New_York">
268
+ America/New_York
269
+ </SelectItem>
270
+ <SelectItem key="America/Los_Angeles">
271
+ America/Los_Angeles
272
+ </SelectItem>
273
+ <SelectItem key="Asia/Tokyo">
274
+ Asia/Tokyo
275
+ </SelectItem>
276
+ </Select>
277
+ </div>
278
+ </CardBody>
279
+ </Card>
280
+
281
+ {/* Actions */}
282
+ <div className="flex justify-end gap-3">
283
+ <Button
284
+ type="button"
285
+ variant="flat"
286
+ onPress={() => fetchSettings()}
287
+ isDisabled={isSaving}
288
+ >
289
+ Reset
290
+ </Button>
291
+ <Button
292
+ type="button"
293
+ color="primary"
294
+ isLoading={isSaving}
295
+ onPress={handleSave}
296
+ startContent={!isSaving && <Save className="w-4 h-4" />}
297
+ >
298
+ {isSaving ? "Saving..." : "Save Settings"}
299
+ </Button>
300
+ </div>
301
+ </div>
302
+ </div>
303
+ );
3
304
  }