-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented role based access control
- Loading branch information
1 parent
a9336e0
commit 422693f
Showing
4 changed files
with
118 additions
and
0 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,52 @@ | ||
/* | ||
@Author: siddiquiaffan | ||
*/ | ||
|
||
import React from 'react' | ||
import type { Role } from '@/server/db/schema' | ||
import { checkAccess } from '@/lib/auth/check-access' | ||
import { redirect } from 'next/navigation' | ||
|
||
type Options = ({ | ||
Fallback: React.FC | ||
}) | ({ | ||
redirect: string | ||
}) | ||
|
||
const DefaultFallback = () => <div>Permission denied</div> | ||
|
||
/** | ||
* | ||
* A high order component which takes a component and roles as arguments and returns a new component. | ||
* @example | ||
* ``` | ||
* withPermissionCheck( | ||
* MyComponent, | ||
* ['user', 'moderator'], | ||
* { Fallback: () => <div>Permission denied</div> } | ||
* ) | ||
* ``` | ||
*/ | ||
|
||
// eslint-disable-next-line | ||
const withPermissionCheck = <T extends Record<string, any>>(Component: React.FC<T>, roles: Role[], options?: Options): React.FC<T> => { | ||
|
||
return async (props: T) => { | ||
|
||
const hasPermission = await checkAccess(roles) | ||
|
||
if (!hasPermission) { | ||
if (options && 'redirect' in options) { | ||
redirect(options.redirect) | ||
} else { | ||
const Fallback = options?.Fallback ?? DefaultFallback | ||
return <Fallback /> | ||
} | ||
} | ||
|
||
return <Component {...props} /> | ||
} | ||
}; | ||
|
||
|
||
export default withPermissionCheck |
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,60 @@ | ||
/* | ||
@Author: siddiquiaffan | ||
@Desc: Utility functions for role based access | ||
*/ | ||
|
||
import { type Role } from "@/server/db/schema"; | ||
import { validateRequest } from './validate-request' | ||
import { cache } from "react"; | ||
|
||
export async function uncachedCheckAccess( | ||
role: Role | Role[], | ||
{ | ||
method, | ||
}: { | ||
method: "some" | "every"; | ||
} = { method: "some" }, | ||
): Promise<boolean> { | ||
const { user } = await validateRequest(); | ||
if (!user) { | ||
return false; | ||
} | ||
|
||
// admin can access everything | ||
if (user.roles?.includes("admin")) { | ||
return true; | ||
} | ||
|
||
if (Array.isArray(role)) { | ||
return role[method]((r: string) => user.roles?.includes(r as Role)); | ||
} | ||
|
||
return !!user.roles?.includes(role as Role); | ||
} | ||
|
||
/** | ||
* Check if the user has access | ||
*/ | ||
export const checkAccess = cache(uncachedCheckAccess); | ||
|
||
|
||
// ============== { Separate methods for each role type } ============== | ||
export async function isModerator(): Promise<boolean> { | ||
return checkAccess("moderator"); | ||
} | ||
|
||
export async function isAdmin(): Promise<boolean> { | ||
return checkAccess("admin"); | ||
} | ||
|
||
export async function isContentCreator(): Promise<boolean> { | ||
return checkAccess("content-creator"); | ||
} | ||
|
||
export async function isOnlyUser(): Promise<boolean> { | ||
const { user } = await validateRequest(); | ||
if (!user) { | ||
return false; | ||
} | ||
return user.roles?.length === 1 && user.roles[0] === "user"; | ||
} |
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