Skip to content

Commit fa04d27

Browse files
committed
add delete account
1 parent 6018e6d commit fa04d27

File tree

2 files changed

+134
-42
lines changed

2 files changed

+134
-42
lines changed

peerprep-fe/src/app/profile/page.tsx

Lines changed: 122 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import {
1212
CardHeader,
1313
CardTitle,
1414
} from '@/components/ui/card';
15-
import { AlertCircle } from 'lucide-react';
15+
import { AlertCircle, Eye, EyeOff } from 'lucide-react';
1616
import { useAuthStore } from '@/state/useAuthStore';
1717
import { axiosClient } from '@/network/axiosClient';
18+
import { logout } from '@/lib/auth';
19+
import { useRouter } from 'next/navigation';
1820

1921
const ProfilePage = () => {
20-
const { user, setUser } = useAuthStore();
22+
const { user, setUser, clearAuth } = useAuthStore();
23+
const router = useRouter();
24+
2125
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
2226

2327
// Add form state
@@ -50,6 +54,13 @@ const ProfilePage = () => {
5054
text: string;
5155
} | null>(null);
5256

57+
// Add state for password visibility
58+
const [showPasswords, setShowPasswords] = useState({
59+
currentPassword: false,
60+
newPassword: false,
61+
confirmPassword: false,
62+
});
63+
5364
// Add function to check if form data has changed
5465
const hasProfileChanges = () => {
5566
return (
@@ -116,6 +127,10 @@ const ProfilePage = () => {
116127
},
117128
);
118129

130+
if (verifyResult.status !== 200) {
131+
throw new Error(verifyResult.data.message);
132+
}
133+
119134
const result = await axiosClient.patch(`/users/${user.id}`, {
120135
password: passwordData.newPassword,
121136
});
@@ -125,6 +140,7 @@ const ProfilePage = () => {
125140
throw new Error(result.data.message);
126141
}
127142

143+
// Sucess state
128144
setPasswordMessage({
129145
type: 'success',
130146
text: 'Password updated successfully',
@@ -149,6 +165,19 @@ const ProfilePage = () => {
149165
}
150166
};
151167

168+
const handleDeleteAccount = async () => {
169+
const res = await axiosClient.delete(`/users/${user?.id}`);
170+
if (res.status === 200) {
171+
const res = await logout();
172+
173+
if (res) {
174+
clearAuth();
175+
router.push('/');
176+
return;
177+
}
178+
}
179+
};
180+
152181
return (
153182
<div className="min-h-screen bg-gray-900 p-6 pt-24 text-gray-100">
154183
<div className="mx-auto max-w-2xl space-y-6">
@@ -219,52 +248,106 @@ const ProfilePage = () => {
219248
<Label htmlFor="current-password" className="text-slate-200">
220249
Current Password
221250
</Label>
222-
<Input
223-
id="current-password"
224-
type="password"
225-
className="border-slate-700 bg-slate-900 text-slate-200"
226-
value={passwordData.currentPassword}
227-
onChange={(e) =>
228-
setPasswordData({
229-
...passwordData,
230-
currentPassword: e.target.value,
231-
})
232-
}
233-
/>
251+
<div className="relative">
252+
<Input
253+
id="current-password"
254+
type={showPasswords.currentPassword ? 'text' : 'password'}
255+
className="border-slate-700 bg-slate-900 text-slate-200"
256+
value={passwordData.currentPassword}
257+
onChange={(e) =>
258+
setPasswordData({
259+
...passwordData,
260+
currentPassword: e.target.value,
261+
})
262+
}
263+
/>
264+
<button
265+
type="button"
266+
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-300"
267+
onClick={() =>
268+
setShowPasswords((prev) => ({
269+
...prev,
270+
currentPassword: !prev.currentPassword,
271+
}))
272+
}
273+
>
274+
{showPasswords.currentPassword ? (
275+
<EyeOff size={16} />
276+
) : (
277+
<Eye size={16} />
278+
)}
279+
</button>
280+
</div>
234281
</div>
235282
<div className="space-y-2">
236283
<Label htmlFor="new-password" className="text-slate-200">
237284
New Password
238285
</Label>
239-
<Input
240-
id="new-password"
241-
type="password"
242-
className="border-slate-700 bg-slate-900 text-slate-200"
243-
value={passwordData.newPassword}
244-
onChange={(e) =>
245-
setPasswordData({
246-
...passwordData,
247-
newPassword: e.target.value,
248-
})
249-
}
250-
/>
286+
<div className="relative">
287+
<Input
288+
id="new-password"
289+
type={showPasswords.newPassword ? 'text' : 'password'}
290+
className="border-slate-700 bg-slate-900 text-slate-200"
291+
value={passwordData.newPassword}
292+
onChange={(e) =>
293+
setPasswordData({
294+
...passwordData,
295+
newPassword: e.target.value,
296+
})
297+
}
298+
/>
299+
<button
300+
type="button"
301+
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-300"
302+
onClick={() =>
303+
setShowPasswords((prev) => ({
304+
...prev,
305+
newPassword: !prev.newPassword,
306+
}))
307+
}
308+
>
309+
{showPasswords.newPassword ? (
310+
<EyeOff size={16} />
311+
) : (
312+
<Eye size={16} />
313+
)}
314+
</button>
315+
</div>
251316
</div>
252317
<div className="space-y-2">
253318
<Label htmlFor="confirm-password" className="text-slate-200">
254319
Confirm New Password
255320
</Label>
256-
<Input
257-
id="confirm-password"
258-
type="password"
259-
className="border-slate-700 bg-slate-900 text-slate-200"
260-
value={passwordData.confirmPassword}
261-
onChange={(e) =>
262-
setPasswordData({
263-
...passwordData,
264-
confirmPassword: e.target.value,
265-
})
266-
}
267-
/>
321+
<div className="relative">
322+
<Input
323+
id="confirm-password"
324+
type={showPasswords.confirmPassword ? 'text' : 'password'}
325+
className="border-slate-700 bg-slate-900 text-slate-200"
326+
value={passwordData.confirmPassword}
327+
onChange={(e) =>
328+
setPasswordData({
329+
...passwordData,
330+
confirmPassword: e.target.value,
331+
})
332+
}
333+
/>
334+
<button
335+
type="button"
336+
className="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-300"
337+
onClick={() =>
338+
setShowPasswords((prev) => ({
339+
...prev,
340+
confirmPassword: !prev.confirmPassword,
341+
}))
342+
}
343+
>
344+
{showPasswords.confirmPassword ? (
345+
<EyeOff size={16} />
346+
) : (
347+
<Eye size={16} />
348+
)}
349+
</button>
350+
</div>
268351
</div>
269352
<Button
270353
type="submit"
@@ -313,6 +396,7 @@ const ProfilePage = () => {
313396
<Button
314397
variant="destructive"
315398
className="bg-red-600 hover:bg-red-700"
399+
onClick={handleDeleteAccount}
316400
>
317401
Yes, delete my account
318402
</Button>

peerprep-fe/src/app/signup/page.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { axiosClient } from '@/network/axiosClient';
88
import { useRouter } from 'next/navigation';
99
import { useAuthStore } from '@/state/useAuthStore';
1010
import Link from 'next/link';
11+
import { login } from '@/lib/auth';
1112

1213
export default function SignUpPage() {
1314
const [name, setName] = useState('');
@@ -36,15 +37,22 @@ export default function SignUpPage() {
3637
});
3738

3839
if (result.request.status === 201) {
39-
//Auto login after accoutn creation
40+
// Auto login after account creation
4041
const loginResult = await axiosClient.post('/auth/login', {
4142
email: email,
4243
password: password,
4344
});
4445
const data = loginResult.data.data;
45-
const token = data.accessToken;
46-
setAuth(true, token, data);
47-
router.push('/');
46+
if (loginResult.status === 200) {
47+
const token = data.accessToken;
48+
const res = await login(token);
49+
if (res) {
50+
setAuth(true, token, data);
51+
router.push('/');
52+
return;
53+
}
54+
}
55+
setError(data.error || 'Unable to create account');
4856
} else {
4957
setError('Username or Email already exists');
5058
console.error('Sign up failed');

0 commit comments

Comments
 (0)