-
Notifications
You must be signed in to change notification settings - Fork 1
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
22 changed files
with
276 additions
and
122 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,18 @@ | ||
import React from "react"; | ||
import { AuthContext } from "../../context/AuthContext"; | ||
import { useRoleQuery } from "../../generated/graphql"; | ||
|
||
export const AuthProvider: React.FC = ({ children }) => { | ||
const [{ data, fetching }] = useRoleQuery(); | ||
|
||
return ( | ||
<AuthContext.Provider | ||
value={{ | ||
isLoading: fetching, | ||
role: data?.role || null, | ||
}} | ||
> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; |
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,12 @@ | ||
import React from "react"; | ||
import { UserRole } from "../generated/graphql"; | ||
|
||
export interface IAuthContext { | ||
isLoading: boolean; | ||
role: UserRole | null; | ||
} | ||
|
||
export const AuthContext = React.createContext<IAuthContext>({ | ||
isLoading: true, | ||
role: null, | ||
}); |
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,3 @@ | ||
query Role { | ||
role | ||
} |
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,48 @@ | ||
import { Block } from "baseui/block"; | ||
import { useRouter } from "next/dist/client/router"; | ||
import { Loading } from "../components/Loading"; | ||
import { UserRole } from "../generated/graphql"; | ||
import { useAuth } from "../hooks/useAuth"; | ||
|
||
/** | ||
* A Higher Order Component to protect pages from unauthenticated and | ||
* unauthorized usage. | ||
* Redirects to "/login" if unauthenticated. | ||
* Redirects to "/" if unauthorized. | ||
* | ||
* @param roles the roles authorized to access the page, | ||
* empty array or undefined if all. | ||
*/ | ||
export const withAuth = ( | ||
WrappedComponent: React.ComponentType, | ||
roles?: UserRole[] | ||
) => { | ||
const WithAuthWrapper = (props: JSX.IntrinsicAttributes) => { | ||
const { isLoading, role } = useAuth(); | ||
const router = useRouter(); | ||
|
||
if (isLoading) { | ||
return ( | ||
<Block display="flex" height="100vh" width="100vw"> | ||
<Loading /> | ||
</Block> | ||
); | ||
} | ||
|
||
if (!role) { | ||
// unauthenticated | ||
router.push("/login"); | ||
return <></>; | ||
} | ||
|
||
if (roles && roles.length > 0 && !roles.includes(role)) { | ||
// unauthorized | ||
router.push("/"); | ||
return <></>; | ||
} | ||
|
||
return <WrappedComponent {...props} />; | ||
}; | ||
|
||
return WithAuthWrapper; | ||
}; |
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,10 @@ | ||
import React from "react"; | ||
import { AuthContext, IAuthContext } from "../context/AuthContext"; | ||
|
||
export function useAuth(): IAuthContext { | ||
const context = React.useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error("useAuth must be used within an AuthProvider"); | ||
} | ||
return context; | ||
} |
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
Oops, something went wrong.