Skip to content

Commit d9bcd82

Browse files
committed
Add api login and signup endpoints
1 parent da92a9e commit d9bcd82

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,23 @@ export default function LoginForm() {
1717
// handle login here
1818
const handleLogin = async (e: React.FormEvent) => {
1919
e.preventDefault();
20-
// setError('');
21-
// const apiEndpoint = 'http://localhost:4040/api/auth/login';
22-
// const result = await fetch(apiEndpoint, {
23-
// method: 'POST',
24-
// headers: {
25-
// 'Content-Type': 'application/json',
26-
// },
27-
// body: JSON.stringify({ email, password }),
28-
// });
20+
setError('');
21+
const apiEndpoint = 'http://localhost:4040/login';
22+
const result = await fetch(apiEndpoint, {
23+
method: 'POST',
24+
headers: {
25+
'Content-Type': 'application/json',
26+
},
27+
body: JSON.stringify({ email, password }),
28+
});
2929

30-
// const data = await result.json();
31-
// const message = data.message;
32-
// const isAuth = message === "Login successful";
33-
const isAuth = false;
34-
if (isAuth) {
35-
// const token = data.token;
36-
// localStorage.setItem('token', token);
37-
// go to homepage
30+
const data = await result.json();
31+
if (result.ok) {
32+
const token = data.token;
33+
localStorage.setItem('token', token);
3834
router.push('/');
3935
} else {
40-
setError('Please provide correct email and password');
36+
setError(data.error || 'Please provide correct email and password');
4137
console.error('Login failed');
4238
}
4339
};

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useRouter } from 'next/navigation';
99
import Link from 'next/link';
1010

1111
export default function SignUpPage() {
12+
const [name, setName] = useState('');
1213
const [email, setEmail] = useState('');
1314
const [password, setPassword] = useState('');
1415
const [confirmPassword, setConfirmPassword] = useState('');
@@ -18,32 +19,27 @@ export default function SignUpPage() {
1819

1920
const handleSignUp = async (e: React.FormEvent) => {
2021
e.preventDefault();
21-
// setError('');
22-
// const apiEndpoint = 'http://localhost:4040/api/auth/signup';
23-
// const result = await fetch(apiEndpoint, {
24-
// method: 'POST',
25-
// headers: {
26-
// 'Content-Type': 'application/json',
27-
// },
28-
// body: JSON.stringify({ email, password }),
29-
// });
22+
setError('');
23+
const apiEndpoint = 'http://localhost:4040/signup';
24+
const type = 'user';
25+
const result = await fetch(apiEndpoint, {
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
},
30+
body: JSON.stringify({ email, name, password, type }),
31+
});
3032

31-
// const data = await result.json();
32-
// const message = data.message;
33-
// const isAuth = message === "Login successful";
34-
const isAuth = true;
33+
const data = await result.json();
3534
if (password !== confirmPassword) {
3635
setError('Passwords do not match');
3736
return;
3837
}
39-
if (isAuth) {
40-
// const token = data.token;
41-
// localStorage.setItem('token', token);
42-
// go to homepage
38+
if (false) {
4339
router.push('/');
4440
} else {
45-
setError('Account creation failed');
46-
console.error('Login failed');
41+
setError(data.error || 'Account creation failed');
42+
console.error('Account creation failed');
4743
}
4844
};
4945

@@ -68,6 +64,15 @@ export default function SignUpPage() {
6864
</p>
6965
</div>
7066
<form onSubmit={handleSignUp} className="space-y-4">
67+
<div>
68+
<Input
69+
type="text"
70+
placeholder="Name"
71+
value={name}
72+
onChange={(e) => setName(e.target.value)}
73+
className="w-full rounded-md border border-gray-600 bg-gray-700 px-3 py-2 text-sm text-white placeholder-gray-400 focus:border-transparent focus:outline-none focus:ring-2 focus:ring-blue-500"
74+
/>
75+
</div>
7176
<div>
7277
<Input
7378
type="email"

0 commit comments

Comments
 (0)