hazo_auth 5.1.30 → 5.1.31
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/cli-src/lib/utils/get_origin_url.ts +61 -0
- package/dist/lib/utils/get_origin_url.d.ts +23 -0
- package/dist/lib/utils/get_origin_url.d.ts.map +1 -0
- package/dist/lib/utils/get_origin_url.js +57 -0
- package/dist/server/routes/oauth_google_callback.d.ts.map +1 -1
- package/dist/server/routes/oauth_google_callback.js +5 -4
- package/package.json +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// file_description: Resolves the public-facing origin URL for constructing redirects
|
|
2
|
+
// When running behind a reverse proxy (Cloudflare, nginx), request.url resolves to
|
|
3
|
+
// the internal address (e.g. http://localhost:3000). This utility returns the correct
|
|
4
|
+
// public origin using NEXTAUTH_URL, falling back to request.url.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Gets the public-facing origin URL for redirect construction.
|
|
8
|
+
*
|
|
9
|
+
* Behind reverse proxies (Cloudflare, nginx, etc.), `request.url` contains the
|
|
10
|
+
* internal address (e.g. `http://localhost:3000`), not the public domain.
|
|
11
|
+
* This function returns the correct origin from environment variables.
|
|
12
|
+
*
|
|
13
|
+
* Priority: NEXTAUTH_URL > APP_DOMAIN_NAME > NEXT_PUBLIC_APP_URL > APP_URL > request.url
|
|
14
|
+
*
|
|
15
|
+
* @param request_url - The request.url to use as fallback
|
|
16
|
+
* @returns The origin URL (e.g. "https://gotimer.org")
|
|
17
|
+
*/
|
|
18
|
+
export function get_origin_url(request_url: string): string {
|
|
19
|
+
// NEXTAUTH_URL is the standard for NextAuth.js apps
|
|
20
|
+
const nextauth_url = process.env.NEXTAUTH_URL;
|
|
21
|
+
if (nextauth_url) {
|
|
22
|
+
return nextauth_url.replace(/\/$/, "");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// APP_DOMAIN_NAME (with protocol handling)
|
|
26
|
+
const app_domain = process.env.APP_DOMAIN_NAME;
|
|
27
|
+
if (app_domain) {
|
|
28
|
+
const domain = app_domain.trim();
|
|
29
|
+
if (domain.startsWith("http://") || domain.startsWith("https://")) {
|
|
30
|
+
return domain.replace(/\/$/, "");
|
|
31
|
+
}
|
|
32
|
+
return `https://${domain}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Other common env vars
|
|
36
|
+
const env_url = process.env.NEXT_PUBLIC_APP_URL || process.env.APP_URL;
|
|
37
|
+
if (env_url) {
|
|
38
|
+
return env_url.replace(/\/$/, "");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Fallback to request.url (works in development without a proxy)
|
|
42
|
+
try {
|
|
43
|
+
const url = new URL(request_url);
|
|
44
|
+
return url.origin;
|
|
45
|
+
} catch {
|
|
46
|
+
return request_url;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a URL using the public-facing origin instead of request.url.
|
|
52
|
+
* Drop-in replacement for `new URL(path, request.url)` in route handlers.
|
|
53
|
+
*
|
|
54
|
+
* @param path - The path or relative URL (e.g. "/hazo_auth/login")
|
|
55
|
+
* @param request_url - The request.url (used as fallback only)
|
|
56
|
+
* @returns A URL object with the correct public origin
|
|
57
|
+
*/
|
|
58
|
+
export function create_redirect_url(path: string, request_url: string): URL {
|
|
59
|
+
const origin = get_origin_url(request_url);
|
|
60
|
+
return new URL(path, origin);
|
|
61
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the public-facing origin URL for redirect construction.
|
|
3
|
+
*
|
|
4
|
+
* Behind reverse proxies (Cloudflare, nginx, etc.), `request.url` contains the
|
|
5
|
+
* internal address (e.g. `http://localhost:3000`), not the public domain.
|
|
6
|
+
* This function returns the correct origin from environment variables.
|
|
7
|
+
*
|
|
8
|
+
* Priority: NEXTAUTH_URL > APP_DOMAIN_NAME > NEXT_PUBLIC_APP_URL > APP_URL > request.url
|
|
9
|
+
*
|
|
10
|
+
* @param request_url - The request.url to use as fallback
|
|
11
|
+
* @returns The origin URL (e.g. "https://gotimer.org")
|
|
12
|
+
*/
|
|
13
|
+
export declare function get_origin_url(request_url: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a URL using the public-facing origin instead of request.url.
|
|
16
|
+
* Drop-in replacement for `new URL(path, request.url)` in route handlers.
|
|
17
|
+
*
|
|
18
|
+
* @param path - The path or relative URL (e.g. "/hazo_auth/login")
|
|
19
|
+
* @param request_url - The request.url (used as fallback only)
|
|
20
|
+
* @returns A URL object with the correct public origin
|
|
21
|
+
*/
|
|
22
|
+
export declare function create_redirect_url(path: string, request_url: string): URL;
|
|
23
|
+
//# sourceMappingURL=get_origin_url.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get_origin_url.d.ts","sourceRoot":"","sources":["../../../src/lib/utils/get_origin_url.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CA8B1D;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,GAAG,CAG1E"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// file_description: Resolves the public-facing origin URL for constructing redirects
|
|
2
|
+
// When running behind a reverse proxy (Cloudflare, nginx), request.url resolves to
|
|
3
|
+
// the internal address (e.g. http://localhost:3000). This utility returns the correct
|
|
4
|
+
// public origin using NEXTAUTH_URL, falling back to request.url.
|
|
5
|
+
/**
|
|
6
|
+
* Gets the public-facing origin URL for redirect construction.
|
|
7
|
+
*
|
|
8
|
+
* Behind reverse proxies (Cloudflare, nginx, etc.), `request.url` contains the
|
|
9
|
+
* internal address (e.g. `http://localhost:3000`), not the public domain.
|
|
10
|
+
* This function returns the correct origin from environment variables.
|
|
11
|
+
*
|
|
12
|
+
* Priority: NEXTAUTH_URL > APP_DOMAIN_NAME > NEXT_PUBLIC_APP_URL > APP_URL > request.url
|
|
13
|
+
*
|
|
14
|
+
* @param request_url - The request.url to use as fallback
|
|
15
|
+
* @returns The origin URL (e.g. "https://gotimer.org")
|
|
16
|
+
*/
|
|
17
|
+
export function get_origin_url(request_url) {
|
|
18
|
+
// NEXTAUTH_URL is the standard for NextAuth.js apps
|
|
19
|
+
const nextauth_url = process.env.NEXTAUTH_URL;
|
|
20
|
+
if (nextauth_url) {
|
|
21
|
+
return nextauth_url.replace(/\/$/, "");
|
|
22
|
+
}
|
|
23
|
+
// APP_DOMAIN_NAME (with protocol handling)
|
|
24
|
+
const app_domain = process.env.APP_DOMAIN_NAME;
|
|
25
|
+
if (app_domain) {
|
|
26
|
+
const domain = app_domain.trim();
|
|
27
|
+
if (domain.startsWith("http://") || domain.startsWith("https://")) {
|
|
28
|
+
return domain.replace(/\/$/, "");
|
|
29
|
+
}
|
|
30
|
+
return `https://${domain}`;
|
|
31
|
+
}
|
|
32
|
+
// Other common env vars
|
|
33
|
+
const env_url = process.env.NEXT_PUBLIC_APP_URL || process.env.APP_URL;
|
|
34
|
+
if (env_url) {
|
|
35
|
+
return env_url.replace(/\/$/, "");
|
|
36
|
+
}
|
|
37
|
+
// Fallback to request.url (works in development without a proxy)
|
|
38
|
+
try {
|
|
39
|
+
const url = new URL(request_url);
|
|
40
|
+
return url.origin;
|
|
41
|
+
}
|
|
42
|
+
catch (_a) {
|
|
43
|
+
return request_url;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates a URL using the public-facing origin instead of request.url.
|
|
48
|
+
* Drop-in replacement for `new URL(path, request.url)` in route handlers.
|
|
49
|
+
*
|
|
50
|
+
* @param path - The path or relative URL (e.g. "/hazo_auth/login")
|
|
51
|
+
* @param request_url - The request.url (used as fallback only)
|
|
52
|
+
* @returns A URL object with the correct public origin
|
|
53
|
+
*/
|
|
54
|
+
export function create_redirect_url(path, request_url) {
|
|
55
|
+
const origin = get_origin_url(request_url);
|
|
56
|
+
return new URL(path, origin);
|
|
57
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oauth_google_callback.d.ts","sourceRoot":"","sources":["../../../src/server/routes/oauth_google_callback.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"oauth_google_callback.d.ts","sourceRoot":"","sources":["../../../src/server/routes/oauth_google_callback.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAwBxD;;;;GAIG;AACH,wBAAsB,GAAG,CAAC,OAAO,EAAE,WAAW,kCAgJ7C"}
|
|
@@ -10,6 +10,7 @@ import { get_cookie_name, get_cookie_options, BASE_COOKIE_NAMES } from "../../li
|
|
|
10
10
|
import { get_hazo_connect_instance } from "../../lib/hazo_connect_instance.server.js";
|
|
11
11
|
import { get_post_login_redirect } from "../../lib/services/post_verification_service.js";
|
|
12
12
|
import { get_oauth_config } from "../../lib/oauth_config.server.js";
|
|
13
|
+
import { create_redirect_url } from "../../lib/utils/get_origin_url.js";
|
|
13
14
|
// section: api_handler
|
|
14
15
|
/**
|
|
15
16
|
* Handles the OAuth callback after Google sign-in
|
|
@@ -36,7 +37,7 @@ export async function GET(request) {
|
|
|
36
37
|
note: "No NextAuth token found - user may not have completed Google sign-in",
|
|
37
38
|
});
|
|
38
39
|
// Redirect to login with error
|
|
39
|
-
const login_url =
|
|
40
|
+
const login_url = create_redirect_url("/hazo_auth/login", request.url);
|
|
40
41
|
login_url.searchParams.set("error", "oauth_failed");
|
|
41
42
|
return NextResponse.redirect(login_url);
|
|
42
43
|
}
|
|
@@ -49,7 +50,7 @@ export async function GET(request) {
|
|
|
49
50
|
has_hazo_user_id: !!token.hazo_user_id,
|
|
50
51
|
has_google_id: !!token.google_id,
|
|
51
52
|
});
|
|
52
|
-
const login_url =
|
|
53
|
+
const login_url = create_redirect_url("/hazo_auth/login", request.url);
|
|
53
54
|
login_url.searchParams.set("error", "oauth_incomplete");
|
|
54
55
|
return NextResponse.redirect(login_url);
|
|
55
56
|
}
|
|
@@ -93,7 +94,7 @@ export async function GET(request) {
|
|
|
93
94
|
invitation_table_error,
|
|
94
95
|
});
|
|
95
96
|
// Create redirect response
|
|
96
|
-
const redirect_url =
|
|
97
|
+
const redirect_url = create_redirect_url(determined_redirect, request.url);
|
|
97
98
|
const response = NextResponse.redirect(redirect_url);
|
|
98
99
|
// Set authentication cookies (same as login route, with configurable prefix and domain)
|
|
99
100
|
const base_cookie_options = {
|
|
@@ -133,7 +134,7 @@ export async function GET(request) {
|
|
|
133
134
|
error_message,
|
|
134
135
|
error_stack,
|
|
135
136
|
});
|
|
136
|
-
const login_url =
|
|
137
|
+
const login_url = create_redirect_url("/hazo_auth/login", request.url);
|
|
137
138
|
login_url.searchParams.set("error", "oauth_error");
|
|
138
139
|
return NextResponse.redirect(login_url);
|
|
139
140
|
}
|
package/package.json
CHANGED