@cedarjs/auth 8.0.0-canary.3518 → 8.0.0-canary.3522
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 -3
- package/dist/AuthContext.d.ts.map +1 -1
- package/dist/AuthProvider/useHasRole.d.ts.map +1 -1
- package/dist/AuthProvider/useHasRole.js +2 -25
- package/dist/currentUserHasRole.d.ts +16 -0
- package/dist/currentUserHasRole.d.ts.map +1 -0
- package/dist/currentUserHasRole.js +23 -0
- package/package.json +6 -2
package/dist/AuthContext.d.ts
CHANGED
|
@@ -2,16 +2,21 @@ import React from 'react';
|
|
|
2
2
|
/**
|
|
3
3
|
* The user returned by your app's `getCurrentUser` function on the api side,
|
|
4
4
|
* when someone is logged in. Cedar generates the fields from your
|
|
5
|
-
* `getCurrentUser` function
|
|
5
|
+
* `getCurrentUser` function.
|
|
6
|
+
*
|
|
7
|
+
* This is a declaration-merging target: Cedar's generated
|
|
8
|
+
* `.cedar/types/includes/all-currentUser.d.ts` augments it with the app's
|
|
9
|
+
* `getCurrentUser` return type. It has no fields of its own, so
|
|
10
|
+
* `LoggedInUser` has exactly the fields `getCurrentUser` returns, and is `{}`
|
|
11
|
+
* in apps where `getCurrentUser` doesn't return a user.
|
|
6
12
|
*/
|
|
7
13
|
export interface LoggedInUser {
|
|
8
|
-
[key: string]: unknown;
|
|
9
14
|
}
|
|
10
15
|
/**
|
|
11
16
|
* What `getCurrentUser` returns: the logged-in user, or `null` when nobody is
|
|
12
17
|
* logged in
|
|
13
18
|
*/
|
|
14
|
-
export type CurrentUser = LoggedInUser | null;
|
|
19
|
+
export type CurrentUser = (LoggedInUser & object) | null;
|
|
15
20
|
export interface AuthContextInterface<TUser, TLogInOptions, TLogIn, TLogOutOptions, TLogOut, TSignUpOptions, TSignUp, TForgotPassword, TResetPasswordOptions, TResetPassword, TValidateResetToken, TClient, TChangePasswordOptions = unknown, TChangePassword = unknown> {
|
|
16
21
|
/** Determining your current authentication state */
|
|
17
22
|
loading: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../src/AuthContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB
|
|
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,CAAC,CAAA;IAClC;;;OAGG;IACH,cAAc,IAAI,OAAO,CAAC,WAAW,CAAC,CAAA;IACtC;;;;;QAKI;IACJ,OAAO,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAA;IACjD;;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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHasRole.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/useHasRole.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"useHasRole.d.ts","sourceRoot":"","sources":["../../src/AuthProvider/useHasRole.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAGpD,eAAO,MAAM,UAAU,GAAI,aAAa,WAAW,oBAehC,MAAM,GAAG,MAAM,EAAE,KAAG,OAItC,CAAA"}
|
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
import { useCallback } from "react";
|
|
2
|
+
import { currentUserHasRole } from "../currentUserHasRole.js";
|
|
2
3
|
const useHasRole = (currentUser) => {
|
|
3
4
|
return useCallback(
|
|
4
|
-
(rolesToCheck) =>
|
|
5
|
-
if (currentUser?.roles) {
|
|
6
|
-
if (typeof rolesToCheck === "string") {
|
|
7
|
-
if (typeof currentUser.roles === "string") {
|
|
8
|
-
return currentUser.roles === rolesToCheck;
|
|
9
|
-
} else if (Array.isArray(currentUser.roles)) {
|
|
10
|
-
return currentUser.roles?.some(
|
|
11
|
-
(allowedRole) => rolesToCheck === allowedRole
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
if (Array.isArray(rolesToCheck)) {
|
|
16
|
-
if (Array.isArray(currentUser.roles)) {
|
|
17
|
-
return currentUser.roles?.some(
|
|
18
|
-
(allowedRole) => rolesToCheck.includes(allowedRole)
|
|
19
|
-
);
|
|
20
|
-
} else if (typeof currentUser.roles === "string") {
|
|
21
|
-
return rolesToCheck.some(
|
|
22
|
-
(allowedRole) => currentUser?.roles === allowedRole
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return false;
|
|
28
|
-
},
|
|
5
|
+
(rolesToCheck) => currentUserHasRole(currentUser, rolesToCheck),
|
|
29
6
|
[currentUser]
|
|
30
7
|
);
|
|
31
8
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CurrentUser } from './AuthContext.js';
|
|
2
|
+
/**
|
|
3
|
+
* @example
|
|
4
|
+
* ```js
|
|
5
|
+
* currentUserHasRole(currentUser, "editor")
|
|
6
|
+
* currentUserHasRole(currentUser, ["editor"])
|
|
7
|
+
* currentUserHasRole(currentUser, ["editor", "author"])
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* Checks if the "currentUser" from the api side
|
|
11
|
+
* is assigned a role or one of a list of roles.
|
|
12
|
+
* If the user is assigned any of the provided list of roles,
|
|
13
|
+
* the hasRole is considered to be true.
|
|
14
|
+
*/
|
|
15
|
+
export declare function currentUserHasRole(currentUser: CurrentUser, rolesToCheck: string | string[]): boolean;
|
|
16
|
+
//# sourceMappingURL=currentUserHasRole.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currentUserHasRole.d.ts","sourceRoot":"","sources":["../src/currentUserHasRole.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAChC,WAAW,EAAE,WAAW,EACxB,YAAY,EAAE,MAAM,GAAG,MAAM,EAAE,GAC9B,OAAO,CA4BT"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function currentUserHasRole(currentUser, rolesToCheck) {
|
|
2
|
+
const roles = currentUser && "roles" in currentUser ? currentUser.roles : void 0;
|
|
3
|
+
if (roles) {
|
|
4
|
+
if (typeof rolesToCheck === "string") {
|
|
5
|
+
if (typeof roles === "string") {
|
|
6
|
+
return roles === rolesToCheck;
|
|
7
|
+
} else if (Array.isArray(roles)) {
|
|
8
|
+
return roles.some((allowedRole) => rolesToCheck === allowedRole);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
if (Array.isArray(rolesToCheck)) {
|
|
12
|
+
if (Array.isArray(roles)) {
|
|
13
|
+
return roles.some((allowedRole) => rolesToCheck.includes(allowedRole));
|
|
14
|
+
} else if (typeof roles === "string") {
|
|
15
|
+
return rolesToCheck.some((allowedRole) => roles === allowedRole);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
currentUserHasRole
|
|
23
|
+
};
|
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.3522",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
"./dist/AuthProvider/ServerAuthProvider.js": {
|
|
33
33
|
"types": "./dist/AuthProvider/ServerAuthProvider.d.ts",
|
|
34
34
|
"default": "./dist/AuthProvider/ServerAuthProvider.js"
|
|
35
|
+
},
|
|
36
|
+
"./currentUserHasRole": {
|
|
37
|
+
"types": "./dist/currentUserHasRole.d.ts",
|
|
38
|
+
"default": "./dist/currentUserHasRole.js"
|
|
35
39
|
}
|
|
36
40
|
},
|
|
37
41
|
"types": "./dist/index.d.ts",
|
|
@@ -51,7 +55,7 @@
|
|
|
51
55
|
},
|
|
52
56
|
"devDependencies": {
|
|
53
57
|
"@arethetypeswrong/cli": "0.18.5",
|
|
54
|
-
"@cedarjs/framework-tools": "8.0.0-canary.
|
|
58
|
+
"@cedarjs/framework-tools": "8.0.0-canary.3522",
|
|
55
59
|
"@testing-library/dom": "10.4.2",
|
|
56
60
|
"@testing-library/jest-dom": "6.9.1",
|
|
57
61
|
"@testing-library/react": "16.3.3",
|