redirect-path
v1.0.1
Published
Utility to extract the redirect path from a callback URL and base path.
Maintainers
Readme
redirect-path
Utility functions for working with URL paths. Primarily used to extract the portion of a callback URL that comes after a given base path—useful in authentication flows, redirects, and multi-tenant routing.
Installation
npm install redirect-pathUsage
import { extractRedirectPath, prependBasePath } from "redirect-path";
const redirectedPath = extractRedirectPath(
"http://localhost/my/app/tasks/42",
"/my/app"
);
console.log(redirectedPath);
// "/tasks/42"
const fullPath = prependBasePath("tasks/42", "/my/app");
console.log(fullPath);
// "/my/app/tasks/42"API Reference
extractRedirectPath(callbackUrl: string, basePath?: string): string
Extracts the portion of the URL path that comes after the base path.
Parameters
| Name | Type | Description |
| ------------- | -------- | --------------------------------------------------------------------------------- |
| callbackUrl | string | The full URL containing the path you want to extract from. |
| basePath | string | Optional base path prefix. If omitted, the function uses NEXT_PUBLIC_BASE_PATH. |
Returns
- A string beginning with
/representing the remaining path. - Returns
""if the base path does not match.
Example
extractRedirectPath("http://localhost/app/users/profile", "/app");
// "/users/profile"prependBasePath(filePath: string, basePath?: string): string
Safely prefixes a base path to any file or route path.
Parameters
| Name | Type | Description |
| ---------- | -------- | ---------------------------------------------- |
| filePath | string | Route or file path to append to the base path. |
| basePath | string | Optional prefix path. |
Returns
- A normalized path starting with
/.
Example
prependBasePath("users/profile", "/app");
// "/app/users/profile"Why Use This?
- Works with multi-segment base paths
- Handles all slash normalization cases
- Framework-agnostic (Next.js, Express, Node, etc.)
- Reliable for auth flows (
callbackUrl), redirects, and rewrites
