@checkstack/auth-frontend 0.5.1 → 0.5.3
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/CHANGELOG.md +19 -0
- package/package.json +1 -1
- package/src/index.test.tsx +3 -8
- package/src/index.tsx +1 -38
- package/src/lib/AuthAccessApi.ts +37 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @checkstack/auth-frontend
|
|
2
2
|
|
|
3
|
+
## 0.5.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [d1324e6]
|
|
8
|
+
- Updated dependencies [2c0822d]
|
|
9
|
+
- @checkstack/ui@0.4.0
|
|
10
|
+
|
|
11
|
+
## 0.5.2
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [8a87cd4]
|
|
16
|
+
- Updated dependencies [8a87cd4]
|
|
17
|
+
- @checkstack/auth-common@0.5.2
|
|
18
|
+
- @checkstack/common@0.5.0
|
|
19
|
+
- @checkstack/frontend-api@0.3.2
|
|
20
|
+
- @checkstack/ui@0.3.1
|
|
21
|
+
|
|
3
22
|
## 0.5.1
|
|
4
23
|
|
|
5
24
|
### Patch Changes
|
package/package.json
CHANGED
package/src/index.test.tsx
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, mock, beforeEach } from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
import { accessApiRef } from "@checkstack/frontend-api";
|
|
2
|
+
import { AuthAccessApi } from "./lib/AuthAccessApi";
|
|
4
3
|
import type { AccessRule } from "@checkstack/common";
|
|
5
4
|
import { useAccessRules } from "./hooks/useAccessRules";
|
|
6
5
|
|
|
@@ -32,14 +31,10 @@ const otherAccess: AccessRule = {
|
|
|
32
31
|
};
|
|
33
32
|
|
|
34
33
|
describe("AuthAccessApi", () => {
|
|
35
|
-
let accessApi:
|
|
36
|
-
useAccess: (p: AccessRule) => { loading: boolean; allowed: boolean };
|
|
37
|
-
};
|
|
34
|
+
let accessApi: AuthAccessApi;
|
|
38
35
|
|
|
39
36
|
beforeEach(() => {
|
|
40
|
-
|
|
41
|
-
if (!apiDef) throw new Error("Access API not found in plugin");
|
|
42
|
-
accessApi = apiDef.factory({ get: () => ({}) } as any) as any;
|
|
37
|
+
accessApi = new AuthAccessApi();
|
|
43
38
|
});
|
|
44
39
|
|
|
45
40
|
it("should return true if user has the access rule", () => {
|
package/src/index.tsx
CHANGED
|
@@ -2,7 +2,6 @@ import React from "react";
|
|
|
2
2
|
import {
|
|
3
3
|
ApiRef,
|
|
4
4
|
accessApiRef,
|
|
5
|
-
AccessApi,
|
|
6
5
|
createFrontendPlugin,
|
|
7
6
|
createSlotExtension,
|
|
8
7
|
NavbarRightSlot,
|
|
@@ -24,10 +23,8 @@ import { OnboardingPage } from "./components/OnboardingPage";
|
|
|
24
23
|
import { ProfilePage } from "./components/ProfilePage";
|
|
25
24
|
import { authApiRef, AuthApi, AuthSession } from "./api";
|
|
26
25
|
import { getAuthClientLazy } from "./lib/auth-client";
|
|
26
|
+
import { AuthAccessApi } from "./lib/AuthAccessApi";
|
|
27
27
|
|
|
28
|
-
import { useAccessRules } from "./hooks/useAccessRules";
|
|
29
|
-
|
|
30
|
-
import type { AccessRule } from "@checkstack/common";
|
|
31
28
|
import { useNavigate } from "react-router-dom";
|
|
32
29
|
import { Settings2, User } from "lucide-react";
|
|
33
30
|
import { DropdownMenuItem } from "@checkstack/ui";
|
|
@@ -41,40 +38,6 @@ import {
|
|
|
41
38
|
import { resolveRoute } from "@checkstack/common";
|
|
42
39
|
import { OnboardingCheck } from "./components/OnboardingCheck";
|
|
43
40
|
|
|
44
|
-
/**
|
|
45
|
-
* Unified access API implementation.
|
|
46
|
-
* Uses AccessRule objects for access checks.
|
|
47
|
-
*/
|
|
48
|
-
class AuthAccessApi implements AccessApi {
|
|
49
|
-
useAccess(accessRule: AccessRule): { loading: boolean; allowed: boolean } {
|
|
50
|
-
const { accessRules, loading } = useAccessRules();
|
|
51
|
-
|
|
52
|
-
if (loading) {
|
|
53
|
-
return { loading: true, allowed: false };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// If no user, or user has no access rules, return false
|
|
57
|
-
if (!accessRules || accessRules.length === 0) {
|
|
58
|
-
return { loading: false, allowed: false };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const accessRuleId = accessRule.id;
|
|
62
|
-
|
|
63
|
-
// Check wildcard, exact match, or manage implies read
|
|
64
|
-
const isWildcard = accessRules.includes("*");
|
|
65
|
-
const hasExact = accessRules.includes(accessRuleId);
|
|
66
|
-
|
|
67
|
-
// For read actions, also check if user has manage access for the same resource
|
|
68
|
-
const hasManage =
|
|
69
|
-
accessRule.level === "read"
|
|
70
|
-
? accessRules.includes(`${accessRule.resource}.manage`)
|
|
71
|
-
: false;
|
|
72
|
-
|
|
73
|
-
const allowed = isWildcard || hasExact || hasManage;
|
|
74
|
-
return { loading: false, allowed };
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
41
|
/**
|
|
79
42
|
* BetterAuthApi wraps only better-auth client methods.
|
|
80
43
|
* For RPC calls, use rpcApiRef.forPlugin<AuthClient>("auth") directly.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AccessApi } from "@checkstack/frontend-api";
|
|
2
|
+
import { useAccessRules } from "../hooks/useAccessRules";
|
|
3
|
+
import type { AccessRule } from "@checkstack/common";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Unified access API implementation.
|
|
7
|
+
* Uses AccessRule objects for access checks.
|
|
8
|
+
*/
|
|
9
|
+
export class AuthAccessApi implements AccessApi {
|
|
10
|
+
useAccess(accessRule: AccessRule): { loading: boolean; allowed: boolean } {
|
|
11
|
+
const { accessRules, loading } = useAccessRules();
|
|
12
|
+
|
|
13
|
+
if (loading) {
|
|
14
|
+
return { loading: true, allowed: false };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// If no user, or user has no access rules, return false
|
|
18
|
+
if (!accessRules || accessRules.length === 0) {
|
|
19
|
+
return { loading: false, allowed: false };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const accessRuleId = accessRule.id;
|
|
23
|
+
|
|
24
|
+
// Check wildcard, exact match, or manage implies read
|
|
25
|
+
const isWildcard = accessRules.includes("*");
|
|
26
|
+
const hasExact = accessRules.includes(accessRuleId);
|
|
27
|
+
|
|
28
|
+
// For read actions, also check if user has manage access for the same resource
|
|
29
|
+
const hasManage =
|
|
30
|
+
accessRule.level === "read"
|
|
31
|
+
? accessRules.includes(`${accessRule.resource}.manage`)
|
|
32
|
+
: false;
|
|
33
|
+
|
|
34
|
+
const allowed = isWildcard || hasExact || hasManage;
|
|
35
|
+
return { loading: false, allowed };
|
|
36
|
+
}
|
|
37
|
+
}
|