From ca165f32c40b8ec0e4da8d7831040398d21d7c9f Mon Sep 17 00:00:00 2001 From: Marc Mogdanz Date: Wed, 25 Sep 2024 02:33:59 +0200 Subject: [PATCH 1/3] fix: enable `returnUserData` when storing/loading sessions --- .../src/__tests__/redis.test.ts | 104 ++++++++++++++++++ .../src/redis.ts | 6 +- 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/packages/apps/session-storage/shopify-app-session-storage-redis/src/__tests__/redis.test.ts b/packages/apps/session-storage/shopify-app-session-storage-redis/src/__tests__/redis.test.ts index 8fd0dff49..5e0cf9def 100644 --- a/packages/apps/session-storage/shopify-app-session-storage-redis/src/__tests__/redis.test.ts +++ b/packages/apps/session-storage/shopify-app-session-storage-redis/src/__tests__/redis.test.ts @@ -216,6 +216,59 @@ describe('RedisSessionStorage', () => { expect(await storage.storeSession(session)).toBeTruthy(); await storage.disconnect(); }); + + it(`can retrieve online access info when using online access tokens`, async () => { + const storage = new RedisSessionStorage(dbURL); + await storage.ready; + const session = new Session({ + id: '456', + shop: 'test-shop.myshopify.com', + state: 'test-state', + isOnline: true, + accessToken: 'fake_access_token', + expires: new Date(), + scope: 'fake_scope', + onlineAccessInfo: { + expires_in: 1, + associated_user_scope: 'fake_scope', + associated_user: { + id: 1, + first_name: 'fake-first-name', + last_name: 'fake-last-name', + email: 'fake-email', + locale: 'fake-locale', + email_verified: true, + account_owner: true, + collaborator: false, + }, + }, + }); + + await storage.storeSession(session); + expect( + (await storage.loadSession('456'))?.onlineAccessInfo?.associated_user, + ).toEqual(session.onlineAccessInfo?.associated_user); + await storage.disconnect(); + }); + + it(`does not retrieve online access info when using offline access tokens`, async () => { + const storage = new RedisSessionStorage(dbURL); + await storage.ready; + const session = new Session({ + id: '456', + shop: 'shop1.myshopify.com', + state: 'state', + isOnline: false, + scope: ['test_scope'].toString(), + accessToken: 'abcde-12345-123', + }); + + await storage.storeSession(session); + expect( + (await storage.loadSession('456'))?.onlineAccessInfo, + ).toBeUndefined(); + await storage.disconnect(); + }); }); describe('using a redis client', () => { @@ -296,6 +349,57 @@ describe('RedisSessionStorage', () => { expect(await storage.storeSession(session)).toBeTruthy(); expect(await client.keys(expectedKey)).toHaveLength(1); }); + + it(`can retrieve online access info when using online access tokens`, async () => { + const storage = new RedisSessionStorage(client); + await storage.ready; + const session = new Session({ + id: '456', + shop: 'test-shop.myshopify.com', + state: 'test-state', + isOnline: true, + accessToken: 'fake_access_token', + expires: new Date(), + scope: 'fake_scope', + onlineAccessInfo: { + expires_in: 1, + associated_user_scope: 'fake_scope', + associated_user: { + id: 1, + first_name: 'fake-first-name', + last_name: 'fake-last-name', + email: 'fake-email', + locale: 'fake-locale', + email_verified: true, + account_owner: true, + collaborator: false, + }, + }, + }); + + await storage.storeSession(session); + expect( + (await storage.loadSession('456'))?.onlineAccessInfo?.associated_user, + ).toEqual(session.onlineAccessInfo?.associated_user); + }); + + it(`does not retrieve online access info when using offline access tokens`, async () => { + const storage = new RedisSessionStorage(client); + await storage.ready; + const session = new Session({ + id: '456', + shop: 'shop1.myshopify.com', + state: 'state', + isOnline: false, + scope: ['test_scope'].toString(), + accessToken: 'abcde-12345-123', + }); + + await storage.storeSession(session); + expect( + (await storage.loadSession('456'))?.onlineAccessInfo, + ).toBeUndefined(); + }); }); }); }); diff --git a/packages/apps/session-storage/shopify-app-session-storage-redis/src/redis.ts b/packages/apps/session-storage/shopify-app-session-storage-redis/src/redis.ts index 5216654f2..61a9f1470 100644 --- a/packages/apps/session-storage/shopify-app-session-storage-redis/src/redis.ts +++ b/packages/apps/session-storage/shopify-app-session-storage-redis/src/redis.ts @@ -92,7 +92,7 @@ export class RedisSessionStorage implements SessionStorage { await this.client.set( session.id, - JSON.stringify(session.toPropertyArray()), + JSON.stringify(session.toPropertyArray(true)), ); await this.addKeyToShopList(session); return true; @@ -106,7 +106,7 @@ export class RedisSessionStorage implements SessionStorage { if (!rawResult) return undefined; rawResult = JSON.parse(rawResult); - return Session.fromPropertyArray(rawResult); + return Session.fromPropertyArray(rawResult, true); } public async deleteSession(id: string): Promise { @@ -137,7 +137,7 @@ export class RedisSessionStorage implements SessionStorage { const rawResult = await this.client.get(idKey, false); if (!rawResult) continue; - const session = Session.fromPropertyArray(JSON.parse(rawResult)); + const session = Session.fromPropertyArray(JSON.parse(rawResult), true); results.push(session); } From 9cb261e3ee91bf113b846209cb2cf29d59e2b127 Mon Sep 17 00:00:00 2001 From: Marc Mogdanz Date: Wed, 25 Sep 2024 02:46:56 +0200 Subject: [PATCH 2/3] chore: added changeset --- .changeset/smart-phones-eat.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smart-phones-eat.md diff --git a/.changeset/smart-phones-eat.md b/.changeset/smart-phones-eat.md new file mode 100644 index 000000000..2f7bdd61d --- /dev/null +++ b/.changeset/smart-phones-eat.md @@ -0,0 +1,5 @@ +--- +'@shopify/shopify-app-session-storage-redis': patch +--- + +Fixed saving/loading online access info and associated user data for redis From efbb145729c4709b4e59f08007a4011bd2c30243 Mon Sep 17 00:00:00 2001 From: Marc Mogdanz Date: Wed, 25 Sep 2024 21:13:13 +0200 Subject: [PATCH 3/3] chore: switch to minor instead of patch --- .changeset/smart-phones-eat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/smart-phones-eat.md b/.changeset/smart-phones-eat.md index 2f7bdd61d..e03249193 100644 --- a/.changeset/smart-phones-eat.md +++ b/.changeset/smart-phones-eat.md @@ -1,5 +1,5 @@ --- -'@shopify/shopify-app-session-storage-redis': patch +'@shopify/shopify-app-session-storage-redis': minor --- Fixed saving/loading online access info and associated user data for redis