@cedarjs/auth 8.0.0-canary.3538 → 8.0.0-canary.3540

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,60 @@ export interface LoggedInUser {
17
17
  * logged in
18
18
  */
19
19
  export type CurrentUser = (LoggedInUser & object) | null;
20
- export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient, TChangePasswordOptions = unknown, TChangePassword = unknown> {
20
+ /**
21
+ * The app's permission names, as keys. Cedar generates them from the app's
22
+ * permission definitions (`packages/permissions`, set up by
23
+ * `cedar setup tenancy`). Empty when the app has none, and then
24
+ * `hasPermission` takes nothing: it could only ever answer `false`.
25
+ */
26
+ export interface AvailablePermissions {
27
+ }
28
+ /**
29
+ * A permission name `hasPermission` takes: see `AvailablePermissions`.
30
+ * `never` in an app without permissions, so calling it is a type error.
31
+ */
32
+ export type PermissionName = [keyof AvailablePermissions] extends [never] ? never : Extract<keyof AvailablePermissions, string>;
33
+ /**
34
+ * Whether the app has tenancy (`cedar setup tenancy`). This is a
35
+ * declaration-merging target: `cedar generate types` writes
36
+ * `.cedar/types/includes/web-tenancy.d.ts`, which augments it with
37
+ * `{ enabled: true }` in an app with tenancy. Empty otherwise.
38
+ */
39
+ export interface AuthTenancy {
40
+ }
41
+ /**
42
+ * `useAuth()`'s role check: `hasUserRole` in an app with tenancy (see
43
+ * `AuthTenancy`), where every role check says which kind it is, and `hasRole`
44
+ * otherwise
45
+ */
46
+ type RoleCheck = AuthTenancy extends {
47
+ enabled: true;
48
+ } ? {
49
+ /**
50
+ * Whether the "currentUser" from the api side has one of `rolesToCheck`
51
+ * (a role, or a list of roles) in `currentUser.roles`, as opposed to a
52
+ * role in the organization they're in
53
+ */
54
+ hasUserRole(rolesToCheck: string | string[]): boolean;
55
+ } : {
56
+ /**
57
+ * Checks if the "currentUser" from the api side is assigned a role or one
58
+ * of a list of roles. If the user is assigned any of the provided list of
59
+ * roles, then hasRole is considered to be true.
60
+ */
61
+ hasRole(rolesToCheck: string | string[]): boolean;
62
+ };
63
+ /**
64
+ * Both of `useAuth()`'s role checks, for an auth provider to spread into the
65
+ * value it provides. It can't know whether the app has tenancy, so it
66
+ * provides both, and the app's types show the one that fits (see
67
+ * `RoleCheck`).
68
+ */
69
+ export declare function roleChecks(check: (rolesToCheck: string | string[]) => boolean): {
70
+ hasRole: (rolesToCheck: string | string[]) => boolean;
71
+ hasUserRole: (rolesToCheck: string | string[]) => boolean;
72
+ };
73
+ export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient, TChangePasswordOptions = unknown, TChangePassword = unknown> extends RoleCheck {
21
74
  /** Determining your current authentication state */
22
75
  loading: boolean;
23
76
  isAuthenticated: boolean;
@@ -58,12 +111,11 @@ export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptio
58
111
  */
59
112
  getCurrentUser(): Promise<CurrentUser>;
60
113
  /**
61
- * Checks if the "currentUser" from the api side
62
- * is assigned a role or one of a list of roles.
63
- * If the user is assigned any of the provided list of roles,
64
- * the hasRole is considered to be true.
65
- **/
66
- hasRole(rolesToCheck: string | string[]): boolean;
114
+ * Whether the user has `permission` where they are in the app. Always
115
+ * `false` outside a scope that provides permissions, such as `OrgScope`
116
+ * from `@cedarjs/tenancy/web`.
117
+ */
118
+ hasPermission(permission: PermissionName): boolean;
67
119
  /**
68
120
  * Redetermine authentication state and update the state.
69
121
  */
@@ -87,4 +139,5 @@ export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptio
87
139
  error?: Error;
88
140
  }
89
141
  export declare function createAuthContext<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient, TChangePasswordOptions = unknown, TChangePassword = unknown>(): React.Context<AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient, TChangePasswordOptions, TChangePassword> | undefined>;
142
+ export {};
90
143
  //# sourceMappingURL=AuthContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../src/AuthContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,YAAY;CAAG;AAEhC;;;GAGG;AAGH,MAAM,MAAM,WAAW,GAAG,CAAC,YAAY,GAAG,MAAM,CAAC,GAAG,IAAI,CAAA;AAExD,MAAM,WAAW,oBAAoB,CACnC,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,GAAG,OAAO,EAChC,eAAe,GAAG,OAAO;IAEzB,oDAAoD;IACpD,OAAO,EAAE,OAAO,CAAA;IAChB,eAAe,EAAE,OAAO,CAAA;IACxB,iFAAiF;IACjF,WAAW,EAAE,WAAW,CAAA;IACxB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,YAAY,EAAE,IAAI,GAAG,KAAK,CAAA;IAC1B,KAAK,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/C,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD;;;OAGG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,GAAG,MAAM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MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD,MAAM,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAClD;;;OAGG;IACH,QAAQ,IAAI,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,CAAA;IAClC;;;OAGG;IACH,cAAc,IAAI,OAAO,CAAC,WAAW,CAAC,CAAA;IACtC;;;;OAIG;IACH,aAAa,CAAC,UAAU,EAAE,cAAc,GAAG,OAAO,CAAA;IAClD;;OAEG;IACH,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAC/B,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;IAC1D,aAAa,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;IACvE,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAA;IAC3E;;;;OAIG;IACH,cAAc,CAAC,OAAO,CAAC,EAAE,sBAAsB,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;IAC1E;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,GAAG,OAAO,EAChC,eAAe,GAAG,OAAO,oQAqB1B"}
@@ -1,7 +1,11 @@
1
1
  import React from "react";
2
+ function roleChecks(check) {
3
+ return { hasRole: check, hasUserRole: check };
4
+ }
2
5
  function createAuthContext() {
3
6
  return React.createContext(void 0);
4
7
  }
5
8
  export {
6
- createAuthContext
9
+ createAuthContext,
10
+ roleChecks
7
11
  };
@@ -1 +1 @@
1
- {"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/AuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACtC,OAAO,KAA0C,MAAM,OAAO,CAAA;AAE9D,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAiBlE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,GAAG,OAAO,EAChC,eAAe,GAAG,OAAO,EAEzB,WAAW,EAAE,KAAK,CAAC,OAAO,CACtB,oBAAoB,CAClB,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,GACD,SAAS,CACZ,EACD,kBAAkB,EAAE,kBAAkB,CACpC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,EACD,mBAAmB,CAAC,EAAE;IACpB,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,CAAA;IAC3C,UAAU,CAAC,EAAE,CACX,WAAW,EAAE,WAAW,KACrB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,OAAO,CAAA;CAClD,kBAEmC,iBAAiB,uBA8FtD"}
1
+ {"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/AuthProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACtC,OAAO,KAA0C,MAAM,OAAO,CAAA;AAG9D,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAiBlE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,CAAA;CACpB;AAKD,wBAAgB,kBAAkB,CAChC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,GAAG,OAAO,EAChC,eAAe,GAAG,OAAO,EAEzB,WAAW,EAAE,KAAK,CAAC,OAAO,CACtB,oBAAoB,CAClB,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,GACD,SAAS,CACZ,EACD,kBAAkB,EAAE,kBAAkB,CACpC,KAAK,EACL,YAAY,EACZ,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,EACD,mBAAmB,CAAC,EAAE;IACpB,cAAc,CAAC,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,CAAA;IAC3C,UAAU,CAAC,EAAE,CACX,WAAW,EAAE,WAAW,KACrB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,KAAK,OAAO,CAAA;CAClD,kBAEmC,iBAAiB,uBA+FtD"}
@@ -1,4 +1,5 @@
1
1
  import React, { useContext, useEffect, useState } from "react";
2
+ import { roleChecks } from "../AuthContext.js";
2
3
  import { spaDefaultAuthProviderState } from "./AuthProviderState.js";
3
4
  import { ServerAuthContext } from "./ServerAuthProvider.js";
4
5
  import { useChangePassword } from "./useChangePassword.js";
@@ -12,6 +13,7 @@ import { useResetPassword } from "./useResetPassword.js";
12
13
  import { useSignUp } from "./useSignUp.js";
13
14
  import { useToken } from "./useToken.js";
14
15
  import { useValidateResetToken } from "./useValidateResetToken.js";
16
+ const noPermissions = () => false;
15
17
  function createAuthProvider(AuthContext, authImplementation, customProviderHooks) {
16
18
  const AuthProvider = ({ children }) => {
17
19
  const serverAuthState = useContext(ServerAuthContext);
@@ -66,7 +68,8 @@ function createAuthProvider(AuthContext, authImplementation, customProviderHooks
66
68
  logOut,
67
69
  getToken,
68
70
  getCurrentUser,
69
- hasRole,
71
+ ...roleChecks(hasRole),
72
+ hasPermission: noPermissions,
70
73
  reauthenticate,
71
74
  forgotPassword,
72
75
  resetPassword,
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+ /**
3
+ * Provides `useAuth().hasPermission` for the components below it. Unset,
4
+ * `hasPermission` is always `false`: permissions belong to a scope, such as
5
+ * an organization, and there is none here.
6
+ *
7
+ * `OrgScope` from `@cedarjs/tenancy/web` provides it, so inside an
8
+ * organization `hasPermission` checks the user's membership there.
9
+ */
10
+ export declare const HasPermissionContext: React.Context<((permission: string) => boolean) | undefined>;
11
+ //# sourceMappingURL=HasPermissionContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HasPermissionContext.d.ts","sourceRoot":"","sources":["../src/HasPermissionContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,8BACjB,MAAM,KAAK,OAAO,cACtB,CAAA"}
@@ -0,0 +1,5 @@
1
+ import React from "react";
2
+ const HasPermissionContext = React.createContext(void 0);
3
+ export {
4
+ HasPermissionContext
5
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export type { AuthContextInterface, CurrentUser, LoggedInUser, } from './AuthContext.js';
1
+ export type { AuthContextInterface, AuthTenancy, AvailablePermissions, CurrentUser, LoggedInUser, PermissionName, } from './AuthContext.js';
2
2
  export type { AuthProviderProps } from './AuthProvider/AuthProvider.js';
3
+ export { HasPermissionContext } from './HasPermissionContext.js';
3
4
  export { useNoAuth } from './useAuth.js';
4
5
  export type { UseAuth } from './useAuth.js';
5
6
  export { createAuthentication } from './authFactory.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,oBAAoB,EACpB,WAAW,EACX,YAAY,GACb,MAAM,kBAAkB,CAAA;AACzB,YAAY,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,YAAY,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAC3D,YAAY,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAEjE,OAAO,EACL,2BAA2B,EAC3B,kCAAkC,GACnC,MAAM,qCAAqC,CAAA;AAC5C,YAAY,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AAE5E,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,oBAAoB,EACpB,WAAW,EACX,oBAAoB,EACpB,WAAW,EACX,YAAY,EACZ,cAAc,GACf,MAAM,kBAAkB,CAAA;AACzB,YAAY,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AACxC,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,YAAY,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AAC3D,YAAY,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAEjE,OAAO,EACL,2BAA2B,EAC3B,kCAAkC,GACnC,MAAM,qCAAqC,CAAA;AAC5C,YAAY,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAA;AAE5E,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAA"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { HasPermissionContext } from "./HasPermissionContext.js";
1
2
  import { useNoAuth } from "./useAuth.js";
2
3
  import { createAuthentication } from "./authFactory.js";
3
4
  import {
@@ -6,6 +7,7 @@ import {
6
7
  } from "./AuthProvider/AuthProviderState.js";
7
8
  import { getCurrentUserFromMiddleware } from "./getCurrentUserFromMiddleware.js";
8
9
  export {
10
+ HasPermissionContext,
9
11
  createAuthentication,
10
12
  getCurrentUserFromMiddleware,
11
13
  middlewareDefaultAuthProviderState,
@@ -1 +1 @@
1
- {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAE5D,wBAAgB,aAAa,CAC3B,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,GAAG,OAAO,EAChC,eAAe,GAAG,OAAO,EAEzB,WAAW,EAAE,KAAK,CAAC,OAAO,CACtB,oBAAoB,CAClB,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,GACD,SAAS,CACZ,SAEmB,oBAAoB,CACtC,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,CAWF;AAED,wBAAgB,SAAS,IAAI,oBAAoB,CAC/C,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,IAAI,EACJ,IAAI,CACL,CAqBA;AAED,MAAM,MAAM,OAAO,GAAG,MAAM,oBAAoB,CAC9C,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,CACR,CAAA"}
1
+ {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAG5D,wBAAgB,aAAa,CAC3B,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,GAAG,OAAO,EAChC,eAAe,GAAG,OAAO,EAEzB,WAAW,EAAE,KAAK,CAAC,OAAO,CACtB,oBAAoB,CAClB,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,GACD,SAAS,CACZ,SAEmB,oBAAoB,CACtC,KAAK,EACL,aAAa,EACb,MAAM,EACN,cAAc,EACd,OAAO,EACP,cAAc,EACd,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,mBAAmB,EACnB,OAAO,EACP,sBAAsB,EACtB,eAAe,CAChB,CAiBF;AAED,wBAAgB,SAAS,IAAI,oBAAoB,CAC/C,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,SAAS,EACT,IAAI,EACJ,IAAI,CACL,CAsBA;AAED,MAAM,MAAM,OAAO,GAAG,MAAM,oBAAoB,CAC9C,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,EACP,OAAO,CACR,CAAA"}
package/dist/useAuth.js CHANGED
@@ -1,11 +1,18 @@
1
1
  import React from "react";
2
+ import { roleChecks } from "./AuthContext.js";
3
+ import { HasPermissionContext } from "./HasPermissionContext.js";
2
4
  function createUseAuth(AuthContext) {
3
5
  const useAuth = () => {
4
6
  const context = React.useContext(AuthContext);
5
- if (!context) {
7
+ const hasPermission = React.useContext(HasPermissionContext);
8
+ const scoped = React.useMemo(
9
+ () => context && hasPermission ? { ...context, hasPermission } : context,
10
+ [context, hasPermission]
11
+ );
12
+ if (!scoped) {
6
13
  throw new Error("useAuth must be used within an AuthProvider");
7
14
  }
8
- return context;
15
+ return scoped;
9
16
  };
10
17
  return useAuth;
11
18
  }
@@ -23,7 +30,8 @@ function useNoAuth() {
23
30
  userMetadata: null,
24
31
  getToken: async () => null,
25
32
  getCurrentUser: async () => null,
26
- hasRole: () => false,
33
+ ...roleChecks(() => false),
34
+ hasPermission: () => false,
27
35
  reauthenticate: async () => {
28
36
  },
29
37
  forgotPassword: async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/auth",
3
- "version": "8.0.0-canary.3538",
3
+ "version": "8.0.0-canary.3540",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@arethetypeswrong/cli": "0.18.5",
58
- "@cedarjs/framework-tools": "8.0.0-canary.3538",
58
+ "@cedarjs/framework-tools": "8.0.0-canary.3540",
59
59
  "@testing-library/dom": "10.4.2",
60
60
  "@testing-library/jest-dom": "6.9.1",
61
61
  "@testing-library/react": "16.3.3",