Hkoradiya123 commited on
Commit
18acdfd
·
1 Parent(s): 7e6ee3b

Fix settings password change flow

Browse files
Backend/routes/profile.py CHANGED
@@ -39,7 +39,6 @@ def handle_profile(user_id):
39
  if 'institution_name' in data: update_data['institution_name'] = data['institution_name']
40
  if 'role' in data: update_data['role'] = data['role']
41
  if 'avatar_url' in data: update_data['avatar_url'] = data['avatar_url']
42
- if 'password' in data: update_data['password'] = data['password']
43
  if 'exam_password' in data:
44
  update_data['exam_password'] = generate_password_hash(data['exam_password'])
45
 
 
39
  if 'institution_name' in data: update_data['institution_name'] = data['institution_name']
40
  if 'role' in data: update_data['role'] = data['role']
41
  if 'avatar_url' in data: update_data['avatar_url'] = data['avatar_url']
 
42
  if 'exam_password' in data:
43
  update_data['exam_password'] = generate_password_hash(data['exam_password'])
44
 
Frontend/src/pages/Settings/Settings.jsx CHANGED
@@ -21,6 +21,12 @@ export default function Settings() {
21
  const [examPassword, setExamPassword] = useState("");
22
  const [confirmExamPassword, setConfirmExamPassword] = useState("");
23
  const [isSavingExamPassword, setIsSavingExamPassword] = useState(false);
 
 
 
 
 
 
24
 
25
  const { addToast } = useToast();
26
 
@@ -169,6 +175,48 @@ export default function Settings() {
169
  }
170
  };
171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  const tabs = [
173
  { id: "profile", label: "Profile", icon: User },
174
  { id: "security", label: "Security", icon: Lock },
@@ -280,12 +328,29 @@ export default function Settings() {
280
  <CardDescription>Update your password to keep your account secure.</CardDescription>
281
  </CardHeader>
282
  <CardContent className="space-y-4 pt-4">
283
- <Input label="Current Password" type="password" />
284
- <Input label="New Password" type="password" />
285
- <Input label="Confirm New Password" type="password" />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  </CardContent>
287
  <CardFooter className="bg-muted/10 border-t justify-end py-4">
288
- <Button>Update Password</Button>
 
 
289
  </CardFooter>
290
  </Card>
291
 
 
21
  const [examPassword, setExamPassword] = useState("");
22
  const [confirmExamPassword, setConfirmExamPassword] = useState("");
23
  const [isSavingExamPassword, setIsSavingExamPassword] = useState(false);
24
+
25
+ // Account Password States
26
+ const [currentPassword, setCurrentPassword] = useState("");
27
+ const [newPassword, setNewPassword] = useState("");
28
+ const [confirmNewPassword, setConfirmNewPassword] = useState("");
29
+ const [isSavingPassword, setIsSavingPassword] = useState(false);
30
 
31
  const { addToast } = useToast();
32
 
 
175
  }
176
  };
177
 
178
+ const handleUpdatePassword = async () => {
179
+ if (!currentPassword || !newPassword || !confirmNewPassword) {
180
+ addToast({ title: "Missing Fields", description: "Please fill all password fields.", variant: "destructive" });
181
+ return;
182
+ }
183
+ if (newPassword.length < 8) {
184
+ addToast({ title: "Weak Password", description: "New password must be at least 8 characters.", variant: "destructive" });
185
+ return;
186
+ }
187
+ if (newPassword !== confirmNewPassword) {
188
+ addToast({ title: "Mismatch", description: "New passwords do not match.", variant: "destructive" });
189
+ return;
190
+ }
191
+
192
+ setIsSavingPassword(true);
193
+ try {
194
+ const { data: { session } } = await supabase.auth.getSession();
195
+ const email = session?.user?.email;
196
+ if (!email) throw new Error("Missing user email for password update.");
197
+
198
+ const { error: reauthError } = await supabase.auth.signInWithPassword({
199
+ email,
200
+ password: currentPassword
201
+ });
202
+ if (reauthError) throw new Error("Current password is incorrect.");
203
+
204
+ const { error: updateError } = await supabase.auth.updateUser({
205
+ password: newPassword
206
+ });
207
+ if (updateError) throw new Error(updateError.message || "Failed to update password.");
208
+
209
+ addToast({ title: "Password Updated", description: "Your password has been changed successfully.", variant: "success" });
210
+ setCurrentPassword("");
211
+ setNewPassword("");
212
+ setConfirmNewPassword("");
213
+ } catch (error) {
214
+ addToast({ title: "Error", description: error.message, variant: "destructive" });
215
+ } finally {
216
+ setIsSavingPassword(false);
217
+ }
218
+ };
219
+
220
  const tabs = [
221
  { id: "profile", label: "Profile", icon: User },
222
  { id: "security", label: "Security", icon: Lock },
 
328
  <CardDescription>Update your password to keep your account secure.</CardDescription>
329
  </CardHeader>
330
  <CardContent className="space-y-4 pt-4">
331
+ <Input
332
+ label="Current Password"
333
+ type="password"
334
+ value={currentPassword}
335
+ onChange={(e) => setCurrentPassword(e.target.value)}
336
+ />
337
+ <Input
338
+ label="New Password"
339
+ type="password"
340
+ value={newPassword}
341
+ onChange={(e) => setNewPassword(e.target.value)}
342
+ />
343
+ <Input
344
+ label="Confirm New Password"
345
+ type="password"
346
+ value={confirmNewPassword}
347
+ onChange={(e) => setConfirmNewPassword(e.target.value)}
348
+ />
349
  </CardContent>
350
  <CardFooter className="bg-muted/10 border-t justify-end py-4">
351
+ <Button onClick={handleUpdatePassword} disabled={isSavingPassword}>
352
+ {isSavingPassword ? "Updating..." : "Update Password"}
353
+ </Button>
354
  </CardFooter>
355
  </Card>
356