added reset password and google auth

This commit is contained in:
QkoSad
2022-11-30 22:09:51 +02:00
parent e31b90d9ab
commit 8cc369bfe6
4 changed files with 88 additions and 9 deletions
+42
View File
@@ -0,0 +1,42 @@
import { useLocation, useNavigate } from "react-router-dom";
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { doc, setDoc, getDoc, serverTimestamp } from "firebase/firestore";
import { db } from "../firebase.config";
import { toast } from "react-toastify";
import googleIcon from "../assets/svg/googleIcon.svg";
function OAuth() {
const navigate = useNavigate();
const location = useLocation();
const onGoogleClick = async () => {
try {
const auth = getAuth();
const provider = new GoogleAuthProvider();
const result = await signInWithPopup(auth, provider);
const user = result.user;
const docRef = doc(db, "users", user.uid);
const docSnap = await getDoc(docRef);
if (!docSnap.exists()) {
await setDoc(doc(db, "users", user.uid), {
name: user.displayName,
email: user.email,
timestamp: serverTimestamp(),
});
}
navigate('/')
} catch (error) {
toast.error('Could not authorize with google')
}
};
return (
<div className="socialLogin">
<p>Sign {location.pathname === "/sign-up" ? "up" : "in"} with</p>
<button className="socialIconDiv" onClick={onGoogleClick}>
<img className="socialIconImg" src={googleIcon} alt="google" />
</button>
</div>
);
}
export default OAuth;