generated from CS3219-AY2324S1/course-assessment-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auth checker and logged in nav bar (#91)
- Loading branch information
1 parent
df277e3
commit d0b916b
Showing
4 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import { AuthContext } from "@/contexts/AuthContext"; | ||
import { getAuth, onAuthStateChanged } from "firebase/auth"; | ||
import { useContext } from "react"; | ||
|
||
interface AuthCheckerProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export default function AuthChecker({ children }: AuthCheckerProps) { | ||
const auth = getAuth(); | ||
const router = useRouter(); | ||
const { user: currentUser, authIsReady } = useContext(AuthContext); | ||
|
||
const currentPage = router.pathname; | ||
|
||
useEffect(() => { | ||
if (!currentUser && currentPage !== "/") { | ||
onAuthStateChanged(auth, (user) => { | ||
if (!user) { | ||
router.push("/"); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
if (currentPage === "/") { | ||
return children; | ||
} | ||
|
||
return (currentUser && children) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters