Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 36 additions & 27 deletions app/src/Context/AuthContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,27 @@ export const AuthProvider = ({ children }) => {
navigate("/register");
};
const SignInWithGoogle = async () => {
let response;
try {
if (!auth) {
console.error("Firebase auth not initialized. Check your .env.frontend file.");
alert("Firebase configuration error. Please check console.");
return null;
alert("Firebase is not configured. Check frontend env values.");
return false;
}
response = await signInWithPopup(auth, googleProvider);
const response = await signInWithPopup(auth, googleProvider);
if (response && !(response["status"] === 400)) {
const credential = GoogleAuthProvider.credentialFromResult(response);
const accessToken =
credential?.accessToken || response?.user?.accessToken;
if (!accessToken) {
alert("Google login failed: missing access token.");
return false;
}
let logresponse = await fetch(BACKEND + "/api/token/google/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: response.user.accessToken,
token: accessToken,
Comment thread
VarshiniGunti marked this conversation as resolved.
}),
});
let data = await readJsonSafely(logresponse);
Expand All @@ -236,37 +241,42 @@ export const AuthProvider = ({ children }) => {
localStorage.setItem("authTokens", JSON.stringify(token));
let usernames_data = await getUsernamesData(token);
setUserNames(usernames_data);
navigate("/");
return true;
} else {
console.error("Backend token error:", data);
alert(data?.message || "Backend error during login");
alert(data?.message || "Google login failed.");
return false;
}
} else {
console.log("Google sign-in was cancelled or failed");
return false;
}
} catch (error) {
console.error("Google login error:", error);
alert("Google login failed: " + error.message);
console.error("Google login failed:", error);
alert("Please try logging in again");
return false;
}
return response;
};
const SignUpWithGoogle = async () => {
let response;
try {
if (!auth) {
console.error("Firebase auth not initialized. Check your .env.frontend file.");
alert("Firebase configuration error. Please check console.");
return null;
alert("Firebase is not configured. Check frontend env values.");
return false;
}
response = await signInWithPopup(auth, googleProvider);
const response = await signInWithPopup(auth, googleProvider);
if (response && !(response["status"] === 400)) {
const credential = GoogleAuthProvider.credentialFromResult(response);
const accessToken =
credential?.accessToken || response?.user?.accessToken;
if (!accessToken) {
alert("Google registration failed: missing access token.");
return false;
}
let regresponse = await fetch(BACKEND + "/api/register/google/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: response.user.accessToken,
token: accessToken,
Comment thread
VarshiniGunti marked this conversation as resolved.
username: response.user.email.split("@")[0],
}),
});
Expand All @@ -278,20 +288,19 @@ export const AuthProvider = ({ children }) => {
localStorage.setItem("authTokens", JSON.stringify(token));
let usernames_data = await getUsernamesData(token);
setUserNames(usernames_data);
navigate("/");
return true;
} else {
console.error("Backend registration error:", data);
alert("Please try registering again: " + (data?.message || ""));
alert("Please try registering again");
return false;
}
console.log(response);
} else {
alert("Google registration was cancelled");
return false;
}
} catch (error) {
console.error("Google signup error:", error);
alert("Google signup failed: " + error.message);
console.error("Google signup failed:", error);
alert("Please try registering again");
return false;
}
return response;
};
let contextData = {
user: user,
Expand Down
11 changes: 7 additions & 4 deletions app/src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ const Login = () => {

async function handleGoogleAuth(e) {
e.preventDefault();
await SignInWithGoogle();
navigate("/");
}
const success = await SignInWithGoogle();
if (success) {
navigate("/");
}
}

if (loading) {
return (
<div
Expand Down Expand Up @@ -130,4 +133,4 @@ const Login = () => {
</div>
);
};
export default Login;
export default Login;
6 changes: 4 additions & 2 deletions app/src/components/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ const Register = () => {

const handleGoogleRegister = async (e) => {
e.preventDefault();
await SignUpWithGoogle();
navigate("/profile");
const success = await SignUpWithGoogle();
if (success) {
navigate("/profile");
}
};
return (
<div
Expand Down
Loading