@meistrari/auth-core 0.1.0

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/index.mjs ADDED
@@ -0,0 +1,81 @@
1
+ import { createRemoteJWKSet, jwtVerify } from 'jose';
2
+ import { createAuthClient as createAuthClient$1 } from 'better-auth/client';
3
+ import { organizationClient } from 'better-auth/client/plugins';
4
+ import { ssoClient } from '@better-auth/sso/client';
5
+ import { createAccessControl } from 'better-auth/plugins/access';
6
+ import { defaultStatements } from 'better-auth/plugins/organization/access';
7
+
8
+ const version = "0.1.0";
9
+
10
+ function isTokenExpired(token) {
11
+ const payload = JSON.parse(atob(token.split(".")[1] ?? ""));
12
+ return payload.exp && Date.now() / 1e3 > payload.exp;
13
+ }
14
+ async function validateToken(token, apiUrl) {
15
+ try {
16
+ const JWKS = createRemoteJWKSet(
17
+ new URL(`${apiUrl}/api/auth/jwks`)
18
+ );
19
+ await jwtVerify(token, JWKS, {
20
+ issuer: apiUrl
21
+ });
22
+ return true;
23
+ } catch (error) {
24
+ return false;
25
+ }
26
+ }
27
+ const statements = {
28
+ ...defaultStatements,
29
+ access: ["admin", "member", "reviewer"]
30
+ };
31
+ const ac = createAccessControl(statements);
32
+ const Roles = {
33
+ ORG_ADMIN: "org:admin",
34
+ ORG_MEMBER: "org:member",
35
+ ORG_REVIEWER: "org:reviewer"
36
+ };
37
+ const rolesAccessControl = {
38
+ [Roles.ORG_ADMIN]: ac.newRole({
39
+ access: ["admin"],
40
+ organization: ["update"],
41
+ team: ["create", "update", "delete"],
42
+ member: ["create", "update", "delete"],
43
+ invitation: ["create", "cancel"]
44
+ }),
45
+ [Roles.ORG_MEMBER]: ac.newRole({
46
+ access: ["member"]
47
+ }),
48
+ [Roles.ORG_REVIEWER]: ac.newRole({
49
+ access: ["reviewer"]
50
+ })
51
+ };
52
+ const customEndpointsPluginClient = () => {
53
+ return {
54
+ id: "custom-endpoints",
55
+ $InferServerPlugin: {}
56
+ };
57
+ };
58
+ function createAuthClient(baseURL) {
59
+ return createAuthClient$1({
60
+ baseURL,
61
+ plugins: [
62
+ organizationClient({
63
+ ac,
64
+ roles: rolesAccessControl,
65
+ teams: {
66
+ enabled: true
67
+ }
68
+ }),
69
+ customEndpointsPluginClient(),
70
+ ssoClient()
71
+ ],
72
+ fetchOptions: {
73
+ credentials: "include",
74
+ headers: {
75
+ "User-Agent": `auth-sdk:${version}`
76
+ }
77
+ }
78
+ });
79
+ }
80
+
81
+ export { Roles, ac, createAuthClient, isTokenExpired, rolesAccessControl, validateToken };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@meistrari/auth-core",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./dist/index.d.ts",
8
+ "import": "./dist/index.js"
9
+ }
10
+ },
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "scripts": {
17
+ "build": "unbuild"
18
+ },
19
+ "dependencies": {
20
+ "@better-auth/sso": "1.3.28",
21
+ "better-auth": "1.3.28",
22
+ "jose": "6.1.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "latest",
26
+ "typescript": "5.9.2",
27
+ "unbuild": "3.6.1"
28
+ }
29
+ }