Skip to content

Commit

Permalink
Pass in handleClientError method from the strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaansyed committed Jan 15, 2024
1 parent 9f02294 commit d00eb03
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ export function authStrategyFactory<
| EmbeddedAdminContext<ConfigArg, Resources>
| NonEmbeddedAdminContext<ConfigArg, Resources> = {
admin: createAdminApiContext<Resources>(
request,
session,
params,
authStrategy,
authStrategy.handleClientError(request),
),
billing: {
require: requireBillingFactory(params, request, session),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import {Session, ShopifyRestResources} from '@shopify/shopify-api';

import type {BasicParams} from '../../../types';
import {AdminApiContext, adminClientFactory} from '../../../clients/admin';
import {AuthorizationStrategy} from '../strategies/types';

import {handleClientErrorFactory} from './handle-client-error';
import {
AdminApiContext,
HandleAdminClientError,
adminClientFactory,
} from '../../../clients/admin';

export function createAdminApiContext<
Resources extends ShopifyRestResources = ShopifyRestResources,
>(
request: Request,
session: Session,
params: BasicParams,
authStrategy?: AuthorizationStrategy,
handleClientError: HandleAdminClientError,
): AdminApiContext<Resources> {
return adminClientFactory<Resources>({
session,
params,
handleClientError: handleClientErrorFactory({
request,
authStrategy,
}),
handleClientError,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {HandleClientErrorOptions} from '../strategies/types';

export function handleClientErrorFactory({
request,
authStrategy,
onError,
}: HandleClientErrorOptions): HandleAdminClientError {
return async function handleClientError({
error,
Expand All @@ -28,12 +28,8 @@ export function handleClientErrorFactory({
},
);

if (error.response.code === 401 && authStrategy) {
await authStrategy.handleInvalidAccessTokenError({
request,
session,
error,
});
if (onError) {
await onError({request, session, error});
}

// forward a minimal copy of the upstream HTTP response instead of an Error:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ export async function triggerAfterAuthHook<
await config.hooks.afterAuth({
session,
admin: createAdminApiContext<Resources>(
request,
session,
params,
authStrategy,
authStrategy.handleClientError(request),
),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import type {BasicParams} from '../../../types';
import {
beginAuth,
handleClientErrorFactory,
redirectToAuthPage,
redirectToShopifyOrAppRoot,
redirectWithExitIframe,
Expand All @@ -20,12 +21,9 @@ import {
} from '../helpers';
import {AppConfig} from '../../../config-types';
import {getSessionTokenHeader} from '../../helpers';
import {HandleAdminClientError} from '../../../clients';

import {
AuthorizationStrategy,
HandleInvalidAccessTokenOptions,
SessionContext,
} from './types';
import {AuthorizationStrategy, SessionContext, OnErrorOptions} from './types';

export class AuthCodeFlowStrategy<
Resources extends ShopifyRestResources = ShopifyRestResources,
Expand Down Expand Up @@ -91,16 +89,20 @@ export class AuthCodeFlowStrategy<
return session!;
}

public async handleInvalidAccessTokenError({
request,
session,
}: HandleInvalidAccessTokenOptions): Promise<never> {
public handleClientError(request: Request): HandleAdminClientError {
const {api, config, logger} = this;
throw await redirectToAuthPage(
{api, config, logger},
return handleClientErrorFactory({
request,
session.shop,
);
onError: async ({session, error}: OnErrorOptions) => {
if (error.response.code === 401) {
throw await redirectToAuthPage(
{api, config, logger},
request,
session.shop,
);
}
},
});
}

private async ensureInstalledOnShop(request: Request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import {AppConfig, AppConfigArg} from 'src/server/config-types';
import {BasicParams, ApiConfigWithFutureFlags} from 'src/server/types';

import {respondToInvalidSessionToken} from '../../helpers';
import {triggerAfterAuthHook} from '../helpers';
import {handleClientErrorFactory, triggerAfterAuthHook} from '../helpers';
import {HandleAdminClientError} from '../../../clients';

import {
AuthorizationStrategy,
HandleInvalidAccessTokenOptions,
SessionContext,
} from './types';
import {AuthorizationStrategy, SessionContext, OnErrorOptions} from './types';

export class TokenExchangeStrategy<Config extends AppConfigArg>
implements AuthorizationStrategy
Expand Down Expand Up @@ -88,17 +85,20 @@ export class TokenExchangeStrategy<Config extends AppConfigArg>
return session!;
}

public async handleInvalidAccessTokenError({
request,
session,
}: HandleInvalidAccessTokenOptions): Promise<void> {
const {config, api, logger} = this;

config.sessionStorage.deleteSession(session.id);

respondToInvalidSessionToken({
params: {config, api, logger},
public handleClientError(request: Request): HandleAdminClientError {
const {api, config, logger} = this;
return handleClientErrorFactory({
request,
onError: async ({session, error}: OnErrorOptions) => {
if (error.response.code === 401) {
config.sessionStorage.deleteSession(session.id);

respondToInvalidSessionToken({
params: {config, api, logger},
request,
});
}
},
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import {Session} from '@shopify/shopify-api';
import {HttpResponseError, Session} from '@shopify/shopify-api';

import {HandleAdminClientError} from '../../../clients';

export interface SessionContext {
shop: string;
session?: Session;
sessionToken?: string;
}

export interface HandleClientErrorOptions {
export interface OnErrorOptions {
request: Request;
authStrategy?: AuthorizationStrategy;
session: Session;
error: HttpResponseError;
}

export interface HandleInvalidAccessTokenOptions {
export interface HandleClientErrorOptions {
request: Request;
session: Session;
error: any;
onError?: ({session, error}: OnErrorOptions) => Promise<void | never>;
}

export interface AuthorizationStrategy {
respondToOAuthRequests: (request: Request) => Promise<void | never>;
authenticate: (
request: Request,
sessionContext: SessionContext,
) => Promise<Session | never>;
handleInvalidAccessTokenError: ({
request,
session,
}: HandleInvalidAccessTokenOptions) => Promise<void | never>;
handleClientError: (request: Request) => HandleAdminClientError;
}

0 comments on commit d00eb03

Please sign in to comment.