Spaces:
Sleeping
Sleeping
Girish Jeswani commited on
Commit ·
d62d909
1
Parent(s): 2b3e7b8
add auth
Browse files
phd-advisor-frontend/src/components/Login.js
CHANGED
|
@@ -53,13 +53,27 @@ const Login = ({ onNavigateToSignup, onNavigateToHome }) => {
|
|
| 53 |
setIsLoading(true);
|
| 54 |
|
| 55 |
try {
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
} catch (error) {
|
| 65 |
console.error('Login error:', error);
|
|
|
|
| 53 |
setIsLoading(true);
|
| 54 |
|
| 55 |
try {
|
| 56 |
+
const response = await fetch('http://localhost:8000/auth/login', {
|
| 57 |
+
method: 'POST',
|
| 58 |
+
headers: {
|
| 59 |
+
'Content-Type': 'application/json',
|
| 60 |
+
},
|
| 61 |
+
body: JSON.stringify({
|
| 62 |
+
email: formData.email,
|
| 63 |
+
password: formData.password
|
| 64 |
+
}),
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
const data = await response.json();
|
| 68 |
+
|
| 69 |
+
if (response.ok) {
|
| 70 |
+
// Store the token and user info
|
| 71 |
+
localStorage.setItem('authToken', data.access_token);
|
| 72 |
+
localStorage.setItem('user', JSON.stringify(data.user));
|
| 73 |
+
onNavigateToHome?.(data.user, data.access_token);
|
| 74 |
+
} else {
|
| 75 |
+
setErrors({ submit: data.detail || 'Login failed. Please try again.' });
|
| 76 |
+
}
|
| 77 |
|
| 78 |
} catch (error) {
|
| 79 |
console.error('Login error:', error);
|
phd-advisor-frontend/src/components/Signup.js
CHANGED
|
@@ -91,17 +91,35 @@ const Signup = ({ onNavigateToLogin, onNavigateToHome }) => {
|
|
| 91 |
setIsLoading(true);
|
| 92 |
|
| 93 |
try {
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
} catch (error) {
|
| 103 |
console.error('Signup error:', error);
|
| 104 |
-
setErrors({ submit: '
|
| 105 |
} finally {
|
| 106 |
setIsLoading(false);
|
| 107 |
}
|
|
|
|
| 91 |
setIsLoading(true);
|
| 92 |
|
| 93 |
try {
|
| 94 |
+
const response = await fetch('http://localhost:8000/auth/signup', {
|
| 95 |
+
method: 'POST',
|
| 96 |
+
headers: {
|
| 97 |
+
'Content-Type': 'application/json',
|
| 98 |
+
},
|
| 99 |
+
body: JSON.stringify({
|
| 100 |
+
firstName: formData.firstName,
|
| 101 |
+
lastName: formData.lastName,
|
| 102 |
+
email: formData.email,
|
| 103 |
+
password: formData.password,
|
| 104 |
+
academicStage: formData.academicStage,
|
| 105 |
+
researchArea: formData.researchArea
|
| 106 |
+
}),
|
| 107 |
+
});
|
| 108 |
+
|
| 109 |
+
const data = await response.json();
|
| 110 |
+
|
| 111 |
+
if (response.ok) {
|
| 112 |
+
// Store the token and user info
|
| 113 |
+
localStorage.setItem('authToken', data.access_token);
|
| 114 |
+
localStorage.setItem('user', JSON.stringify(data.user));
|
| 115 |
+
onNavigateToHome?.(data.user, data.access_token);
|
| 116 |
+
} else {
|
| 117 |
+
setErrors({ submit: data.detail || 'Signup failed. Please try again.' });
|
| 118 |
+
}
|
| 119 |
|
| 120 |
} catch (error) {
|
| 121 |
console.error('Signup error:', error);
|
| 122 |
+
setErrors({ submit: 'Signup failed. Please try again.' });
|
| 123 |
} finally {
|
| 124 |
setIsLoading(false);
|
| 125 |
}
|