Skip to content

Commit

Permalink
auth template: optional roles for requireAuth (#5336)
Browse files Browse the repository at this point in the history
* auth template: optional roles for requireAuth

* Update tutorial with optional roles

* requireAuth: default options

Co-authored-by: Dominic Saadi <[email protected]>
  • Loading branch information
Tobbe and jtoar authored May 2, 2022
1 parent f110d91 commit 96956c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/docs/tutorial/chapter4/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export const hasRole = ({ roles }): boolean => {
return false
}

export const requireAuth = ({ roles }: { roles: AllowedRoles }) => {
export const requireAuth = ({ roles }: { roles?: AllowedRoles } = {}) => {
if (!isAuthenticated()) {
throw new AuthenticationError("You don't have permission to do that.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ export const hasRole = (roles: AllowedRoles): boolean => {
* whether or not they are assigned a role, and optionally raise an
* error if they're not.
*
* @param roles: AllowedRoles - When checking role membership, these roles grant access.
* @param roles?: AllowedRoles - When checking role membership, these roles grant access.
*
* @returns - If the currentUser is authenticated (and assigned one of the given roles)
*
* @throws {AuthenticationError} - If the currentUser is not authenticated
* @throws {ForbiddenError} If the currentUser is not allowed due to role permissions
* @throws {ForbiddenError} - If the currentUser is not allowed due to role permissions
*
* @see https://github.com/redwoodjs/redwood/tree/main/packages/auth for examples
*/
export const requireAuth = ({ roles }: { roles: AllowedRoles }) => {
export const requireAuth = ({ roles }: { roles?: AllowedRoles } = {}) => {
if (!isAuthenticated()) {
throw new AuthenticationError("You don't have permission to do that.")
}
Expand Down

0 comments on commit 96956c8

Please sign in to comment.