Skip to content

Commit

Permalink
Add Google Sign in option (#1181)
Browse files Browse the repository at this point in the history
Added Google sign in as a possible sign-in option. This eliminates the
need for verifying email, if user chooses this sign-in option.
This means less clicks to get a new user to play a game.
  • Loading branch information
DrSharky authored Sep 22, 2023
1 parent b88679e commit aadb26d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
Binary file removed react_main/public/images/icons/google-padding.png
Binary file not shown.
Binary file removed react_main/public/images/icons/google.png
Binary file not shown.
Binary file added react_main/public/images/icons/google.webp
Binary file not shown.
11 changes: 11 additions & 0 deletions react_main/src/css/auth.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
margin-bottom: 0px;
}

.auth-btn.google {
margin-top: 15px;
margin-bottom: 15px;
}

.auth-btn.google img {
width: 25px;
height: 25px;
margin-right: 10px;
}

.auth .modal .form .field-wrapper {
margin-bottom: 0px;
}
Expand Down
34 changes: 30 additions & 4 deletions react_main/src/pages/Auth/Auth.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { useContext, useEffect } from "react";
import React, { useContext, useState, useEffect } from "react";
import { Redirect, Switch, Route, useLocation } from "react-router-dom";
import { initializeApp } from "firebase/app";
import { getAuth, inMemoryPersistence } from "firebase/auth";
import { getAuth, inMemoryPersistence, getRedirectResult, GoogleAuthProvider } from "firebase/auth";
import axios from "axios";
import { useErrorAlert } from "../../components/Alerts";

import LogIn from "./LogIn";
import SignUp from "./SignUp";
Expand All @@ -13,7 +15,9 @@ import "../../css/auth.css";

export default function Auth() {
const user = useContext(UserContext);
const [loading, setLoading] = useState(true);
const location = useLocation();
const errorAlert = useErrorAlert();
const links = [
{
text: "Log In",
Expand Down Expand Up @@ -42,11 +46,33 @@ export default function Auth() {
appId: process.env.REACT_APP_FIREBASE_APP_ID,
measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID,
};

initializeApp(fbConfig);
getAuth().setPersistence(inMemoryPersistence);
const auth = getAuth();
auth.setPersistence(inMemoryPersistence);

getRedirectResult(auth)
.then(async(result) => {
if (result && result.user) {
const credential = GoogleAuthProvider.credentialFromResult(result);
const idToken = await auth.currentUser.getIdToken(true);
axios
.post("/auth", { idToken })
.then(() => {
window.location.reload();
})
.catch((e) => {
errorAlert(e);
setLoading(false);
});
}
else{
setLoading(false);
}
});
}, []);

if (!user.loaded) return <LoadingPage />;
if (!user.loaded || loading) return <LoadingPage />;
else if (user.loggedIn) return <Redirect to="/play" />;

return (
Expand Down
33 changes: 33 additions & 0 deletions react_main/src/pages/Auth/LogIn.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
signInWithEmailAndPassword,
sendPasswordResetEmail,
sendEmailVerification,
GoogleAuthProvider,
signInWithRedirect,
} from "firebase/auth";
import axios from "axios";

Expand All @@ -22,6 +24,7 @@ export default function LogIn() {
const [showResetPw, setShowResetPw] = useState(false);
const siteInfo = useContext(SiteInfoContext);
const errorAlert = useErrorAlert();
const googleProvider = new GoogleAuthProvider();

useEffect(() => {
document.title = "Log In | UltiMafia";
Expand Down Expand Up @@ -87,6 +90,24 @@ export default function LogIn() {
}
}

async function googleSubmit(e) {
try {
e.preventDefault();
setLoading(true);
await verifyRecaptcha("auth");
await signInWithRedirect(getAuth(), googleProvider);
} catch (e) {
setLoading(false);
if (!e || !e.message) return;

if (e.message.indexOf("(auth/too-many-requests)") !== -1)
errorAlert(
"Too many login attempts on this account. Please try again later."
);
else errorAlert("Failed to login. Please check your account details.");
}
}

const modalContent = (
<form className="form" onSubmit={onResetPw}>
<div className="field-wrapper">
Expand Down Expand Up @@ -143,6 +164,16 @@ export default function LogIn() {
value="Log In"
/>
</form>

<div className="or">or</div>

<div
className="auth-btn google"
onClick={googleSubmit}>
<img src="/images/icons/google.webp" alt="Google" />
Log In with Google
</div>

<div className="legal">
By logging in you agree to follow our{" "}
<Link to="/legal/tos">Terms of Service </Link>
Expand All @@ -157,5 +188,7 @@ export default function LogIn() {
footer={modalFooter}
/>
</div>


);
}
31 changes: 31 additions & 0 deletions react_main/src/pages/Auth/SignUp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
createUserWithEmailAndPassword,
getAuth,
sendEmailVerification,
signInWithRedirect,
GoogleAuthProvider,
} from "firebase/auth";

import LoadingPage from "../Loading";
Expand All @@ -20,6 +22,7 @@ export default function SignUp() {
const [signedUp, setSignedUp] = useState(false);
const siteInfo = useContext(SiteInfoContext);
const errorAlert = useErrorAlert();
const googleProvider = new GoogleAuthProvider();

const allowedEmailDomans = JSON.parse(process.env.REACT_APP_EMAIL_DOMAINS);

Expand Down Expand Up @@ -86,6 +89,24 @@ export default function SignUp() {

if (signedUp) return <Redirect to="/auth/login" />;

async function googleSubmit(e) {
try {
e.preventDefault();
setLoading(true);
await verifyRecaptcha("auth");
await signInWithRedirect(getAuth(), googleProvider);
} catch (e) {
setLoading(false);
if (!e || !e.message) return;

if (e.message.indexOf("(auth/too-many-requests)") !== -1)
errorAlert(
"Too many login attempts on this account. Please try again later."
);
else errorAlert("Failed to login. Please check your account details.");
}
}

return (
<div className="span-panel main login">
<form className="form" onSubmit={onSubmit}>
Expand Down Expand Up @@ -119,6 +140,16 @@ export default function SignUp() {
value="Sign Up"
/>
</form>

<div className="or">or</div>

<div
className="auth-btn google"
onClick={googleSubmit}>
<img src="/images/icons/google.webp" alt="Google" />
Sign Up with Google
</div>

<div className="legal">
By signing up you agree to follow our{" "}
<Link to="/legal/tos">Terms of Service </Link>
Expand Down

0 comments on commit aadb26d

Please sign in to comment.