@markwharton/pwa-core 3.4.0 → 3.4.2

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/server.d.ts CHANGED
@@ -458,6 +458,8 @@ export interface SessionAuthConfig {
458
458
  allowedDomain?: string;
459
459
  /** Emails that get isAdmin=true */
460
460
  adminEmails?: string[];
461
+ /** Custom email validation callback (overrides default allowedEmails/allowedDomain check). Supports async for database lookups. */
462
+ isEmailAllowed?: (email: string) => boolean | Promise<boolean>;
461
463
  /** Base URL for magic links and SWA preview URL validation */
462
464
  appBaseUrl?: string;
463
465
  /** Required callback to send magic link emails */
@@ -479,14 +481,15 @@ export declare function initSessionAuth(config: SessionAuthConfig): void;
479
481
  * Initializes session auth from environment variables.
480
482
  * Reads: SESSION_COOKIE_NAME, APP_BASE_URL, ALLOWED_EMAILS, ALLOWED_DOMAIN, ADMIN_EMAILS.
481
483
  * @param sendEmail - Required callback to send magic link emails
484
+ * @param overrides - Optional config overrides (e.g., isEmailAllowed callback)
482
485
  * @throws Error if sendEmail is not provided
483
486
  * @example
484
487
  * initSessionAuthFromEnv(async (to, magicLink) => {
485
488
  * await resend.emails.send({ to, html: `<a href="${magicLink}">Sign In</a>` });
486
489
  * return true;
487
- * });
490
+ * }, { isEmailAllowed: async (email) => lookupInDatabase(email) });
488
491
  */
489
- export declare function initSessionAuthFromEnv(sendEmail: (to: string, magicLink: string) => Promise<boolean>): void;
492
+ export declare function initSessionAuthFromEnv(sendEmail: (to: string, magicLink: string) => Promise<boolean>, overrides?: Partial<Omit<SessionAuthConfig, 'sendEmail'>>): void;
490
493
  /**
491
494
  * Parses cookies from a request's Cookie header.
492
495
  * @param request - Request object with headers.get() method
package/dist/server.js CHANGED
@@ -754,14 +754,15 @@ function initSessionAuth(config) {
754
754
  * Initializes session auth from environment variables.
755
755
  * Reads: SESSION_COOKIE_NAME, APP_BASE_URL, ALLOWED_EMAILS, ALLOWED_DOMAIN, ADMIN_EMAILS.
756
756
  * @param sendEmail - Required callback to send magic link emails
757
+ * @param overrides - Optional config overrides (e.g., isEmailAllowed callback)
757
758
  * @throws Error if sendEmail is not provided
758
759
  * @example
759
760
  * initSessionAuthFromEnv(async (to, magicLink) => {
760
761
  * await resend.emails.send({ to, html: `<a href="${magicLink}">Sign In</a>` });
761
762
  * return true;
762
- * });
763
+ * }, { isEmailAllowed: async (email) => lookupInDatabase(email) });
763
764
  */
764
- function initSessionAuthFromEnv(sendEmail) {
765
+ function initSessionAuthFromEnv(sendEmail, overrides) {
765
766
  const allowedEmailsStr = process.env.ALLOWED_EMAILS;
766
767
  const adminEmailsStr = process.env.ADMIN_EMAILS;
767
768
  initSessionAuth({
@@ -774,6 +775,7 @@ function initSessionAuthFromEnv(sendEmail) {
774
775
  adminEmails: adminEmailsStr
775
776
  ? adminEmailsStr.split(',').map(e => e.trim().toLowerCase())
776
777
  : undefined,
778
+ ...overrides,
777
779
  sendEmail
778
780
  });
779
781
  }
@@ -970,8 +972,11 @@ async function createMagicLink(email, request) {
970
972
  if (!isValidEmail(normalizedEmail)) {
971
973
  return (0, shared_1.err)('Valid email required', shared_1.HTTP_STATUS.BAD_REQUEST);
972
974
  }
973
- // Check allowlist
974
- if (!isEmailAllowed(normalizedEmail)) {
975
+ // Check allowlist (custom callback overrides default)
976
+ const emailAllowed = config.isEmailAllowed
977
+ ? await config.isEmailAllowed(normalizedEmail)
978
+ : isEmailAllowed(normalizedEmail);
979
+ if (!emailAllowed) {
975
980
  return (0, shared_1.err)('Email not allowed', shared_1.HTTP_STATUS.FORBIDDEN);
976
981
  }
977
982
  // Check rate limit
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markwharton/pwa-core",
3
- "version": "3.4.0",
3
+ "version": "3.4.2",
4
4
  "description": "Shared patterns for Azure PWA projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",