@cedarjs/auth 8.0.0-canary.3568 → 8.0.0-canary.3570
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.
- package/dist/AuthContext.d.ts +8 -6
- package/dist/AuthContext.d.ts.map +1 -1
- package/dist/AuthProvider/AuthProvider.d.ts.map +1 -1
- package/dist/AuthProvider/AuthProvider.js +8 -3
- package/dist/HasPermissionContext.d.ts +6 -4
- package/dist/HasPermissionContext.d.ts.map +1 -1
- package/dist/currentUserHasPermission.d.ts +10 -0
- package/dist/currentUserHasPermission.d.ts.map +1 -0
- package/dist/currentUserHasPermission.js +7 -0
- package/package.json +6 -2
package/dist/AuthContext.d.ts
CHANGED
|
@@ -20,8 +20,9 @@ export type CurrentUser = (LoggedInUser & object) | null;
|
|
|
20
20
|
/**
|
|
21
21
|
* The app's permission names, as keys. Cedar generates them from the app's
|
|
22
22
|
* permission definitions (`packages/permissions`, set up by
|
|
23
|
-
* `cedar setup tenancy`). Empty when the app
|
|
24
|
-
* `hasPermission` takes nothing: it could only ever
|
|
23
|
+
* `cedar setup permissions` or `cedar setup tenancy`). Empty when the app
|
|
24
|
+
* has none, and then `hasPermission` takes nothing: it could only ever
|
|
25
|
+
* answer `false`.
|
|
25
26
|
*/
|
|
26
27
|
export interface AvailablePermissions {
|
|
27
28
|
}
|
|
@@ -85,10 +86,11 @@ export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptio
|
|
|
85
86
|
*/
|
|
86
87
|
hasRole(rolesToCheck: string | string[]): boolean;
|
|
87
88
|
/**
|
|
88
|
-
* Whether the user has `permission` where they are in the app
|
|
89
|
-
* `
|
|
90
|
-
* organization route with `tenancy` from
|
|
91
|
-
* `Router`.
|
|
89
|
+
* Whether the user has `permission` where they are in the app: in
|
|
90
|
+
* `currentUser.permissions`, or in a scope that provides permissions,
|
|
91
|
+
* such as an organization route with `tenancy` from
|
|
92
|
+
* `@cedarjs/tenancy/web` on the `Router`. With tenancy, `false` outside
|
|
93
|
+
* an organization.
|
|
92
94
|
*/
|
|
93
95
|
hasPermission(permission: PermissionName): boolean;
|
|
94
96
|
/**
|
|
@@ -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
|
|
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;;;;;;GAMG;AAEH,MAAM,WAAW,oBAAoB;CAAG;AAExC;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,MAAM,oBAAoB,CAAC,SAAS,CAAC,KAAK,CAAC,GACrE,KAAK,GACL,OAAO,CAAC,MAAM,oBAAoB,EAAE,MAAM,CAAC,CAAA;AAE/C;;;;;GAKG;AAEH,MAAM,WAAW,WAAW;CAAG;AAE/B,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,WA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,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 +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,
|
|
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,KAAuD,MAAM,OAAO,CAAA;AAE3E,OAAO,KAAK,EAAE,oBAAoB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAkBlE,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,uBAuGtD"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import React, { useContext, useEffect, useState } from "react";
|
|
1
|
+
import React, { useCallback, useContext, useEffect, useState } from "react";
|
|
2
|
+
import { currentUserHasPermission } from "../currentUserHasPermission.js";
|
|
2
3
|
import { spaDefaultAuthProviderState } from "./AuthProviderState.js";
|
|
3
4
|
import { ServerAuthContext } from "./ServerAuthProvider.js";
|
|
4
5
|
import { useChangePassword } from "./useChangePassword.js";
|
|
@@ -12,7 +13,6 @@ 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";
|
|
15
|
-
const noPermissions = () => false;
|
|
16
16
|
function createAuthProvider(AuthContext, authImplementation, customProviderHooks) {
|
|
17
17
|
const AuthProvider = ({ children }) => {
|
|
18
18
|
const serverAuthState = useContext(ServerAuthContext);
|
|
@@ -31,6 +31,11 @@ function createAuthProvider(AuthContext, authImplementation, customProviderHooks
|
|
|
31
31
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
32
32
|
useHasRole(authProviderState.currentUser)
|
|
33
33
|
);
|
|
34
|
+
const { currentUser } = authProviderState;
|
|
35
|
+
const hasPermission = useCallback(
|
|
36
|
+
(permission) => currentUserHasPermission(currentUser, permission),
|
|
37
|
+
[currentUser]
|
|
38
|
+
);
|
|
34
39
|
const signUp = useSignUp(
|
|
35
40
|
authImplementation,
|
|
36
41
|
setAuthProviderState,
|
|
@@ -68,7 +73,7 @@ function createAuthProvider(AuthContext, authImplementation, customProviderHooks
|
|
|
68
73
|
getToken,
|
|
69
74
|
getCurrentUser,
|
|
70
75
|
hasRole,
|
|
71
|
-
hasPermission
|
|
76
|
+
hasPermission,
|
|
72
77
|
reauthenticate,
|
|
73
78
|
forgotPassword,
|
|
74
79
|
resetPassword,
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
/**
|
|
3
|
-
* Provides `useAuth().hasPermission` for the components below it
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Provides `useAuth().hasPermission` for the components below it, for a
|
|
4
|
+
* scope with permissions of its own. Unset, `hasPermission` checks
|
|
5
|
+
* `currentUser.permissions`, the user's own (`cedar setup permissions`).
|
|
6
6
|
*
|
|
7
7
|
* The organization provider that the `Router` renders for `tenancy` from
|
|
8
8
|
* `@cedarjs/tenancy/web` provides it, so inside an organization route
|
|
9
|
-
* `hasPermission` checks the user's membership there.
|
|
9
|
+
* `hasPermission` checks the user's membership there. With tenancy,
|
|
10
|
+
* `currentUser` has no permissions of its own, so outside an organization
|
|
11
|
+
* `hasPermission` is `false`.
|
|
10
12
|
*/
|
|
11
13
|
export declare const HasPermissionContext: React.Context<((permission: string) => boolean) | undefined>;
|
|
12
14
|
//# sourceMappingURL=HasPermissionContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HasPermissionContext.d.ts","sourceRoot":"","sources":["../src/HasPermissionContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB
|
|
1
|
+
{"version":3,"file":"HasPermissionContext.d.ts","sourceRoot":"","sources":["../src/HasPermissionContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,8BACjB,MAAM,KAAK,OAAO,cACtB,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CurrentUser } from './AuthContext.js';
|
|
2
|
+
/**
|
|
3
|
+
* Whether `currentUser.permissions` has `permission`. `getCurrentUser` adds
|
|
4
|
+
* them with `withPermissions` in an app that has run
|
|
5
|
+
* `cedar setup permissions`: the permissions of the user's roles. With
|
|
6
|
+
* tenancy, permissions belong to a membership instead, so `currentUser`
|
|
7
|
+
* has none of its own.
|
|
8
|
+
*/
|
|
9
|
+
export declare function currentUserHasPermission(currentUser: CurrentUser, permission: string): boolean;
|
|
10
|
+
//# sourceMappingURL=currentUserHasPermission.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currentUserHasPermission.d.ts","sourceRoot":"","sources":["../src/currentUserHasPermission.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,WAAW,EAAE,WAAW,EACxB,UAAU,EAAE,MAAM,GACjB,OAAO,CAQT"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
function currentUserHasPermission(currentUser, permission) {
|
|
2
|
+
const permissions = currentUser && "permissions" in currentUser ? currentUser.permissions : void 0;
|
|
3
|
+
return Array.isArray(permissions) && permissions.includes(permission);
|
|
4
|
+
}
|
|
5
|
+
export {
|
|
6
|
+
currentUserHasPermission
|
|
7
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cedarjs/auth",
|
|
3
|
-
"version": "8.0.0-canary.
|
|
3
|
+
"version": "8.0.0-canary.3570",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"./currentUserHasRole": {
|
|
37
37
|
"types": "./dist/currentUserHasRole.d.ts",
|
|
38
38
|
"default": "./dist/currentUserHasRole.js"
|
|
39
|
+
},
|
|
40
|
+
"./currentUserHasPermission": {
|
|
41
|
+
"types": "./dist/currentUserHasPermission.d.ts",
|
|
42
|
+
"default": "./dist/currentUserHasPermission.js"
|
|
39
43
|
}
|
|
40
44
|
},
|
|
41
45
|
"types": "./dist/index.d.ts",
|
|
@@ -55,7 +59,7 @@
|
|
|
55
59
|
},
|
|
56
60
|
"devDependencies": {
|
|
57
61
|
"@arethetypeswrong/cli": "0.18.5",
|
|
58
|
-
"@cedarjs/framework-tools": "8.0.0-canary.
|
|
62
|
+
"@cedarjs/framework-tools": "8.0.0-canary.3570",
|
|
59
63
|
"@testing-library/dom": "10.4.2",
|
|
60
64
|
"@testing-library/jest-dom": "6.9.1",
|
|
61
65
|
"@testing-library/react": "16.3.3",
|