hazo_auth 10.4.7 → 10.4.9

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.
@@ -947,10 +947,10 @@ Google OAuth Sign-In allows users to authenticate with their Google accounts. Th
947
947
  5. Configure OAuth consent screen if prompted
948
948
  6. Set **Application type** to "Web application"
949
949
  7. Add **Authorized JavaScript origins**:
950
- - Development: `http://localhost:3000`
950
+ - Development: `http://localhost:<YOUR_PORT>`
951
951
  - Production: `https://yourdomain.com`
952
- 8. Add **Authorized redirect URIs**:
953
- - Development: `http://localhost:3000/api/auth/callback/google`
952
+ 8. Add **Authorized redirect URIs** — the path is fixed and exported as `GOOGLE_NEXTAUTH_CALLBACK_PATH` from `hazo_auth`:
953
+ - Development: `http://localhost:<YOUR_PORT>/api/auth/callback/google`
954
954
  - Production: `https://yourdomain.com/api/auth/callback/google`
955
955
  9. Copy the **Client ID** and **Client Secret**
956
956
 
@@ -12,6 +12,7 @@ import { handle_google_oauth_login } from "../services/oauth_service.js";
12
12
  import { get_hazo_connect_instance } from "../hazo_connect_instance.server.js";
13
13
  import { create_app_logger } from "../app_logger.js";
14
14
  import { store_google_oauth_token, GoogleTokenStorageUnconfigured } from "../services/google_token_service.js";
15
+ import { GOOGLE_OAUTH_CALLBACK_PATH, FACEBOOK_OAUTH_CALLBACK_PATH } from "../oauth_routes.js";
15
16
 
16
17
  // section: types
