astro-sessionkit 0.1.18 → 0.1.20

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.
@@ -1 +1 @@
1
- {"version":3,"file":"guardMiddleware.d.ts","sourceRoot":"","sources":["../../src/core/guardMiddleware.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAa,iBAAiB,EAAC,MAAM,OAAO,CAAC;AAsEzD,wBAAgB,qBAAqB,IAAI,iBAAiB,CA8DzD"}
1
+ {"version":3,"file":"guardMiddleware.d.ts","sourceRoot":"","sources":["../../src/core/guardMiddleware.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAa,iBAAiB,EAAC,MAAM,OAAO,CAAC;AAsEzD,wBAAgB,qBAAqB,IAAI,iBAAiB,CAoEzD"}
@@ -48,9 +48,6 @@ async function checkRule(rule, session) {
48
48
  function createGuardMiddleware() {
49
49
  return async (context, next) => {
50
50
  const { protect, loginPath, globalProtect, exclude } = getConfig();
51
- if (protect.length === 0 && !globalProtect) {
52
- return next();
53
- }
54
51
  let pathname;
55
52
  try {
56
53
  pathname = new URL(context.request.url).pathname;
@@ -58,10 +55,17 @@ function createGuardMiddleware() {
58
55
  catch {
59
56
  pathname = "/";
60
57
  }
58
+ debug(`[Guard] Pathname: ${pathname}, GlobalProtect: ${globalProtect}, Rules: ${protect.length}`);
59
+ if (protect.length === 0 && !globalProtect) {
60
+ debug(`[Guard] Skipping ${pathname} because no rules are configured and globalProtect is false`);
61
+ return next();
62
+ }
61
63
  const sessionContext = getContextStore();
62
64
  const session = sessionContext?.session ?? null;
63
65
  const rule = protect.find((r) => matchesPattern(r.pattern, pathname));
64
- debug(`[Guard] Pathname: ${pathname}, Found rule: ${rule ? JSON.stringify(rule) : 'none'}, GlobalProtect: ${globalProtect}`);
66
+ if (rule) {
67
+ debug(`[Guard] Found matching rule for ${pathname}:`, rule);
68
+ }
65
69
  if (!rule) {
66
70
  if (globalProtect) {
67
71
  if (exclude.some((pattern) => matchesPattern(pattern, pathname))) {
@@ -1 +1 @@
1
- {"version":3,"file":"guardMiddleware.js","sources":["../../src/core/guardMiddleware.ts"],"sourcesContent":["// ============================================================================\n// Route Guard Middleware - Enforces protection rules\n// ============================================================================\n\nimport type {APIContext, MiddlewareHandler} from \"astro\";\nimport { getContextStore } from \"./context\";\nimport { getConfig } from \"./config\";\nimport { matchesPattern } from \"./matcher\";\nimport type { ProtectionRule, Session } from \"./types\";\nimport { isValidSessionStructure } from \"./validation\";\nimport * as logger from \"./logger\";\n\n/**\n * Check if session satisfies a protection rule\n */\nasync function checkRule(rule: ProtectionRule, session: Session | null): Promise<boolean> {\n const { access } = getConfig();\n\n // Custom check overrides everything\n if (access.check) {\n try {\n return await access.check(rule, session);\n } catch (error) {\n logger.error('Error in custom access check hook:', error);\n return false;\n }\n }\n\n // Custom allow function\n if (\"allow\" in rule) {\n try {\n return await rule.allow(session);\n } catch (error) {\n logger.error('Error in custom rule allow function:', error);\n return false;\n }\n }\n\n // Must be authenticated and have a valid session structure for all other checks\n if (!session || !isValidSessionStructure(session)) {\n return false;\n }\n\n // Single role check\n if (\"role\" in rule) {\n const userRole = access.getRole(session);\n return userRole === rule.role;\n }\n\n // Multiple roles check (user must have ONE of these)\n if (\"roles\" in rule) {\n const userRole = access.getRole(session);\n return userRole !== null && rule.roles.includes(userRole);\n }\n\n // Single permission check\n if (\"permission\" in rule) {\n const userPermissions = access.getPermissions(session);\n return userPermissions.includes(rule.permission);\n }\n\n // Multiple permissions check (user must have ALL of these)\n if (\"permissions\" in rule) {\n const userPermissions = access.getPermissions(session);\n return rule.permissions.every((p) => userPermissions.includes(p));\n }\n\n // No specific rule matched - allow by default\n return true;\n}\n\n/**\n * Create route guard middleware\n */\nexport function createGuardMiddleware(): MiddlewareHandler {\n return async (context : APIContext, next) => {\n const { protect, loginPath, globalProtect, exclude } = getConfig();\n \n // No rules configured and no global protect - skip\n if (protect.length === 0 && !globalProtect) {\n return next();\n }\n\n let pathname: string;\n try {\n pathname = new URL(context.request.url).pathname;\n } catch {\n // Fallback if URL is invalid (unlikely in Astro)\n pathname = \"/\";\n }\n const sessionContext = getContextStore();\n const session = sessionContext?.session ?? null;\n\n // Find matching rule\n const rule = protect.find((r) => matchesPattern(r.pattern, pathname));\n \n logger.debug(`[Guard] Pathname: ${pathname}, Found rule: ${rule ? JSON.stringify(rule) : 'none'}, GlobalProtect: ${globalProtect}`);\n\n // No matching rule - check global protection\n if (!rule) {\n if (globalProtect) {\n // Skip if path is in exclude list\n if (exclude.some((pattern) => matchesPattern(pattern, pathname))) {\n logger.debug(`[GlobalProtect] Skipping ${pathname} because it matches an exclude pattern`);\n return next();\n }\n \n // Skip if it's the login page itself (to avoid redirect loops)\n if (pathname === loginPath) {\n logger.debug(`[GlobalProtect] Skipping ${pathname} because it is the loginPath`);\n return next();\n }\n\n // Require valid session\n if (!session || !isValidSessionStructure(session)) {\n logger.debug(`[GlobalProtect] Redirecting to ${loginPath} because session is ${session ? 'invalid' : 'missing'}`);\n return context.redirect(loginPath);\n }\n }\n \n logger.debug(`[GlobalProtect] Allowing ${pathname} because session is valid or globalProtect is false`);\n return next();\n }\n\n // Check if access is allowed\n const allowed = await checkRule(rule, session);\n\n if (!allowed) {\n const redirectTo = rule.redirectTo ?? loginPath;\n logger.debug(`[Guard] Redirecting to ${redirectTo} because access was denied by rule:`, rule);\n return context.redirect(redirectTo);\n }\n\n logger.debug(`[Guard] Allowing ${pathname} because access was granted by rule:`, rule);\n return next();\n };\n}\n"],"names":["error","logger.error","logger.debug"],"mappings":";;;;;;AAeA,eAAe,SAAS,CAAC,IAAoB,EAAE,OAAuB,EAAA;AACpE,IAAA,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE;AAG9B,IAAA,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,QAAA,IAAI;YACF,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC;QAC1C;QAAE,OAAOA,OAAK,EAAE;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CAAC,OAAO,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,EAAE;AACjD,oBAAAA,KAAY,CAAC,CAAA,+BAAA,EAAkC,SAAS,uBAAuB,OAAO,GAAG,SAAS,GAAG,SAAS,CAAA,CAAE,CAAC;AACjH,oBAAA,OAAO,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC;gBACpC;YACF;AAEA,YAAAA,KAAY,CAAC,4BAA4B,QAAQ,CAAA,mDAAA,CAAqD,CAAC;YACvG,OAAO,IAAI,EAAE;QACf;QAGA,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC;QAE9C,IAAI,CAAC,OAAO,EAAE;AACZ,YAAA,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,SAAS;YAC/CA,KAAY,CAAC,CAAA,uBAAA,EAA0B,UAAU,CAAA,mCAAA,CAAqC,EAAE,IAAI,CAAC;AAC7F,YAAA,OAAO,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC;QACrC;QAEAA,KAAY,CAAC,CAAA,iBAAA,EAAoB,QAAQ,CAAA,oCAAA,CAAsC,EAAE,IAAI,CAAC;QACtF,OAAO,IAAI,EAAE;AACf,IAAA,CAAC;AACH;;;;"}
1
+ {"version":3,"file":"guardMiddleware.js","sources":["../../src/core/guardMiddleware.ts"],"sourcesContent":["// ============================================================================\n// Route Guard Middleware - Enforces protection rules\n// ============================================================================\n\nimport type {APIContext, MiddlewareHandler} from \"astro\";\nimport { getContextStore } from \"./context\";\nimport { getConfig } from \"./config\";\nimport { matchesPattern } from \"./matcher\";\nimport type { ProtectionRule, Session } from \"./types\";\nimport { isValidSessionStructure } from \"./validation\";\nimport * as logger from \"./logger\";\n\n/**\n * Check if session satisfies a protection rule\n */\nasync function checkRule(rule: ProtectionRule, session: Session | null): Promise<boolean> {\n const { access } = getConfig();\n\n // Custom check overrides everything\n if (access.check) {\n try {\n return await access.check(rule, session);\n } catch (error) {\n logger.error('Error in custom access check hook:', error);\n return false;\n }\n }\n\n // Custom allow function\n if (\"allow\" in rule) {\n try {\n return await rule.allow(session);\n } catch (error) {\n logger.error('Error in custom rule allow function:', error);\n return false;\n }\n }\n\n // Must be authenticated and have a valid session structure for all other checks\n if (!session || !isValidSessionStructure(session)) {\n return false;\n }\n\n // Single role check\n if (\"role\" in rule) {\n const userRole = access.getRole(session);\n return userRole === rule.role;\n }\n\n // Multiple roles check (user must have ONE of these)\n if (\"roles\" in rule) {\n const userRole = access.getRole(session);\n return userRole !== null && rule.roles.includes(userRole);\n }\n\n // Single permission check\n if (\"permission\" in rule) {\n const userPermissions = access.getPermissions(session);\n return userPermissions.includes(rule.permission);\n }\n\n // Multiple permissions check (user must have ALL of these)\n if (\"permissions\" in rule) {\n const userPermissions = access.getPermissions(session);\n return rule.permissions.every((p) => userPermissions.includes(p));\n }\n\n // No specific rule matched - allow by default\n return true;\n}\n\n/**\n * Create route guard middleware\n */\nexport function createGuardMiddleware(): MiddlewareHandler {\n return async (context : APIContext, next) => {\n const { protect, loginPath, globalProtect, exclude } = getConfig();\n \n let pathname: string;\n try {\n pathname = new URL(context.request.url).pathname;\n } catch {\n // Fallback if URL is invalid (unlikely in Astro)\n pathname = \"/\";\n }\n\n logger.debug(`[Guard] Pathname: ${pathname}, GlobalProtect: ${globalProtect}, Rules: ${protect.length}`);\n\n // No rules configured and no global protect - skip\n if (protect.length === 0 && !globalProtect) {\n logger.debug(`[Guard] Skipping ${pathname} because no rules are configured and globalProtect is false`);\n return next();\n }\n\n const sessionContext = getContextStore();\n const session = sessionContext?.session ?? null;\n\n // Find matching rule\n const rule = protect.find((r) => matchesPattern(r.pattern, pathname));\n \n if (rule) {\n logger.debug(`[Guard] Found matching rule for ${pathname}:`, rule);\n }\n\n // No matching rule - check global protection\n if (!rule) {\n if (globalProtect) {\n // Skip if path is in exclude list\n if (exclude.some((pattern) => matchesPattern(pattern, pathname))) {\n logger.debug(`[GlobalProtect] Skipping ${pathname} because it matches an exclude pattern`);\n return next();\n }\n \n // Skip if it's the login page itself (to avoid redirect loops)\n if (pathname === loginPath) {\n logger.debug(`[GlobalProtect] Skipping ${pathname} because it is the loginPath`);\n return next();\n }\n\n // Require valid session\n if (!session || !isValidSessionStructure(session)) {\n logger.debug(`[GlobalProtect] Redirecting to ${loginPath} because session is ${session ? 'invalid' : 'missing'}`);\n return context.redirect(loginPath);\n }\n }\n \n logger.debug(`[GlobalProtect] Allowing ${pathname} because session is valid or globalProtect is false`);\n return next();\n }\n\n // Check if access is allowed\n const allowed = await checkRule(rule, session);\n\n if (!allowed) {\n const redirectTo = rule.redirectTo ?? loginPath;\n logger.debug(`[Guard] Redirecting to ${redirectTo} because access was denied by rule:`, rule);\n return context.redirect(redirectTo);\n }\n\n logger.debug(`[Guard] Allowing ${pathname} because access was granted by rule:`, rule);\n return next();\n };\n}\n"],"names":["error","logger.error","logger.debug"],"mappings":";;;;;;AAeA,eAAe,SAAS,CAAC,IAAoB,EAAE,OAAuB,EAAA;AACpE,IAAA,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE;AAG9B,IAAA,IAAI,MAAM,CAAC,KAAK,EAAE;AAChB,QAAA,IAAI;YACF,OAAO,MAAM,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC;QAC1C;QAAE,OAAOA,OAAK,EAAE;AACd,YAAAC,KAAY,CAAC,oCAAoC,EAAED,OAAK,CAAC;AACzD,YAAA,OAAO,KAAK;QACd;IACF;AAGA,IAAA,IAAI,OAAO,IAAI,IAAI,EAAE;AACnB,QAAA,IAAI;AACF,YAAA,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;QAClC;QAAE,OAAOA,OAAK,EAAE;AACd,YAAAC,KAAY,CAAC,sCAAsC,EAAED,OAAK,CAAC;AAC3D,YAAA,OAAO,KAAK;QACd;IACF;IAGA,IAAI,CAAC,OAAO,IAAI,CAAC,uBAAuB,CAAC,OAAO,CAAC,EAAE;AACjD,QAAA,OAAO,KAAK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,4BAA4B,QAAQ,CAAA,mDAAA,CAAqD,CAAC;YACvG,OAAO,IAAI,EAAE;QACf;QAGA,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC;QAE9C,IAAI,CAAC,OAAO,EAAE;AACZ,YAAA,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,SAAS;YAC/CA,KAAY,CAAC,CAAA,uBAAA,EAA0B,UAAU,CAAA,mCAAA,CAAqC,EAAE,IAAI,CAAC;AAC7F,YAAA,OAAO,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC;QACrC;QAEAA,KAAY,CAAC,CAAA,iBAAA,EAAoB,QAAQ,CAAA,oCAAA,CAAsC,EAAE,IAAI,CAAC;QACtF,OAAO,IAAI,EAAE;AACf,IAAA,CAAC;AACH;;;;"}
@@ -3,7 +3,7 @@ import { getConfig } from './config.js';
3
3
  function debug(message, ...args) {
4
4
  const { debug } = getConfig();
5
5
  if (debug) {
6
- console.debug(`[SessionKit] ${message}`, ...args);
6
+ console.log(`[SessionKit] ${message}`, ...args);
7
7
  }
8
8
  }
9
9
  function error(message, ...args) {
@@ -1 +1 @@
1
- {"version":3,"file":"logger.js","sources":["../../src/core/logger.ts"],"sourcesContent":["import { getConfig } from \"./config\";\n\n/**\n * Log message if debug mode is enabled\n */\nexport function debug(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug) {\n console.debug(`[SessionKit] ${message}`, ...args);\n }\n}\n\n/**\n * Log error message. Always logs unless in production, but can be forced via debug flag.\n */\nexport function error(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug || process.env.NODE_ENV !== 'production') {\n console.error(`[SessionKit] ${message}`, ...args);\n }\n}\n\n/**\n * Log warning message. Always logs unless in production, but can be forced via debug flag.\n */\nexport function warn(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug || process.env.NODE_ENV !== 'production') {\n console.warn(`[SessionKit] ${message}`, ...args);\n }\n}\n\n/**\n * Log info message. Always logs unless in production, but can be forced via debug flag.\n */\nexport function info(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug || process.env.NODE_ENV !== 'production') {\n console.log(`[SessionKit] ${message}`, ...args);\n }\n}\n"],"names":[],"mappings":";;SAKgB,KAAK,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AACnD,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAC7B,IAAI,KAAK,EAAE;QACT,OAAO,CAAC,KAAK,CAAC,CAAA,aAAA,EAAgB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC;IACnD;AACF;SAKgB,KAAK,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AACnD,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAC7B,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QAClD,OAAO,CAAC,KAAK,CAAC,CAAA,aAAA,EAAgB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC;IACnD;AACF;SAKgB,IAAI,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AAClD,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAC7B,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QAClD,OAAO,CAAC,IAAI,CAAC,CAAA,aAAA,EAAgB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC;IAClD;AACF;;;;"}
1
+ {"version":3,"file":"logger.js","sources":["../../src/core/logger.ts"],"sourcesContent":["import { getConfig } from \"./config\";\n\n/**\n * Log message if debug mode is enabled\n */\nexport function debug(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug) {\n console.log(`[SessionKit] ${message}`, ...args);\n }\n}\n\n/**\n * Log error message. Always logs unless in production, but can be forced via debug flag.\n */\nexport function error(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug || process.env.NODE_ENV !== 'production') {\n console.error(`[SessionKit] ${message}`, ...args);\n }\n}\n\n/**\n * Log warning message. Always logs unless in production, but can be forced via debug flag.\n */\nexport function warn(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug || process.env.NODE_ENV !== 'production') {\n console.warn(`[SessionKit] ${message}`, ...args);\n }\n}\n\n/**\n * Log info message. Always logs unless in production, but can be forced via debug flag.\n */\nexport function info(message: string, ...args: any[]): void {\n const { debug } = getConfig();\n if (debug || process.env.NODE_ENV !== 'production') {\n console.log(`[SessionKit] ${message}`, ...args);\n }\n}\n"],"names":[],"mappings":";;SAKgB,KAAK,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AACnD,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAC7B,IAAI,KAAK,EAAE;QACT,OAAO,CAAC,GAAG,CAAC,CAAA,aAAA,EAAgB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC;IACjD;AACF;SAKgB,KAAK,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AACnD,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAC7B,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QAClD,OAAO,CAAC,KAAK,CAAC,CAAA,aAAA,EAAgB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC;IACnD;AACF;SAKgB,IAAI,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AAClD,IAAA,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,EAAE;IAC7B,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE;QAClD,OAAO,CAAC,IAAI,CAAC,CAAA,aAAA,EAAgB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC;IAClD;AACF;;;;"}
package/dist/index.d.ts CHANGED
@@ -48,9 +48,9 @@ interface SessionKitConfig {
48
48
  debug?: boolean;
49
49
  }
50
50
 
51
- declare function sessionkit(config?: SessionKitConfig): AstroIntegration;
51
+ declare function sessionKit(config?: SessionKitConfig): AstroIntegration;
52
52
 
53
- declare const version = "0.1.0";
53
+ declare const version = "0.1.20";
54
54
 
55
- export { sessionkit as default, version };
55
+ export { sessionKit as default, version };
56
56
  export type { AccessHooks, CustomProtectionRule, PermissionProtectionRule, PermissionsProtectionRule, ProtectionRule, RoleProtectionRule, RolesProtectionRule, Session, SessionContext, SessionKitConfig };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAE7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAwBrD,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,MAAM,GAAE,gBAAqB,GAAG,gBAAgB,CAwBlF;AAMD,YAAY,EACR,OAAO,EACP,cAAc,EACd,kBAAkB,EAClB,mBAAmB,EACnB,wBAAwB,EACxB,yBAAyB,EACzB,oBAAoB,EACpB,gBAAgB,EAChB,WAAW,EACX,cAAc,EACjB,MAAM,cAAc,CAAC;AAMtB,eAAO,MAAM,OAAO,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,UAAU,MAAM,eAAe,CAAC;AAEvC,eAAe,UAAU,CAAC;AAM1B,YAAY,EACR,OAAO,EACP,cAAc,EACd,kBAAkB,EAClB,mBAAmB,EACnB,wBAAwB,EACxB,yBAAyB,EACzB,oBAAoB,EACpB,gBAAgB,EAChB,WAAW,EACX,cAAc,EACjB,MAAM,cAAc,CAAC;AAMtB,eAAO,MAAM,OAAO,WAAW,CAAC"}
package/dist/index.js CHANGED
@@ -1,26 +1,6 @@
1
- import { setConfig } from './core/config.js';
1
+ import sessionKit from './integration.js';
2
2
 
3
- function sessionkit(config = {}) {
4
- setConfig(config);
5
- return {
6
- name: "astro-sessionkit",
7
- hooks: {
8
- "astro:config:setup": ({ addMiddleware }) => {
9
- addMiddleware({
10
- entrypoint: "astro-sessionkit/middleware",
11
- order: "pre",
12
- });
13
- if ((config.protect && config.protect.length > 0) || config.globalProtect) {
14
- addMiddleware({
15
- entrypoint: "astro-sessionkit/guard",
16
- order: "pre",
17
- });
18
- }
19
- },
20
- },
21
- };
22
- }
23
- const version = "0.1.0";
3
+ const version = "0.1.20";
24
4
 
25
- export { sessionkit as default, version };
5
+ export { sessionKit as default, version };
26
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["// ============================================================================\n// Astro SessionKit - Main Integration Entry Point\n// ============================================================================\n\nimport type {AstroIntegration } from \"astro\";\nimport { setConfig } from \"./core/config\";\nimport type { SessionKitConfig } from \"./core/types\";\n\n/**\n * SessionKit - Simple session access and route protection for Astro\n *\n * @example\n * ```ts\n * // astro.config.mjs\n * import sessionkit from 'astro-sessionkit';\n *\n * export default defineConfig({\n * integrations: [\n * sessionkit({\n * loginPath: '/login',\n * protect: [\n * { pattern: '/admin/**', role: 'admin' },\n * { pattern: '/dashboard', roles: ['user', 'admin'] },\n * { pattern: '/settings', permissions: ['settings:write'] }\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport default function sessionkit(config: SessionKitConfig = {}): AstroIntegration {\n // Store configuration\n setConfig(config);\n\n return {\n name: \"astro-sessionkit\",\n hooks: {\n \"astro:config:setup\": ({ addMiddleware }) => {\n // 1. Always add session context middleware first\n addMiddleware({\n entrypoint: \"astro-sessionkit/middleware\",\n order: \"pre\",\n });\n\n // 2. Add route guard if there are protection rules or global protection is enabled\n if ((config.protect && config.protect.length > 0) || config.globalProtect) {\n addMiddleware({\n entrypoint: \"astro-sessionkit/guard\",\n order: \"pre\",\n });\n }\n },\n },\n };\n}\n\n// ============================================================================\n// Re-export types for convenience\n// ============================================================================\n\nexport type {\n Session,\n ProtectionRule,\n RoleProtectionRule,\n RolesProtectionRule,\n PermissionProtectionRule,\n PermissionsProtectionRule,\n CustomProtectionRule,\n SessionKitConfig,\n AccessHooks,\n SessionContext\n} from \"./core/types\";\n\n// ============================================================================\n// Version export\n// ============================================================================\n\nexport const version = \"0.1.0\";"],"names":[],"mappings":";;AA8Bc,SAAU,UAAU,CAAC,SAA2B,EAAE,EAAA;IAE5D,SAAS,CAAC,MAAM,CAAC;IAEjB,OAAO;AACH,QAAA,IAAI,EAAE,kBAAkB;AACxB,QAAA,KAAK,EAAE;AACH,YAAA,oBAAoB,EAAE,CAAC,EAAE,aAAa,EAAE,KAAI;AAExC,gBAAA,aAAa,CAAC;AACV,oBAAA,UAAU,EAAE,6BAA6B;AACzC,oBAAA,KAAK,EAAE,KAAK;AACf,iBAAA,CAAC;AAGF,gBAAA,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,KAAK,MAAM,CAAC,aAAa,EAAE;AACvE,oBAAA,aAAa,CAAC;AACV,wBAAA,UAAU,EAAE,wBAAwB;AACpC,wBAAA,KAAK,EAAE,KAAK;AACf,qBAAA,CAAC;gBACN;YACJ,CAAC;AACJ,SAAA;KACJ;AACL;AAuBO,MAAM,OAAO,GAAG;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["// ============================================================================\n// Astro SessionKit - Main Integration Entry Point\n// ============================================================================\n\nimport sessionkit from \"./integration\";\n\nexport default sessionkit;\n\n// ============================================================================\n// Re-export types for convenience\n// ============================================================================\n\nexport type {\n Session,\n ProtectionRule,\n RoleProtectionRule,\n RolesProtectionRule,\n PermissionProtectionRule,\n PermissionsProtectionRule,\n CustomProtectionRule,\n SessionKitConfig,\n AccessHooks,\n SessionContext\n} from \"./core/types\";\n\n// ============================================================================\n// Version export\n// ============================================================================\n\nexport const version = \"0.1.20\";\n"],"names":[],"mappings":";;AA6BO,MAAM,OAAO,GAAG;;;;"}
@@ -1,5 +1,5 @@
1
1
  import type { AstroIntegration } from "astro";
2
2
  import type { SessionKitConfig } from "./core/types";
3
3
  export default function sessionKit(config?: SessionKitConfig): AstroIntegration;
4
- export type { Session, ProtectionRule, SessionKitConfig } from "./core/types";
4
+ export type { Session, ProtectionRule, RoleProtectionRule, RolesProtectionRule, PermissionProtectionRule, PermissionsProtectionRule, CustomProtectionRule, SessionKitConfig, AccessHooks, SessionContext } from "./core/types";
5
5
  //# sourceMappingURL=integration.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../src/integration.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAE9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAwBrD,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,MAAM,GAAE,gBAAqB,GAAG,gBAAgB,CAwBlF;AAGD,YAAY,EAAE,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"integration.d.ts","sourceRoot":"","sources":["../src/integration.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAC;AAE9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAwBrD,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,MAAM,GAAE,gBAAqB,GAAG,gBAAgB,CA8BlF;AAED,YAAY,EACV,OAAO,EACP,cAAc,EACd,kBAAkB,EAClB,mBAAmB,EACnB,wBAAwB,EACxB,yBAAyB,EACzB,oBAAoB,EACpB,gBAAgB,EAChB,WAAW,EACX,cAAc,EACf,MAAM,cAAc,CAAC"}
@@ -1,6 +1,8 @@
1
- import { setConfig } from "./core/config";
2
- export default function sessionKit(config = {}) {
1
+ import { setConfig, getConfig } from './core/config.js';
2
+
3
+ function sessionKit(config = {}) {
3
4
  setConfig(config);
5
+ const resolvedConfig = getConfig();
4
6
  return {
5
7
  name: "astro-sessionkit",
6
8
  hooks: {
@@ -9,14 +11,21 @@ export default function sessionKit(config = {}) {
9
11
  entrypoint: "astro-sessionkit/middleware",
10
12
  order: "pre",
11
13
  });
12
- if ((config.protect && config.protect.length > 0) || config.globalProtect) {
14
+ const hasRules = (resolvedConfig.protect && resolvedConfig.protect.length > 0);
15
+ const isGlobal = !!resolvedConfig.globalProtect;
16
+ if (hasRules || isGlobal) {
13
17
  addMiddleware({
14
18
  entrypoint: "astro-sessionkit/guard",
15
19
  order: "pre",
16
20
  });
17
21
  }
22
+ else if (resolvedConfig.debug) {
23
+ console.log("[SessionKit] Route guard NOT registered: no rules and globalProtect is false.");
24
+ }
18
25
  },
19
26
  },
20
27
  };
21
28
  }
22
- //# sourceMappingURL=integration.js.map
29
+
30
+ export { sessionKit as default };
31
+ //# sourceMappingURL=integration.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"integration.js","sourceRoot":"","sources":["../src/integration.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAyB1C,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,SAA2B,EAAE;IAE9D,SAAS,CAAC,MAAM,CAAC,CAAC;IAElB,OAAO;QACL,IAAI,EAAE,kBAAkB;QACxB,KAAK,EAAE;YACL,oBAAoB,EAAE,CAAC,EAAE,aAAa,EAAE,EAAE,EAAE;gBAE1C,aAAa,CAAC;oBACZ,UAAU,EAAE,6BAA6B;oBACzC,KAAK,EAAE,KAAK;iBACb,CAAC,CAAC;gBAGH,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,MAAM,CAAC,aAAa,EAAE,CAAC;oBAC1E,aAAa,CAAC;wBACZ,UAAU,EAAE,wBAAwB;wBACpC,KAAK,EAAE,KAAK;qBACb,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["// ============================================================================\n// Astro Integration\n// ============================================================================\n\nimport type { AstroIntegration } from \"astro\";\nimport { setConfig } from \"./core/config\";\nimport type { SessionKitConfig } from \"./core/types\";\n\n/**\n * SessionKit - Simple session access and route protection for Astro\n * \n * @example\n * ```ts\n * // astro.config.mjs\n * import sessionkit from 'astro-sessionkit';\n * \n * export default defineConfig({\n * integrations: [\n * sessionkit({\n * loginPath: '/login',\n * protect: [\n * { pattern: '/admin/**', role: 'admin' },\n * { pattern: '/dashboard', roles: ['user', 'admin'] },\n * { pattern: '/settings', permissions: ['settings:write'] }\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport default function sessionKit(config: SessionKitConfig = {}): AstroIntegration {\n // Store configuration\n setConfig(config);\n\n return {\n name: \"astro-sessionkit\",\n hooks: {\n \"astro:config:setup\": ({ addMiddleware }) => {\n // 1. Always add session context middleware first\n addMiddleware({\n entrypoint: \"astro-sessionkit/middleware\",\n order: \"pre\",\n });\n\n // 2. Add route guard if there are protection rules or global protection is enabled\n if ((config.protect && config.protect.length > 0) || config.globalProtect) {\n addMiddleware({\n entrypoint: \"astro-sessionkit/guard\",\n order: \"pre\",\n });\n }\n },\n },\n };\n}\n\n// Re-export types for convenience\nexport type { Session, ProtectionRule, SessionKitConfig } from \"./core/types\";\n"]}
1
+ {"version":3,"file":"integration.js","sources":["../src/integration.ts"],"sourcesContent":["// ============================================================================\n// Astro Integration\n// ============================================================================\n\nimport type { AstroIntegration } from \"astro\";\nimport {getConfig, setConfig} from \"./core/config\";\nimport type { SessionKitConfig } from \"./core/types\";\n\n/**\n * SessionKit - Simple session access and route protection for Astro\n * \n * @example\n * ```ts\n * // astro.config.mjs\n * import sessionkit from 'astro-sessionkit';\n * \n * export default defineConfig({\n * integrations: [\n * sessionkit({\n * loginPath: '/login',\n * protect: [\n * { pattern: '/admin/**', role: 'admin' },\n * { pattern: '/dashboard', roles: ['user', 'admin'] },\n * { pattern: '/settings', permissions: ['settings:write'] }\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport default function sessionKit(config: SessionKitConfig = {}): AstroIntegration {\n // Store configuration\n setConfig(config);\n const resolvedConfig = getConfig();\n\n return {\n name: \"astro-sessionkit\",\n hooks: {\n \"astro:config:setup\": ({ addMiddleware }) => {\n // 1. Always add session context middleware first\n addMiddleware({\n entrypoint: \"astro-sessionkit/middleware\",\n order: \"pre\",\n });\n\n // 2. Add route guard if there are protection rules or global protection is enabled\n const hasRules = (resolvedConfig.protect && resolvedConfig.protect.length > 0);\n const isGlobal = !!resolvedConfig.globalProtect;\n\n if (hasRules || isGlobal) {\n addMiddleware({\n entrypoint: \"astro-sessionkit/guard\",\n order: \"pre\",\n });\n } else if (resolvedConfig.debug) {\n console.log(\"[SessionKit] Route guard NOT registered: no rules and globalProtect is false.\");\n }\n },\n },\n };\n}\n\nexport type {\n Session,\n ProtectionRule,\n RoleProtectionRule,\n RolesProtectionRule,\n PermissionProtectionRule,\n PermissionsProtectionRule,\n CustomProtectionRule,\n SessionKitConfig,\n AccessHooks,\n SessionContext\n} from \"./core/types\";\n"],"names":[],"mappings":";;AA8Bc,SAAU,UAAU,CAAC,SAA2B,EAAE,EAAA;IAE9D,SAAS,CAAC,MAAM,CAAC;AACjB,IAAA,MAAM,cAAc,GAAG,SAAS,EAAE;IAElC,OAAO;AACL,QAAA,IAAI,EAAE,kBAAkB;AACxB,QAAA,KAAK,EAAE;AACL,YAAA,oBAAoB,EAAE,CAAC,EAAE,aAAa,EAAE,KAAI;AAE1C,gBAAA,aAAa,CAAC;AACZ,oBAAA,UAAU,EAAE,6BAA6B;AACzC,oBAAA,KAAK,EAAE,KAAK;AACb,iBAAA,CAAC;AAGF,gBAAA,MAAM,QAAQ,IAAI,cAAc,CAAC,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;AAC9E,gBAAA,MAAM,QAAQ,GAAG,CAAC,CAAC,cAAc,CAAC,aAAa;AAE/C,gBAAA,IAAI,QAAQ,IAAI,QAAQ,EAAE;AACxB,oBAAA,aAAa,CAAC;AACZ,wBAAA,UAAU,EAAE,wBAAwB;AACpC,wBAAA,KAAK,EAAE,KAAK;AACb,qBAAA,CAAC;gBACJ;AAAO,qBAAA,IAAI,cAAc,CAAC,KAAK,EAAE;AAC/B,oBAAA,OAAO,CAAC,GAAG,CAAC,+EAA+E,CAAC;gBAC9F;YACF,CAAC;AACF,SAAA;KACF;AACH;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-sessionkit",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
4
4
  "description": "Simple session access and route protection for Astro applications",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -76,11 +76,6 @@ export function createGuardMiddleware(): MiddlewareHandler {
76
76
  return async (context : APIContext, next) => {
77
77
  const { protect, loginPath, globalProtect, exclude } = getConfig();
78
78
 
79
- // No rules configured and no global protect - skip
80
- if (protect.length === 0 && !globalProtect) {
81
- return next();
82
- }
83
-
84
79
  let pathname: string;
85
80
  try {
86
81
  pathname = new URL(context.request.url).pathname;
@@ -88,13 +83,24 @@ export function createGuardMiddleware(): MiddlewareHandler {
88
83
  // Fallback if URL is invalid (unlikely in Astro)
89
84
  pathname = "/";
90
85
  }
86
+
87
+ logger.debug(`[Guard] Pathname: ${pathname}, GlobalProtect: ${globalProtect}, Rules: ${protect.length}`);
88
+
89
+ // No rules configured and no global protect - skip
90
+ if (protect.length === 0 && !globalProtect) {
91
+ logger.debug(`[Guard] Skipping ${pathname} because no rules are configured and globalProtect is false`);
92
+ return next();
93
+ }
94
+
91
95
  const sessionContext = getContextStore();
92
96
  const session = sessionContext?.session ?? null;
93
97
 
94
98
  // Find matching rule
95
99
  const rule = protect.find((r) => matchesPattern(r.pattern, pathname));
96
100
 
97
- logger.debug(`[Guard] Pathname: ${pathname}, Found rule: ${rule ? JSON.stringify(rule) : 'none'}, GlobalProtect: ${globalProtect}`);
101
+ if (rule) {
102
+ logger.debug(`[Guard] Found matching rule for ${pathname}:`, rule);
103
+ }
98
104
 
99
105
  // No matching rule - check global protection
100
106
  if (!rule) {
@@ -6,7 +6,7 @@ import { getConfig } from "./config";
6
6
  export function debug(message: string, ...args: any[]): void {
7
7
  const { debug } = getConfig();
8
8
  if (debug) {
9
- console.debug(`[SessionKit] ${message}`, ...args);
9
+ console.log(`[SessionKit] ${message}`, ...args);
10
10
  }
11
11
  }
12
12
 
package/src/index.ts CHANGED
@@ -2,57 +2,9 @@
2
2
  // Astro SessionKit - Main Integration Entry Point
3
3
  // ============================================================================
4
4
 
5
- import type {AstroIntegration } from "astro";
6
- import { setConfig } from "./core/config";
7
- import type { SessionKitConfig } from "./core/types";
5
+ import sessionkit from "./integration";
8
6
 
9
- /**
10
- * SessionKit - Simple session access and route protection for Astro
11
- *
12
- * @example
13
- * ```ts
14
- * // astro.config.mjs
15
- * import sessionkit from 'astro-sessionkit';
16
- *
17
- * export default defineConfig({
18
- * integrations: [
19
- * sessionkit({
20
- * loginPath: '/login',
21
- * protect: [
22
- * { pattern: '/admin/**', role: 'admin' },
23
- * { pattern: '/dashboard', roles: ['user', 'admin'] },
24
- * { pattern: '/settings', permissions: ['settings:write'] }
25
- * ]
26
- * })
27
- * ]
28
- * });
29
- * ```
30
- */
31
- export default function sessionkit(config: SessionKitConfig = {}): AstroIntegration {
32
- // Store configuration
33
- setConfig(config);
34
-
35
- return {
36
- name: "astro-sessionkit",
37
- hooks: {
38
- "astro:config:setup": ({ addMiddleware }) => {
39
- // 1. Always add session context middleware first
40
- addMiddleware({
41
- entrypoint: "astro-sessionkit/middleware",
42
- order: "pre",
43
- });
44
-
45
- // 2. Add route guard if there are protection rules or global protection is enabled
46
- if ((config.protect && config.protect.length > 0) || config.globalProtect) {
47
- addMiddleware({
48
- entrypoint: "astro-sessionkit/guard",
49
- order: "pre",
50
- });
51
- }
52
- },
53
- },
54
- };
55
- }
7
+ export default sessionkit;
56
8
 
57
9
  // ============================================================================
58
10
  // Re-export types for convenience
@@ -75,4 +27,4 @@ export type {
75
27
  // Version export
76
28
  // ============================================================================
77
29
 
78
- export const version = "0.1.0";
30
+ export const version = "0.1.20";
@@ -3,7 +3,7 @@
3
3
  // ============================================================================
4
4
 
5
5
  import type { AstroIntegration } from "astro";
6
- import { setConfig } from "./core/config";
6
+ import {getConfig, setConfig} from "./core/config";
7
7
  import type { SessionKitConfig } from "./core/types";
8
8
 
9
9
  /**
@@ -31,6 +31,7 @@ import type { SessionKitConfig } from "./core/types";
31
31
  export default function sessionKit(config: SessionKitConfig = {}): AstroIntegration {
32
32
  // Store configuration
33
33
  setConfig(config);
34
+ const resolvedConfig = getConfig();
34
35
 
35
36
  return {
36
37
  name: "astro-sessionkit",
@@ -43,16 +44,31 @@ export default function sessionKit(config: SessionKitConfig = {}): AstroIntegrat
43
44
  });
44
45
 
45
46
  // 2. Add route guard if there are protection rules or global protection is enabled
46
- if ((config.protect && config.protect.length > 0) || config.globalProtect) {
47
+ const hasRules = (resolvedConfig.protect && resolvedConfig.protect.length > 0);
48
+ const isGlobal = !!resolvedConfig.globalProtect;
49
+
50
+ if (hasRules || isGlobal) {
47
51
  addMiddleware({
48
52
  entrypoint: "astro-sessionkit/guard",
49
53
  order: "pre",
50
54
  });
55
+ } else if (resolvedConfig.debug) {
56
+ console.log("[SessionKit] Route guard NOT registered: no rules and globalProtect is false.");
51
57
  }
52
58
  },
53
59
  },
54
60
  };
55
61
  }
56
62
 
57
- // Re-export types for convenience
58
- export type { Session, ProtectionRule, SessionKitConfig } from "./core/types";
63
+ export type {
64
+ Session,
65
+ ProtectionRule,
66
+ RoleProtectionRule,
67
+ RolesProtectionRule,
68
+ PermissionProtectionRule,
69
+ PermissionsProtectionRule,
70
+ CustomProtectionRule,
71
+ SessionKitConfig,
72
+ AccessHooks,
73
+ SessionContext
74
+ } from "./core/types";