Skip to content

Commit

Permalink
update tsconfig and add HoC functions for next
Browse files Browse the repository at this point in the history
  • Loading branch information
bluebeel committed Dec 7, 2020
1 parent 4436754 commit f3fd534
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
dist
coverage/
.idea/
tsconfig.tsbuildinfo
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import shopifyAuth from "./auth";
import {authenticateShopifyPage, authenticateShopifyAPI, NextShopifyApiRequest} from "./requireAuthentication";

export default shopifyAuth;

Expand All @@ -7,5 +8,4 @@ export * from "./auth";
export * from "./types";

export { default as verifyRequest } from "./verify-request";

export { default as authenticateShopifyPage } from "./requireAuthentication";
export {authenticateShopifyPage, authenticateShopifyAPI, NextShopifyApiRequest};
67 changes: 42 additions & 25 deletions src/requireAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import {
GetServerSidePropsContext,
} from "next";
import { GetServerSidePropsContext } from "next";
import { parseCookies } from "nookies";
import verifyRequest from "./verify-request/verify-request";
import type { NextApiRequest, NextApiResponse } from 'next';

// Overwrite ServerResponse to allow `shopOrigin`
interface GetServerSideShopifyPropsContext extends GetServerSidePropsContext {
shopOrigin?: string;
}


export type GetServerSideProps<
P extends { [key: string]: any } = { [key: string]: any }
> = (context: GetServerSideShopifyPropsContext) => Promise<{ props: P }>;

const authenticateShopifyPage = async (
getServerSidePropsInner: GetServerSideProps = async () => ({ props: {} })
) => {
const getServerSideProps: GetServerSideProps = async (ctx) => {
const pathname = new URL(ctx.resolvedUrl).pathname;

export function authenticateShopifyPage(getServerSidePropsFunc?: Function) {
return async (ctx: GetServerSideShopifyPropsContext) => {
const authRoute = "/api/shopify/auth";
const fallbackRoute = "/login";
const verifyTokenUrl = `${process.env.HOST}/api/shopify/verify-token`;
const cookies = parseCookies(ctx);
const shopOrigin = ctx.query.shop ?? cookies.shopOrigin;

if (pathname !== fallbackRoute) {
if (ctx.resolvedUrl !== fallbackRoute) {
await verifyRequest({
query: ctx.query,
cookies,
Expand All @@ -36,17 +26,44 @@ const authenticateShopifyPage = async (
}

ctx.shopOrigin = shopOrigin as string;
if (getServerSidePropsFunc) {
const result = await getServerSidePropsFunc(ctx);

const result = await getServerSidePropsInner(ctx);

return {
props: {
shopOrigin,
...result.props,
},
};
return {
props: {
shopOrigin,
...result.props,
},
};
}
return { props: { shopOrigin } };
};
return getServerSideProps;
}

export type NextShopifyApiRequest = NextApiRequest & {
shopOrigin?: string;
shopifyToken?: string;
shopifyAssociatedUser?: string;
}

export const authenticateShopifyAPI = (handler: (req: NextShopifyApiRequest, res: NextApiResponse) => Promise<void>) => async (req: NextShopifyApiRequest, res: NextApiResponse) => {
if (!req) {
throw new Error('Request is not available');
}

if (!res) {
throw new Error('Response is not available');
}

const authRoute = "/api/shopify/auth";
const fallbackRoute = "/login";
const verifyTokenUrl = `${process.env.HOST}/api/shopify/verify-token`;
await verifyRequest({ query: req.query, cookies: req.cookies, res, options: { authRoute, fallbackRoute, verifyTokenUrl } });

req.shopOrigin = req.cookies.shopOrigin;
req.shopifyToken = req.cookies.shopifyToken;
req.shopifyAssociatedUser = req.cookies.shopifyAssociatedUser;

return handler(req, res);
};

export default authenticateShopifyPage;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"declaration": true,
"declarationMap": true,
"skipLibCheck": true,
"downlevelIteration": true,
"downlevelIteration": true
},
"exclude": ["node_modules", "dist"]
}

0 comments on commit f3fd534

Please sign in to comment.