Skip to content
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

Add params to override the login and authorize paths #2648

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/tricky-mails-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@shopify/hydrogen': patch
---

Add params to override the login and authorize paths:

```ts
const hydrogenContext = createHydrogenContext({
// ...
customerAccount: {
loginPath = '/account/login',
authorizePath = '/account/authorize',
defaultRedirectPath = '/account',
},
});
```
24 changes: 13 additions & 11 deletions packages/hydrogen/src/customer/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ import type {
import {createCustomerAccountHelper, URL_TYPE} from './customer-account-helper';
import {warnOnce} from '../utils/warning';

const DEFAULT_LOGIN_URL = '/account/login';
const DEFAULT_AUTH_URL = '/account/authorize';
const DEFAULT_REDIRECT_PATH = '/account';

function defaultAuthStatusHandler(request: CrossRuntimeRequest) {
if (!request.url) return DEFAULT_LOGIN_URL;
function defaultAuthStatusHandler(
request: CrossRuntimeRequest,
defaultLoginUrl: string,
) {
if (!request.url) return defaultLoginUrl;

const {pathname} = new URL(request.url);

const redirectTo =
DEFAULT_LOGIN_URL +
defaultLoginUrl +
`?${new URLSearchParams({return_to: pathname}).toString()}`;

return redirect(redirectTo);
Expand All @@ -79,6 +78,9 @@ export function createCustomerAccountClient({
customAuthStatusHandler,
logErrors = true,
unstableB2b = false,
loginPath = '/account/login',
authorizePath = '/account/authorize',
defaultRedirectPath = '/account',
}: CustomerAccountOptions): CustomerAccount {
if (customerApiVersion !== DEFAULT_CUSTOMER_API_VERSION) {
console.warn(
Expand Down Expand Up @@ -106,7 +108,7 @@ export function createCustomerAccountClient({

const authStatusHandler = customAuthStatusHandler
? customAuthStatusHandler
: () => defaultAuthStatusHandler(request);
: () => defaultAuthStatusHandler(request, loginPath);

const requestUrl = new URL(request.url);
const httpsOrigin =
Expand All @@ -115,7 +117,7 @@ export function createCustomerAccountClient({
: requestUrl.origin;
const redirectUri = ensureLocalRedirectUrl({
requestUrl: httpsOrigin,
defaultUrl: DEFAULT_AUTH_URL,
defaultUrl: authorizePath,
redirectUrl: authUrl,
});

Expand Down Expand Up @@ -400,7 +402,7 @@ export function createCustomerAccountClient({
redirectPath:
getRedirectUrl(request.url) ||
getHeader(request, 'Referer') ||
DEFAULT_REDIRECT_PATH,
defaultRedirectPath,
});

loginUrl.searchParams.append('code_challenge', challenge);
Expand Down Expand Up @@ -565,7 +567,7 @@ export function createCustomerAccountClient({

await exchangeForStorefrontCustomerAccessToken();

return redirect(redirectPath || DEFAULT_REDIRECT_PATH);
return redirect(redirectPath || defaultRedirectPath);
},
UNSTABLE_setBuyer: setBuyer,
UNSTABLE_getBuyer: getBuyer,
Expand Down
6 changes: 6 additions & 0 deletions packages/hydrogen/src/customer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export type CustomerAccountOptions = {
logErrors?: boolean | ((error?: Error) => boolean);
/** UNSTABLE feature, this will eventually goes away. If true then we will exchange customerAccessToken for storefrontCustomerAccessToken. */
unstableB2b?: boolean;
/** The path to redirect to after login. Defaults to `/account`. */
defaultRedirectPath?: string;
/** The path to login. Defaults to `/account/login`. */
loginPath?: string;
/** The oauth authorize path. Defaults to `/account/authorize`. */
authorizePath?: string;
};

/** Below are types meant for documentation only. Ensure it stay in sync with the type above. */
Expand Down
Loading