Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth template: optional roles for requireAuth #5336

Merged
merged 4 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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