17
18
  export type NextAuthCallbackUser = {
@@ -102,8 +103,8 @@ export function get_nextauth_config(): AuthOptions {
102
103
  // Always redirect to our custom callback after sign-in to set hazo_auth cookies
103
104
  // The callbackUrl from signIn() comes through as `url`
104
105
  if (
105
- url.includes("/api/hazo_auth/oauth/google/callback") ||
106
- url.includes("/api/hazo_auth/oauth/facebook/callback")
106
+ url.includes(GOOGLE_OAUTH_CALLBACK_PATH) ||
107
+ url.includes(FACEBOOK_OAUTH_CALLBACK_PATH)
107
108
  ) {
108
109
  return url;
109
110
  }
@@ -118,9 +119,9 @@ export function get_nextauth_config(): AuthOptions {
118
119
 
119
120
  // Default: redirect to our custom OAuth callback to set cookies
120
121
  if (url.includes("facebook")) {
121
- return `${baseUrl}/api/hazo_auth/oauth/facebook/callback`;
122
+ return `${baseUrl}${FACEBOOK_OAUTH_CALLBACK_PATH}`;
122
123
  }
123
- return `${baseUrl}/api/hazo_auth/oauth/google/callback`;
124
+ return `${baseUrl}${GOOGLE_OAUTH_CALLBACK_PATH}`;
124
125
  },
125
126
  /**
126
127
  * Sign-in callback - handle user creation/linking for Google OAuth
@@ -174,9 +175,22 @@ export function get_nextauth_config(): AuthOptions {
174
175
  // Store user_id in account for the JWT callback to pick up
175
176
  (account as Record<string, unknown>).hazo_user_id = result.user_id;
176
177
 
177
- // Capture refresh token when extra scopes were granted
178
+ // Capture refresh token when extra scopes were granted.
179
+ // Google echoes the base scopes back in full-URL form
180
+ // (https://www.googleapis.com/auth/userinfo.email|profile), so we
181
+ // normalise to short names before comparing — otherwise every plain
182
+ // login looks like it requested extra scopes and triggers offline
183
+ // token storage that should never run.
178
184
  const BASE_SCOPES = ["openid", "email", "profile"];
179
- const granted_scopes = (account.scope ?? "").split(" ").filter(Boolean);
185
+ const normalise_scope = (s: string): string => {
186
+ if (s === "https://www.googleapis.com/auth/userinfo.email") return "email";
187
+ if (s === "https://www.googleapis.com/auth/userinfo.profile") return "profile";
188
+ return s;
189
+ };
190
+ const granted_scopes = (account.scope ?? "")
191
+ .split(" ")
192
+ .filter(Boolean)
193
+ .map(normalise_scope);
180
194
  const has_extra_scopes = granted_scopes.some(s => !BASE_SCOPES.includes(s));
181
195
 
182
196
  if (account.refresh_token && has_extra_scopes) {
@@ -196,17 +210,23 @@ export function get_nextauth_config(): AuthOptions {
196
210
  });
197
211
  } catch (tokenError) {
198
212
  if (tokenError instanceof GoogleTokenStorageUnconfigured) {
199
- logger.error("nextauth_google_token_storage_unconfigured", {
213
+ // Non-fatal: offline Google API access (refresh-token storage)
214
+ // requires HAZO_AUTH_OAUTH_KEY_* encryption keys. Login itself
215
+ // does not — the user already authenticated. Warn so the admin
216
+ // can configure keys if they want offline access, but let the
217
+ // sign-in complete instead of dead-ending on an error page.
218
+ logger.warn("nextauth_google_token_storage_unconfigured", {
200
219
  user_id: result.user_id,
201
220
  error: tokenError.message,
221
+ impact: "Login succeeded; offline Google API access disabled until HAZO_AUTH_OAUTH_KEY_CURRENT and HAZO_AUTH_OAUTH_KEY_<ID> are set.",
222
+ });
223
+ } else {
224
+ const errorMsg = tokenError instanceof Error ? tokenError.message : String(tokenError);
225
+ logger.error("nextauth_google_token_store_failed", {
226
+ user_id: result.user_id,
227
+ error: errorMsg,
202
228
  });
203
- return "/api/auth/error?error=GoogleTokenStorageUnconfigured";
204
229
  }
205
- const errorMsg = tokenError instanceof Error ? tokenError.message : String(tokenError);
206
- logger.error("nextauth_google_token_store_failed", {
207
- user_id: result.user_id,
208
- error: errorMsg,
209
- });
210
230
  // Non-fatal: continue sign-in even if token storage fails
211
231
  }
212
232
  }
@@ -0,0 +1,13 @@
1
+ // file_description: Fixed OAuth route paths exported for consumer apps and internal use
2
+
3
+ /** NextAuth's built-in OAuth redirect URI — register this in Google Cloud Console. */
4
+ export const GOOGLE_NEXTAUTH_CALLBACK_PATH = "/api/auth/callback/google" as const;
5
+
6
+ /** hazo_auth's custom post-auth handler — creates the hazo_auth session after Google sign-in. */
7
+ export const GOOGLE_OAUTH_CALLBACK_PATH = "/api/hazo_auth/oauth/google/callback" as const;
8
+
9
+ /** NextAuth's built-in OAuth redirect URI — register this in Facebook Developer Console. */
10
+ export const FACEBOOK_NEXTAUTH_CALLBACK_PATH = "/api/auth/callback/facebook" as const;
11
+
12
+ /** hazo_auth's custom post-auth handler — creates the hazo_auth session after Facebook sign-in. */
13
+ export const FACEBOOK_OAUTH_CALLBACK_PATH = "/api/hazo_auth/oauth/facebook/callback" as const;
package/dist/index.d.ts CHANGED
@@ -6,5 +6,6 @@ export { AuthenticationRequiredError, TenantRequiredError, TenantAccessDeniedErr
6
6
  export type { LegalDoc, LegalAcceptanceRecord, LegalAcceptanceMap } from './lib/legal/legal_docs_types';
7
7
  export { cn, merge_class_names } from "./lib/utils.js";
8
8
  export { HAZO_AUTH_PERMISSIONS, ALL_ADMIN_PERMISSIONS, GLOBAL_ADMIN_PERMISSION } from "./lib/constants.js";
9
+ export { GOOGLE_NEXTAUTH_CALLBACK_PATH, GOOGLE_OAUTH_CALLBACK_PATH, FACEBOOK_NEXTAUTH_CALLBACK_PATH, FACEBOOK_OAUTH_CALLBACK_PATH, } from "./lib/oauth_routes.js";
9
10
  export { requestGoogleScopes } from "./lib/auth/request_google_scopes.js";
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAG5C,cAAc,oBAAoB,CAAC;AAGnC,YAAY,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,2BAA2B,EAC3B,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,uBAAuB,CAAC;AAG/B,YAAY,EAAE,QAAQ,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGxG,OAAO,EAAE,EAAE,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGpD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAGxG,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAG5C,cAAc,oBAAoB,CAAC;AAGnC,YAAY,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,eAAe,EACf,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,2BAA2B,EAC3B,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,uBAAuB,CAAC;AAG/B,YAAY,EAAE,QAAQ,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGxG,OAAO,EAAE,EAAE,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGpD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AACxG,OAAO,EACL,6BAA6B,EAC7B,0BAA0B,EAC1B,+BAA+B,EAC/B,4BAA4B,GAC7B,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC"}
package/dist/index.js CHANGED
@@ -14,5 +14,6 @@ export { AuthenticationRequiredError, TenantRequiredError, TenantAccessDeniedErr
14
14
  export { cn, merge_class_names } from "./lib/utils.js";
15
15
  // section: constant_exports
16
16
  export { HAZO_AUTH_PERMISSIONS, ALL_ADMIN_PERMISSIONS, GLOBAL_ADMIN_PERMISSION } from "./lib/constants.js";
17
+ export { GOOGLE_NEXTAUTH_CALLBACK_PATH, GOOGLE_OAUTH_CALLBACK_PATH, FACEBOOK_NEXTAUTH_CALLBACK_PATH, FACEBOOK_OAUTH_CALLBACK_PATH, } from "./lib/oauth_routes.js";
17
18
  // section: google_oauth_exports
18
19
  export { requestGoogleScopes } from "./lib/auth/request_google_scopes.js";
@@ -1 +1 @@
1
- {"version":3,"file":"nextauth_config.d.ts","sourceRoot":"","sources":["../../../src/lib/auth/nextauth_config.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAW,MAAM,WAAW,CAAC;A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;CACvB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AAGF;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,WAAW,CA2RjD;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAW7C"}
@@ -8,6 +8,7 @@ import { handle_google_oauth_login } from "../services/oauth_service.js";
8
8
  import { get_hazo_connect_instance } from "../hazo_connect_instance.server.js";
9
9
  import { create_app_logger } from "../app_logger.js";
10
10
  import { store_google_oauth_token, GoogleTokenStorageUnconfigured } from "../services/google_token_service.js";
11
+ import { GOOGLE_OAUTH_CALLBACK_PATH, FACEBOOK_OAUTH_CALLBACK_PATH } from "../oauth_routes.js";
11
12
  // section: config
12
13
  /**
13
14
  * Gets NextAuth.js configuration with enabled OAuth providers
@@ -60,8 +61,8 @@ export function get_nextauth_config() {
60
61
  console.log("[NextAuth redirect callback]", { url, baseUrl });
61
62
  // Always redirect to our custom callback after sign-in to set hazo_auth cookies
62
63
  // The callbackUrl from signIn() comes through as `url`
63
- if (url.includes("/api/hazo_auth/oauth/google/callback") ||
64
- url.includes("/api/hazo_auth/oauth/facebook/callback")) {
64
+ if (url.includes(GOOGLE_OAUTH_CALLBACK_PATH) ||
65
+ url.includes(FACEBOOK_OAUTH_CALLBACK_PATH)) {
65
66
  return url;
66
67
  }
67
68
  // If URL is relative or same origin, allow it
@@ -73,9 +74,9 @@ export function get_nextauth_config() {
73
74
  }
74
75
  // Default: redirect to our custom OAuth callback to set cookies
75
76
  if (url.includes("facebook")) {
76
- return `${baseUrl}/api/hazo_auth/oauth/facebook/callback`;
77
+ return `${baseUrl}${FACEBOOK_OAUTH_CALLBACK_PATH}`;
77
78
  }
78
- return `${baseUrl}/api/hazo_auth/oauth/google/callback`;
79
+ return `${baseUrl}${GOOGLE_OAUTH_CALLBACK_PATH}`;
79
80
  },
80
81
  /**
81
82
  * Sign-in callback - handle user creation/linking for Google OAuth
@@ -115,9 +116,24 @@ export function get_nextauth_config() {
115
116
  });
116
117
  // Store user_id in account for the JWT callback to pick up
117
118
  account.hazo_user_id = result.user_id;
118
- // Capture refresh token when extra scopes were granted
119
+ // Capture refresh token when extra scopes were granted.
120
+ // Google echoes the base scopes back in full-URL form
121
+ // (https://www.googleapis.com/auth/userinfo.email|profile), so we
122
+ // normalise to short names before comparing — otherwise every plain
123
+ // login looks like it requested extra scopes and triggers offline
124
+ // token storage that should never run.
119
125
  const BASE_SCOPES = ["openid", "email", "profile"];
120
- const granted_scopes = ((_b = account.scope) !== null && _b !== void 0 ? _b : "").split(" ").filter(Boolean);
126
+ const normalise_scope = (s) => {
127
+ if (s === "https://www.googleapis.com/auth/userinfo.email")
128
+ return "email";
129
+ if (s === "https://www.googleapis.com/auth/userinfo.profile")
130
+ return "profile";
131
+ return s;
132
+ };
133
+ const granted_scopes = ((_b = account.scope) !== null && _b !== void 0 ? _b : "")
134
+ .split(" ")
135
+ .filter(Boolean)
136
+ .map(normalise_scope);
121
137
  const has_extra_scopes = granted_scopes.some(s => !BASE_SCOPES.includes(s));
122
138
  if (account.refresh_token && has_extra_scopes) {
123
139
  try {
@@ -137,17 +153,24 @@ export function get_nextauth_config() {
137
153
  }
138
154
  catch (tokenError) {
139
155
  if (tokenError instanceof GoogleTokenStorageUnconfigured) {
140
- logger.error("nextauth_google_token_storage_unconfigured", {
156
+ // Non-fatal: offline Google API access (refresh-token storage)
157
+ // requires HAZO_AUTH_OAUTH_KEY_* encryption keys. Login itself
158
+ // does not — the user already authenticated. Warn so the admin
159
+ // can configure keys if they want offline access, but let the
160
+ // sign-in complete instead of dead-ending on an error page.
161
+ logger.warn("nextauth_google_token_storage_unconfigured", {
141
162
  user_id: result.user_id,
142
163
  error: tokenError.message,
164
+ impact: "Login succeeded; offline Google API access disabled until HAZO_AUTH_OAUTH_KEY_CURRENT and HAZO_AUTH_OAUTH_KEY_<ID> are set.",
165
+ });
166
+ }
167
+ else {
168
+ const errorMsg = tokenError instanceof Error ? tokenError.message : String(tokenError);
169
+ logger.error("nextauth_google_token_store_failed", {
170
+ user_id: result.user_id,
171
+ error: errorMsg,
143
172
  });
144
- return "/api/auth/error?error=GoogleTokenStorageUnconfigured";
145
173
  }
146
- const errorMsg = tokenError instanceof Error ? tokenError.message : String(tokenError);
147
- logger.error("nextauth_google_token_store_failed", {
148
- user_id: result.user_id,
149
- error: errorMsg,
150
- });
151
174
  // Non-fatal: continue sign-in even if token storage fails
152
175
  }
153
176
  }
@@ -0,0 +1,9 @@
1
+ /** NextAuth's built-in OAuth redirect URI — register this in Google Cloud Console. */
2
+ export declare const GOOGLE_NEXTAUTH_CALLBACK_PATH: "/api/auth/callback/google";
3
+ /** hazo_auth's custom post-auth handler — creates the hazo_auth session after Google sign-in. */
4
+ export declare const GOOGLE_OAUTH_CALLBACK_PATH: "/api/hazo_auth/oauth/google/callback";
5
+ /** NextAuth's built-in OAuth redirect URI — register this in Facebook Developer Console. */
6
+ export declare const FACEBOOK_NEXTAUTH_CALLBACK_PATH: "/api/auth/callback/facebook";
7
+ /** hazo_auth's custom post-auth handler — creates the hazo_auth session after Facebook sign-in. */
8
+ export declare const FACEBOOK_OAUTH_CALLBACK_PATH: "/api/hazo_auth/oauth/facebook/callback";
9
+ //# sourceMappingURL=oauth_routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oauth_routes.d.ts","sourceRoot":"","sources":["../../src/lib/oauth_routes.ts"],"names":[],"mappings":"AAEA,sFAAsF;AACtF,eAAO,MAAM,6BAA6B,EAAG,2BAAoC,CAAC;AAElF,iGAAiG;AACjG,eAAO,MAAM,0BAA0B,EAAG,sCAA+C,CAAC;AAE1F,4FAA4F;AAC5F,eAAO,MAAM,+BAA+B,EAAG,6BAAsC,CAAC;AAEtF,mGAAmG;AACnG,eAAO,MAAM,4BAA4B,EAAG,wCAAiD,CAAC"}
@@ -0,0 +1,9 @@
1
+ // file_description: Fixed OAuth route paths exported for consumer apps and internal use
2
+ /** NextAuth's built-in OAuth redirect URI — register this in Google Cloud Console. */
3
+ export const GOOGLE_NEXTAUTH_CALLBACK_PATH = "/api/auth/callback/google";
4
+ /** hazo_auth's custom post-auth handler — creates the hazo_auth session after Google sign-in. */
5
+ export const GOOGLE_OAUTH_CALLBACK_PATH = "/api/hazo_auth/oauth/google/callback";
6
+ /** NextAuth's built-in OAuth redirect URI — register this in Facebook Developer Console. */
7
+ export const FACEBOOK_NEXTAUTH_CALLBACK_PATH = "/api/auth/callback/facebook";
8
+ /** hazo_auth's custom post-auth handler — creates the hazo_auth session after Facebook sign-in. */
9
+ export const FACEBOOK_OAUTH_CALLBACK_PATH = "/api/hazo_auth/oauth/facebook/callback";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hazo_auth",
3
- "version": "10.4.7",
3
+ "version": "10.4.9",
4
4
  "description": "Zero-config authentication UI components for Next.js with RBAC, OAuth, scope-based multi-tenancy, and invitations",
5
5
  "keywords": [
6
6
  "authentication",