Skip to content

Commit 42e4624

Browse files
committed
fix error handling for signin and signup
1 parent fa04d27 commit 42e4624

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,29 @@ export default function LoginForm({ searchParams }: Props) {
3535
const handleLogin = async (e: React.FormEvent) => {
3636
e.preventDefault();
3737
setError('');
38-
const result = await axiosClient.post('/auth/login', {
39-
email: email,
40-
password: password,
41-
});
4238

43-
const data = result.data.data;
44-
if (result.status === 200) {
45-
const token = data.accessToken;
46-
const res = await login(token);
47-
if (res) {
48-
setAuth(true, token, data);
49-
router.push('/');
50-
return;
39+
try {
40+
const result = await axiosClient.post('/auth/login', {
41+
email: email,
42+
password: password,
43+
});
44+
45+
const data = result.data.data;
46+
if (result.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+
}
5154
}
55+
} catch (error: unknown) {
56+
const message =
57+
(error as { response?: { data?: { message?: string } } })?.response
58+
?.data?.message || 'Please provide correct email and password';
59+
setError(message);
5260
}
53-
setError(data.error || 'Please provide correct email and password');
5461
};
5562

5663
const handleGithubLogin = () => {

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

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,51 @@ export default function SignUpPage() {
2626

2727
// const type = 'user';
2828
if (password !== confirmPassword) {
29-
router.push('/signup');
3029
setError('Passwords do not match');
3130
return;
3231
}
33-
const result = await axiosClient.post('/users', {
34-
username: name,
35-
email: email,
36-
password: password,
37-
});
3832

39-
if (result.request.status === 201) {
33+
if (!agreeTerms) {
34+
setError('Please agree to the Terms of Service and Privacy Policy');
35+
return;
36+
}
37+
38+
try {
39+
const result = await axiosClient.post('/users', {
40+
username: name,
41+
email: email,
42+
password: password,
43+
});
44+
45+
if (result.request.status !== 201) {
46+
setError('Username or Email already exists');
47+
return;
48+
}
49+
4050
// Auto login after account creation
4151
const loginResult = await axiosClient.post('/auth/login', {
4252
email: email,
4353
password: password,
4454
});
55+
if (loginResult.request.status !== 200) {
56+
setError('Unable to login');
57+
return;
58+
}
59+
4560
const data = loginResult.data.data;
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-
}
61+
62+
const token = data.accessToken;
63+
const res = await login(token);
64+
if (res) {
65+
setAuth(true, token, data);
66+
router.push('/');
67+
return;
5468
}
55-
setError(data.error || 'Unable to create account');
56-
} else {
57-
setError('Username or Email already exists');
58-
console.error('Sign up failed');
69+
} catch (error: unknown) {
70+
const message =
71+
(error as { response?: { data?: { message?: string } } })?.response
72+
?.data?.message || 'Username or Email already exists';
73+
setError(message);
5974
}
6075
};
6176

0 commit comments

Comments
 (0)