forked from Gaurav-Gosain/nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_app.js
41 lines (37 loc) · 1.04 KB
/
_app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import "../styles/globals.css";
import { StyledEngineProvider } from "@mui/material/styles";
import { useEffect } from "react";
import { useRouter } from "next/router";
import { useAuthState } from "react-firebase-hooks/auth";
import { auth, db } from "../components/Auth/firebaseAuth";
import { doc, setDoc } from "firebase/firestore";
function MyApp({ Component, pageProps }) {
const router = useRouter();
const [user, loading, error] = useAuthState(auth);
useEffect(() => {
if (user === null) {
router.push("/login");
} else if (user) {
console.log(user);
const userRef = doc(db, "users", user.uid);
setDoc(
userRef,
{
displayName: user.displayName,
email: user.email,
photoURL: user.photoURL,
uid: user.uid,
},
{ merge: true }
).then(() => {
router.push("/");
});
}
}, [user]);
return (
<StyledEngineProvider injectFirst>
<Component {...pageProps} />
</StyledEngineProvider>
);
}
export default MyApp;