Spaces:
Sleeping
Sleeping
Update frontend/src/services/auth.js
Browse files
frontend/src/services/auth.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
/**
|
| 2 |
-
* Authentication service for
|
| 3 |
*/
|
| 4 |
|
| 5 |
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
|
|
@@ -25,7 +25,75 @@ export async function getCurrentUser() {
|
|
| 25 |
if (response.status === 401) {
|
| 26 |
localStorage.removeItem("auth_token");
|
| 27 |
}
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
return await response.json();
|
|
|
|
| 1 |
/**
|
| 2 |
+
* Authentication service for Firebase and OTP authentication
|
| 3 |
*/
|
| 4 |
|
| 5 |
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
|
|
|
|
| 25 |
if (response.status === 401) {
|
| 26 |
localStorage.removeItem("auth_token");
|
| 27 |
}
|
| 28 |
+
const errorData = await response.json().catch(() => ({}));
|
| 29 |
+
throw new Error(errorData.detail || "Failed to get user");
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return await response.json();
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
/**
|
| 36 |
+
* Login with Firebase ID token
|
| 37 |
+
* @param {string} idToken - Firebase ID token
|
| 38 |
+
* @returns {Promise<Object>} Response with token and user
|
| 39 |
+
*/
|
| 40 |
+
export async function firebaseLogin(idToken) {
|
| 41 |
+
const response = await fetch(`${API_BASE_URL}/api/auth/firebase/login`, {
|
| 42 |
+
method: "POST",
|
| 43 |
+
headers: {
|
| 44 |
+
"Content-Type": "application/json",
|
| 45 |
+
},
|
| 46 |
+
body: JSON.stringify({ id_token: idToken }),
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
if (!response.ok) {
|
| 50 |
+
const errorData = await response.json().catch(() => ({}));
|
| 51 |
+
throw new Error(errorData.detail || "Firebase login failed");
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
return await response.json();
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/**
|
| 58 |
+
* Request OTP for email login
|
| 59 |
+
* @param {string} email - Email address
|
| 60 |
+
* @returns {Promise<Object>} Response with success message
|
| 61 |
+
*/
|
| 62 |
+
export async function requestOTP(email) {
|
| 63 |
+
const response = await fetch(`${API_BASE_URL}/api/auth/otp/request`, {
|
| 64 |
+
method: "POST",
|
| 65 |
+
headers: {
|
| 66 |
+
"Content-Type": "application/json",
|
| 67 |
+
},
|
| 68 |
+
body: JSON.stringify({ email }),
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
if (!response.ok) {
|
| 72 |
+
const errorData = await response.json().catch(() => ({}));
|
| 73 |
+
throw new Error(errorData.detail || "Failed to send OTP");
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
return await response.json();
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/**
|
| 80 |
+
* Verify OTP and login
|
| 81 |
+
* @param {string} email - Email address
|
| 82 |
+
* @param {string} otp - OTP code
|
| 83 |
+
* @returns {Promise<Object>} Response with token and user
|
| 84 |
+
*/
|
| 85 |
+
export async function verifyOTP(email, otp) {
|
| 86 |
+
const response = await fetch(`${API_BASE_URL}/api/auth/otp/verify`, {
|
| 87 |
+
method: "POST",
|
| 88 |
+
headers: {
|
| 89 |
+
"Content-Type": "application/json",
|
| 90 |
+
},
|
| 91 |
+
body: JSON.stringify({ email, otp }),
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
if (!response.ok) {
|
| 95 |
+
const errorData = await response.json().catch(() => ({}));
|
| 96 |
+
throw new Error(errorData.detail || "OTP verification failed");
|
| 97 |
}
|
| 98 |
|
| 99 |
return await response.json();
|