authorize.js 599 B

12345678910111213141516171819202122232425
  1. export default async function authorize(user, resource, permissions) {
  2. if (_isNil(user)) {
  3. throw new AuthError(`You must be signed in to modify ${resource}s.`, {
  4. resource,
  5. permissions
  6. })
  7. }
  8. permissions = _flatten([permissions])
  9. // noinspection JSUnresolvedReference
  10. const result = await auth.api.userHasPermission({
  11. body: {
  12. userId: user.id,
  13. permissions: { [resource]: permissions }
  14. }
  15. })
  16. if (!result.success) {
  17. throw new AuthError(
  18. `${user.username} isn't allowed to modify ${resource}s.`,
  19. { resource, permissions }
  20. )
  21. }
  22. }