Skip to content

Commit 2ad4710

Browse files
author
Tyler Broadway
committed
feat(common): migrate-to-signed-payload-jwt
1 parent fb2b510 commit 2ad4710

File tree

6 files changed

+54
-21
lines changed

6 files changed

+54
-21
lines changed

lib/auth.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import db from './db';
77
const { AUTH_CALLBACK, CLIENT_ID, CLIENT_SECRET, JWT_KEY } = process.env;
88

99
// Create BigCommerce instance
10-
// https://github.com/getconversio/node-bigcommerce
10+
// https://github.com/bigcommerce/node-bigcommerce/
1111
const bigcommerce = new BigCommerce({
1212
logLevel: 'info',
1313
clientId: CLIENT_ID,
1414
secret: CLIENT_SECRET,
1515
callback: AUTH_CALLBACK,
1616
responseType: 'json',
1717
headers: { 'Accept-Encoding': '*' },
18-
apiVersion: 'v3'
18+
apiVersion: 'v3',
1919
});
2020

2121
const bigcommerceSigned = new BigCommerce({
2222
secret: CLIENT_SECRET,
23-
responseType: 'json'
23+
responseType: 'json',
2424
});
2525

2626
export function bigcommerceClient(accessToken: string, storeHash: string) {
@@ -29,16 +29,16 @@ export function bigcommerceClient(accessToken: string, storeHash: string) {
2929
accessToken,
3030
storeHash,
3131
responseType: 'json',
32-
apiVersion: 'v3'
32+
apiVersion: 'v3',
3333
});
3434
}
3535
// Authorizes app on install
3636
export function getBCAuth(query: QueryParams) {
3737
return bigcommerce.authorize(query);
3838
}
3939
// Verifies app on load/ uninstall
40-
export function getBCVerify({ signed_payload }: QueryParams) {
41-
return bigcommerceSigned.verify(signed_payload);
40+
export function getBCVerify({ signed_payload_jwt }: QueryParams) {
41+
return bigcommerceSigned.verifyJWT(signed_payload_jwt);
4242
}
4343

4444
export function setSession(session: SessionProps) {
@@ -64,7 +64,8 @@ export async function getSession({ query: { context = '' } }: NextApiRequest) {
6464

6565
// JWT functions to sign/ verify 'context' query param from /api/auth||load
6666
export function encodePayload({ user, owner, ...session }: SessionProps) {
67-
const context = session?.context?.split('/')[1] || '';
67+
const contextString = session?.context ?? session?.sub;
68+
const context = contextString.split('/')[1] || '';
6869

6970
return jwt.sign({ context, user, owner }, JWT_KEY, { expiresIn: '24h' });
7071
}

lib/dbs/firebase.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ export async function setUser({ user }: SessionProps) {
3939
}
4040

4141
export async function setStore(session: SessionProps) {
42-
const { access_token: accessToken, context, scope, user: { id } } = session;
42+
const {
43+
access_token: accessToken,
44+
context,
45+
scope,
46+
user: { id },
47+
} = session;
4348
// Only set on app install or update
4449
if (!accessToken || !scope) return null;
4550

@@ -53,10 +58,17 @@ export async function setStore(session: SessionProps) {
5358
// User management for multi-user apps
5459
// Use setStoreUser for storing store specific variables
5560
export async function setStoreUser(session: SessionProps) {
56-
const { access_token: accessToken, context, owner, user: { id: userId } } = session;
61+
const {
62+
access_token: accessToken,
63+
context,
64+
owner,
65+
sub,
66+
user: { id: userId },
67+
} = session;
5768
if (!userId) return null;
5869

59-
const storeHash = context?.split('/')[1] || '';
70+
const contextString = context ?? sub;
71+
const storeHash = contextString?.split('/')[1] || '';
6072
const collection = db.collection('storeUsers');
6173
const documentId = `${userId}_${storeHash}`; // users can belong to multiple stores
6274
const ref = collection.doc(documentId);
@@ -79,8 +91,9 @@ export async function setStoreUser(session: SessionProps) {
7991
}
8092
}
8193

82-
export async function deleteUser({ context, user }: SessionProps) {
83-
const storeHash = context?.split('/')[1] || '';
94+
export async function deleteUser({ context, user, sub }: SessionProps) {
95+
const contextString = context ?? sub;
96+
const storeHash = contextString?.split('/')[1] || '';
8497
const docId = `${user?.id}_${storeHash}`;
8598
const storeUsersRef = db.collection('storeUsers').doc(docId);
8699

lib/dbs/mysql.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ export async function setStore(session: SessionProps) {
3030

3131
// Use setStoreUser for storing store specific variables
3232
export async function setStoreUser(session: SessionProps) {
33-
const { access_token: accessToken, context, owner, user: { id: userId } } = session;
33+
const { access_token: accessToken, context, owner, sub, user: { id: userId } } = session;
3434
if (!userId) return null;
3535

36-
const storeHash = context?.split('/')[1] || '';
36+
const contextString = context ?? sub;
37+
const storeHash = contextString?.split('/')[1] || '';
3738
const sql = 'SELECT * FROM storeUsers WHERE userId = ? AND storeHash = ?';
3839
const values = [String(userId), storeHash];
3940
const storeUser = await query(sql, values);
@@ -55,8 +56,9 @@ export async function setStoreUser(session: SessionProps) {
5556
}
5657
}
5758

58-
export async function deleteUser({ context, user }: SessionProps) {
59-
const storeHash = context?.split('/')[1] || '';
59+
export async function deleteUser({ context, user, sub }: SessionProps) {
60+
const contextString = context ?? sub;
61+
const storeHash = contextString?.split('/')[1] || '';
6062
const values = [String(user?.id), storeHash];
6163
await query('DELETE FROM storeUsers WHERE userId = ? AND storeHash = ?', values);
6264
}

package-lock.json

Lines changed: 20 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"jsonwebtoken": "^8.5.1",
2121
"mysql": "^2.18.1",
2222
"next": "^10.2.3",
23-
"node-bigcommerce": "^4.1.0",
23+
"node-bigcommerce": "bigcommerce/node-bigcommerce",
2424
"react": "^17.0.1",
2525
"react-dom": "^17.0.1",
2626
"styled-components": "^4.4.1",

types/auth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface SessionProps {
1010
owner?: User;
1111
scope?: string;
1212
store_hash?: string;
13+
sub?: string;
1314
timestamp?: number;
1415
user: User;
1516
}

0 commit comments

Comments
 (0)