-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/user-service/admin-edit-user
- Loading branch information
Showing
10 changed files
with
454 additions
and
278 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { ReactNode } from "react"; | ||
import { useAuth } from "@/app/auth/auth-context"; | ||
import { Button } from "@/components/ui/button"; | ||
import { useRouter } from "next/navigation"; | ||
|
||
type AuthCheck = (user: { isAdmin: boolean } | undefined | null) => boolean; | ||
|
||
interface AuthPageWrapperProps extends React.HTMLProps<HTMLDivElement> { | ||
children: ReactNode; | ||
|
||
// User access rules | ||
authCheck?: AuthCheck; // Custom predicate which is true when user is to be granted access | ||
requireAdmin?: boolean; | ||
requireLoggedIn?: boolean; | ||
} | ||
|
||
const AuthPageWrapper: React.FC<AuthPageWrapperProps> = ({ | ||
children, | ||
...props | ||
}) => { | ||
const auth = useAuth(); | ||
const router = useRouter(); | ||
|
||
const authCheck = ( | ||
user: { isAdmin: boolean } | undefined | null | ||
): boolean => { | ||
if (props?.requireLoggedIn && !user) { | ||
return false; | ||
} | ||
if (props?.requireAdmin && !user?.isAdmin) { | ||
return false; | ||
} | ||
if (props?.authCheck) { | ||
return props.authCheck(user); | ||
} | ||
// Allow access if no user access rule is defined | ||
return true; | ||
}; | ||
|
||
return ( | ||
<div> | ||
{authCheck(auth?.user) ? ( | ||
children | ||
) : ( | ||
<div className="flex items-start justify-center h-2/6"> | ||
<div className="text-center mt-[20vh]"> | ||
<h1 className="text-4xl font-extrabold tracking-tight lg:text-5xl mb-6"> | ||
Uh Oh! You're not supposed to be here! | ||
</h1> | ||
<Button | ||
size="lg" | ||
onClick={() => { | ||
auth?.user ? router.push("/") : router.push("/auth/login"); | ||
}} | ||
> | ||
Return Home | ||
</Button> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AuthPageWrapper; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.