-
Notifications
You must be signed in to change notification settings - Fork 48
Refactor: Align Vertex Social Auth with Platform & Improve URL Resilience (vertex app) #3613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b9aa06e
Manage OAuth sign-out flag & refactor API routing
Codebmk de3f297
Update changelog.md
Codebmk 7dcd2ec
Check signed-out flag AFTER consuming OAuth token, not before
Codebmk bdff903
Merge branch 'staging' into fix-login-vertex
Codebmk 46d8749
Update changelog.md
Codebmk 19626e4
Prevent duplicate bootstrap in TokenHandoffHandler
Codebmk a8e0b51
checking shouldSkipBackendOAuthBootstrap() before handling handoff.token
Codebmk bd67908
Update api-routing.ts
Codebmk 3e25dea
Update changelog.md
Codebmk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,124 @@ | ||
| import { getApiBaseUrl } from './envConstants'; | ||
| const DEFAULT_API_VERSION_FALLBACK = 'v2'; | ||
|
|
||
| const ensureLeadingSlash = (value: string): string => { | ||
| if (!value) return ''; | ||
| return value.startsWith('/') ? value : `/${value}`; | ||
| const resolveDefaultApiVersion = (): string => { | ||
| return DEFAULT_API_VERSION_FALLBACK; | ||
| }; | ||
|
|
||
| const resolveServiceVersionMap = (): Record<string, string> => { | ||
| return {}; | ||
| }; | ||
|
|
||
| const stripApiSuffix = (baseUrl: string): string => { | ||
| const trimmedBaseUrl = baseUrl.trim().replace(/\/+$/, ''); | ||
|
|
||
| if (/\/api\/v\d+\/[^/]+$/i.test(trimmedBaseUrl)) { | ||
| return trimmedBaseUrl.replace(/\/api\/v\d+\/[^/]+$/i, ''); | ||
| } | ||
|
|
||
| if (/\/api\/v\d+$/i.test(trimmedBaseUrl)) { | ||
| return trimmedBaseUrl.replace(/\/api\/v\d+$/i, ''); | ||
| } | ||
|
|
||
| if (/\/api$/i.test(trimmedBaseUrl)) { | ||
| return trimmedBaseUrl.replace(/\/api$/i, ''); | ||
| } | ||
|
|
||
| return trimmedBaseUrl; | ||
| }; | ||
|
|
||
| const splitPathAndQuery = (value: string): { path: string; query: string } => { | ||
| const queryStartIndex = value.indexOf('?'); | ||
| if (queryStartIndex === -1) { | ||
| return { path: value, query: '' }; | ||
| } | ||
|
|
||
| return { | ||
| path: value.slice(0, queryStartIndex), | ||
| query: value.slice(queryStartIndex), | ||
| }; | ||
| }; | ||
|
|
||
| const isAbsoluteUrl = (value: string): boolean => { | ||
| return /^https?:\/\//i.test(value); | ||
| }; | ||
|
|
||
| /** | ||
| * Builds an absolute API URL for server-side requests. | ||
| * Uses the environment-configured API base URL. | ||
| * | ||
| * @param inputPath - The path to append to the base URL (e.g. '/users/profile') | ||
| * @returns The absolute URL string | ||
| */ | ||
| export const buildServerApiUrl = (inputPath: string): string => { | ||
| const ensureLeadingSlash = (value: string): string => { | ||
| return value.startsWith('/') ? value : `/${value}`; | ||
| }; | ||
|
|
||
| const isAlreadyVersionedPath = (value: string): boolean => { | ||
| return /^\/?api\/v\d+\//i.test(value) || /^\/?api\/v\d+$/i.test(value); | ||
| }; | ||
|
|
||
| export const resolveApiOrigin = (): string => { | ||
| const configuredBaseUrl = | ||
| process.env.API_BASE_URL || | ||
| process.env.NEXT_PUBLIC_API_BASE_URL || | ||
| process.env.NEXT_PUBLIC_API_URL || | ||
| process.env.NEXT_PUBLIC_BASE_URL || | ||
| ''; | ||
|
|
||
| const normalizedOrigin = stripApiSuffix(configuredBaseUrl); | ||
| if (!normalizedOrigin) { | ||
| throw new Error( | ||
| 'API base URL is not defined. Set NEXT_PUBLIC_API_URL or API_BASE_URL in environment variables.' | ||
| ); | ||
|
Comment on lines
+61
to
+65
|
||
| } | ||
|
|
||
| return normalizedOrigin; | ||
| }; | ||
|
|
||
| export const resolveVersionedApiPath = (inputPath: string): string => { | ||
| const trimmedInput = (inputPath || '').trim(); | ||
|
|
||
| if (!trimmedInput) { | ||
| return `/api/${resolveDefaultApiVersion()}`; | ||
| } | ||
|
|
||
| if (isAbsoluteUrl(trimmedInput)) { | ||
| return trimmedInput; | ||
| } | ||
|
|
||
| const baseUrl = getApiBaseUrl(); // E.g., https://staging-vertex.airqo.net/api/v2 | ||
| return `${baseUrl}${ensureLeadingSlash(trimmedInput)}`; | ||
|
|
||
| const { path, query } = splitPathAndQuery(trimmedInput); | ||
| const normalizedPath = ensureLeadingSlash(path.trim()); | ||
|
|
||
| if (isAlreadyVersionedPath(normalizedPath)) { | ||
| return `${normalizedPath}${query}`; | ||
| } | ||
|
|
||
| const noLeadingSlashPath = normalizedPath.replace(/^\/+/, ''); | ||
| if ( | ||
| /^v\d+\//i.test(noLeadingSlashPath) || | ||
| /^v\d+$/i.test(noLeadingSlashPath) | ||
| ) { | ||
| return `/api/${noLeadingSlashPath}${query}`; | ||
| } | ||
|
|
||
| const segments = noLeadingSlashPath.split('/').filter(Boolean); | ||
| if (segments.length === 0) { | ||
| return `/api/${resolveDefaultApiVersion()}${query}`; | ||
| } | ||
|
|
||
| const service = segments[0].toLowerCase(); | ||
| const serviceVersionMap = resolveServiceVersionMap(); | ||
| const apiVersion = serviceVersionMap[service] || resolveDefaultApiVersion(); | ||
|
|
||
| return `/api/${apiVersion}/${segments.join('/')}${query}`; | ||
| }; | ||
|
|
||
| export const buildServerApiUrl = (inputPath: string): string => { | ||
| const versionedPath = resolveVersionedApiPath(inputPath); | ||
| if (isAbsoluteUrl(versionedPath)) { | ||
| return versionedPath; | ||
| } | ||
|
|
||
| return `${resolveApiOrigin()}${versionedPath}`; | ||
| }; | ||
|
|
||
| /** | ||
| * Builds a relative API URL for browser-side requests. | ||
| * By returning a relative path, the browser natively routes to the current domain, | ||
| * completely eliminating "split-brain" environment variable issues on the client. | ||
| * | ||
| * @param inputPath - The path to format (e.g. '/users/profile') | ||
| * @returns The relative URL string (e.g. '/api/v2/users/profile') | ||
| */ | ||
| export const buildBrowserApiUrl = (inputPath: string): string => { | ||
| const trimmedInput = (inputPath || '').trim(); | ||
|
|
||
| if (isAbsoluteUrl(trimmedInput)) { | ||
| return trimmedInput; | ||
| const versionedPath = resolveVersionedApiPath(inputPath); | ||
| if (isAbsoluteUrl(versionedPath)) { | ||
| return versionedPath; | ||
| } | ||
|
|
||
| // The vertex backend sits under /api/v2 relative to the frontend domain | ||
| return `/api/v2${ensureLeadingSlash(trimmedInput)}`; | ||
|
|
||
| return versionedPath; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.