@classic-homes/auth 0.1.25 → 0.1.26

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.
@@ -0,0 +1,3 @@
1
+ export { authActions, authStore, currentUser, isAuthenticated } from './chunk-U2YM3E3Q.js';
2
+ import './chunk-MI4B4ZRK.js';
3
+ import './chunk-DCGC6CNV.js';
@@ -0,0 +1,98 @@
1
+ // src/core/config.ts
2
+ var config = null;
3
+ function initAuth(options) {
4
+ config = {
5
+ ...options,
6
+ storageKey: options.storageKey ?? "classic_auth"
7
+ };
8
+ }
9
+ function getConfig() {
10
+ if (!config) {
11
+ throw new Error(
12
+ '@classic-homes/auth not initialized. Call initAuth({ baseUrl: "..." }) before using auth services.'
13
+ );
14
+ }
15
+ return config;
16
+ }
17
+ function isInitialized() {
18
+ return config !== null;
19
+ }
20
+ function resetConfig() {
21
+ config = null;
22
+ }
23
+ function getDefaultStorage() {
24
+ if (typeof window !== "undefined" && window.localStorage) {
25
+ return {
26
+ getItem: (key) => localStorage.getItem(key),
27
+ setItem: (key, value) => localStorage.setItem(key, value),
28
+ removeItem: (key) => localStorage.removeItem(key)
29
+ };
30
+ }
31
+ return {
32
+ getItem: () => null,
33
+ setItem: () => {
34
+ },
35
+ removeItem: () => {
36
+ }
37
+ };
38
+ }
39
+ function getStorage() {
40
+ const cfg = getConfig();
41
+ return cfg.storage ?? getDefaultStorage();
42
+ }
43
+ function getFetch() {
44
+ const cfg = getConfig();
45
+ return cfg.fetch ?? fetch;
46
+ }
47
+
48
+ // src/core/jwt.ts
49
+ function decodeJWT(token) {
50
+ try {
51
+ const parts = token.split(".");
52
+ if (parts.length !== 3) {
53
+ return null;
54
+ }
55
+ const payload = parts[1];
56
+ const decoded = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
57
+ return JSON.parse(decoded);
58
+ } catch {
59
+ return null;
60
+ }
61
+ }
62
+ function isTokenExpired(token) {
63
+ const payload = decodeJWT(token);
64
+ if (!payload || !payload.exp) {
65
+ return true;
66
+ }
67
+ return payload.exp * 1e3 < Date.now();
68
+ }
69
+ function getTokenRemainingTime(token) {
70
+ const payload = decodeJWT(token);
71
+ if (!payload || !payload.exp) {
72
+ return 0;
73
+ }
74
+ const remainingMs = payload.exp * 1e3 - Date.now();
75
+ return Math.max(0, remainingMs);
76
+ }
77
+ function getTokenExpiration(token) {
78
+ const payload = decodeJWT(token);
79
+ if (!payload || !payload.exp) {
80
+ return null;
81
+ }
82
+ return new Date(payload.exp * 1e3);
83
+ }
84
+ function extractClaims(token, claims) {
85
+ const payload = decodeJWT(token);
86
+ if (!payload) {
87
+ return null;
88
+ }
89
+ const result = {};
90
+ for (const claim of claims) {
91
+ if (claim in payload) {
92
+ result[claim] = payload[claim];
93
+ }
94
+ }
95
+ return result;
96
+ }
97
+
98
+ export { decodeJWT, extractClaims, getConfig, getDefaultStorage, getFetch, getStorage, getTokenExpiration, getTokenRemainingTime, initAuth, isInitialized, isTokenExpired, resetConfig };
@@ -0,0 +1,66 @@
1
+ import { authStore } from './chunk-U2YM3E3Q.js';
2
+
3
+ // src/svelte/guards/auth-guard.ts
4
+ function checkAuth(options = {}) {
5
+ const { roles, permissions, requireAllRoles, requireAllPermissions } = options;
6
+ if (!authStore.isAuthenticated) {
7
+ return {
8
+ allowed: false,
9
+ reason: "not_authenticated",
10
+ redirectTo: "/login"
11
+ };
12
+ }
13
+ if (roles && roles.length > 0) {
14
+ const hasRoles = requireAllRoles ? authStore.hasAllRoles(roles) : authStore.hasAnyRole(roles);
15
+ if (!hasRoles) {
16
+ return {
17
+ allowed: false,
18
+ reason: "missing_role",
19
+ redirectTo: "/unauthorized"
20
+ };
21
+ }
22
+ }
23
+ if (permissions && permissions.length > 0) {
24
+ const hasPermissions = requireAllPermissions ? authStore.hasAllPermissions(permissions) : authStore.hasAnyPermission(permissions);
25
+ if (!hasPermissions) {
26
+ return {
27
+ allowed: false,
28
+ reason: "missing_permission",
29
+ redirectTo: "/unauthorized"
30
+ };
31
+ }
32
+ }
33
+ return { allowed: true };
34
+ }
35
+ function createAuthGuard(options = {}) {
36
+ return (onDenied) => {
37
+ const result = checkAuth(options);
38
+ if (!result.allowed) {
39
+ onDenied(result.redirectTo ?? "/login", result.reason ?? "not_authenticated");
40
+ return false;
41
+ }
42
+ return true;
43
+ };
44
+ }
45
+ function requireAuth() {
46
+ return checkAuth();
47
+ }
48
+ function requireRole(roles, requireAll = false) {
49
+ const roleArray = Array.isArray(roles) ? roles : [roles];
50
+ return checkAuth({ roles: roleArray, requireAllRoles: requireAll });
51
+ }
52
+ function requirePermission(permissions, requireAll = false) {
53
+ const permArray = Array.isArray(permissions) ? permissions : [permissions];
54
+ return checkAuth({ permissions: permArray, requireAllPermissions: requireAll });
55
+ }
56
+ function protectedLoad(options, loadFn) {
57
+ return async (event) => {
58
+ const result = checkAuth(options);
59
+ if (!result.allowed) {
60
+ return { redirect: result.redirectTo ?? "/login" };
61
+ }
62
+ return loadFn(event);
63
+ };
64
+ }
65
+
66
+ export { checkAuth, createAuthGuard, protectedLoad, requireAuth, requirePermission, requireRole };