-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
413 additions
and
27 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { minimatch } from "minimatch"; | ||
import { useEffect } from "react"; | ||
import { useLocation } from "react-router"; | ||
import { useAuth } from "../authentication/hook.js"; | ||
import { useZudoku } from "../components/context/ZudokuContext.js"; | ||
import { ZudokuError } from "../util/invariant.js"; | ||
|
||
export function isProtectedRoute( | ||
path: string, | ||
protectedPatterns?: string[], | ||
): boolean { | ||
if (!protectedPatterns?.length) return false; | ||
return protectedPatterns.some((pattern) => minimatch(path, pattern)); | ||
} | ||
|
||
export function ProtectedRoute({ children }: { children: React.ReactNode }) { | ||
const auth = useAuth(); | ||
const zudoku = useZudoku(); | ||
const location = useLocation(); | ||
|
||
const isProtected = isProtectedRoute( | ||
location.pathname, | ||
zudoku.options.protectedRoutes, | ||
); | ||
|
||
useEffect(() => { | ||
if (isProtected && !auth.isAuthenticated) { | ||
void zudoku.authentication?.signIn(); | ||
} | ||
}, [isProtected, auth.isAuthenticated, zudoku.authentication]); | ||
|
||
if (isProtected && !auth.isAuthenticated) { | ||
return null; | ||
} | ||
|
||
if (isProtected && !auth.isAuthEnabled) { | ||
throw new ZudokuError("Authentication is not enabled", { | ||
title: "Authentication is not enabled", | ||
developerHint: | ||
"To use protectedRoutes you need authentication to be enabled", | ||
}); | ||
} | ||
|
||
return 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
Oops, something went wrong.