Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Check auth with API request
Browse files Browse the repository at this point in the history
  • Loading branch information
mllemango committed Apr 13, 2021
1 parent 976e95a commit 77b01ec
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/verify-request/verify-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {TEST_COOKIE_NAME, TOP_LEVEL_OAUTH_COOKIE_NAME} from '../index';
import {Routes} from './types';
import {redirectToAuth} from './utilities';
import {DEFAULT_ACCESS_MODE} from '../auth';
import { HttpResponseError } from '@shopify/shopify-api/dist/error';

export const REAUTH_HEADER = 'X-Shopify-API-Request-Failure-Reauthorize';
export const REAUTH_URL_HEADER = 'X-Shopify-API-Request-Failure-Reauthorize-Url';
Expand All @@ -25,9 +26,21 @@ export function verifyToken(routes: Routes, accessMode: AccessMode = DEFAULT_ACC
const scopesChanged = !Shopify.Context.SCOPES.equals(session.scope);

if (!scopesChanged && session.accessToken && (!session.expires || session.expires >= new Date())) {
ctx.cookies.set(TOP_LEVEL_OAUTH_COOKIE_NAME);
await next();
return;
try {
// make a request to make sure oauth has succeeded, retry otherwise
const client = new Shopify.Clients.Rest(session.shop, session.accessToken)
await client.get({ path: "metafields" })

ctx.cookies.set(TOP_LEVEL_OAUTH_COOKIE_NAME);
await next();
return;
} catch(e) {
if (e instanceof HttpResponseError && e.code == 401){
// only catch 401 errors
} else {
throw e
}
}
}
}

Expand Down

0 comments on commit 77b01ec

Please sign in to comment.