Skip to content

Commit

Permalink
Add params to override the login and authorize paths
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Nov 18, 2024
1 parent 2a978cd commit 1a1bd89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
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',
},
});
```
26 changes: 14 additions & 12 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 @@ -430,7 +432,7 @@ export function createCustomerAccountClient({

clearSession(session);

return redirect(logoutUrl, {headers: options?.headers || {}});
return redirect(logoutUrl);
},
isLoggedIn,
handleAuthStatus,
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

0 comments on commit 1a1bd89

Please sign in to comment.