Update frontend/src/contexts/AuthContext.jsx
Browse files
frontend/src/contexts/AuthContext.jsx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import React, { createContext, useContext, useState, useEffect } from "react";
|
| 2 |
-
import {
|
|
|
|
|
|
|
| 3 |
|
| 4 |
const AuthContext = createContext(null);
|
| 5 |
|
|
@@ -31,14 +33,47 @@ export function AuthProvider({ children }) {
|
|
| 31 |
}
|
| 32 |
};
|
| 33 |
|
| 34 |
-
const
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
};
|
| 39 |
|
| 40 |
const handleLogout = async () => {
|
| 41 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
await apiLogout();
|
| 43 |
} catch (error) {
|
| 44 |
console.error("Logout error:", error);
|
|
@@ -59,7 +94,9 @@ export function AuthProvider({ children }) {
|
|
| 59 |
user,
|
| 60 |
token,
|
| 61 |
loading,
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
logout: handleLogout,
|
| 64 |
handleAuthCallback,
|
| 65 |
isAuthenticated: !!user,
|
|
|
|
| 1 |
import React, { createContext, useContext, useState, useEffect } from "react";
|
| 2 |
+
import { signInWithPopup, signOut as firebaseSignOut } from "firebase/auth";
|
| 3 |
+
import { auth, googleProvider } from "@/config/firebase";
|
| 4 |
+
import { getCurrentUser, firebaseLogin, requestOTP, verifyOTP, logout as apiLogout } from "@/services/auth";
|
| 5 |
|
| 6 |
const AuthContext = createContext(null);
|
| 7 |
|
|
|
|
| 33 |
}
|
| 34 |
};
|
| 35 |
|
| 36 |
+
const handleFirebaseLogin = async () => {
|
| 37 |
+
try {
|
| 38 |
+
const result = await signInWithPopup(auth, googleProvider);
|
| 39 |
+
const idToken = await result.user.getIdToken();
|
| 40 |
+
const response = await firebaseLogin(idToken);
|
| 41 |
+
handleAuthCallback(response.token);
|
| 42 |
+
} catch (error) {
|
| 43 |
+
if (error.code === 'auth/popup-closed' || error.code === 'auth/cancelled-popup-request') {
|
| 44 |
+
// User closed popup or cancelled - don't show error
|
| 45 |
+
return;
|
| 46 |
+
}
|
| 47 |
+
console.error("Firebase login error:", error);
|
| 48 |
+
throw new Error(error.message || "Firebase authentication failed");
|
| 49 |
+
}
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
const handleOTPRequest = async (email) => {
|
| 53 |
+
try {
|
| 54 |
+
await requestOTP(email);
|
| 55 |
+
} catch (error) {
|
| 56 |
+
console.error("OTP request error:", error);
|
| 57 |
+
throw error;
|
| 58 |
+
}
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
const handleOTPVerify = async (email, otp) => {
|
| 62 |
+
try {
|
| 63 |
+
const response = await verifyOTP(email, otp);
|
| 64 |
+
handleAuthCallback(response.token);
|
| 65 |
+
} catch (error) {
|
| 66 |
+
console.error("OTP verify error:", error);
|
| 67 |
+
throw error;
|
| 68 |
+
}
|
| 69 |
};
|
| 70 |
|
| 71 |
const handleLogout = async () => {
|
| 72 |
try {
|
| 73 |
+
// Sign out from Firebase if user was using Firebase auth
|
| 74 |
+
if (auth.currentUser) {
|
| 75 |
+
await firebaseSignOut(auth);
|
| 76 |
+
}
|
| 77 |
await apiLogout();
|
| 78 |
} catch (error) {
|
| 79 |
console.error("Logout error:", error);
|
|
|
|
| 94 |
user,
|
| 95 |
token,
|
| 96 |
loading,
|
| 97 |
+
firebaseLogin: handleFirebaseLogin,
|
| 98 |
+
requestOTP: handleOTPRequest,
|
| 99 |
+
verifyOTP: handleOTPVerify,
|
| 100 |
logout: handleLogout,
|
| 101 |
handleAuthCallback,
|
| 102 |
isAuthenticated: !!user,
|