Seth0330 commited on
Commit
1d68698
·
verified ·
1 Parent(s): 6d4a9a4

Create auth.js

Browse files
Files changed (1) hide show
  1. frontend/src/services/auth.js +43 -0
frontend/src/services/auth.js ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Authentication service for Google OAuth
3
+ */
4
+
5
+ const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
6
+
7
+ /**
8
+ * Get the current authenticated user
9
+ * @returns {Promise<Object>} User object
10
+ */
11
+ export async function getCurrentUser() {
12
+ const token = localStorage.getItem("auth_token");
13
+ if (!token) {
14
+ throw new Error("No token found");
15
+ }
16
+
17
+ const response = await fetch(`${API_BASE_URL}/api/auth/me`, {
18
+ method: "GET",
19
+ headers: {
20
+ Authorization: `Bearer ${token}`,
21
+ },
22
+ });
23
+
24
+ if (!response.ok) {
25
+ if (response.status === 401) {
26
+ localStorage.removeItem("auth_token");
27
+ }
28
+ throw new Error("Failed to get user");
29
+ }
30
+
31
+ return await response.json();
32
+ }
33
+
34
+ /**
35
+ * Logout the current user
36
+ * @returns {Promise<void>}
37
+ */
38
+ export async function logout() {
39
+ // For JWT tokens, logout is handled client-side by removing the token
40
+ // No server-side logout needed
41
+ return Promise.resolve();
42
+ }
43
+