diff --git a/.changeset/smart-phones-eat.md b/.changeset/smart-phones-eat.md new file mode 100644 index 000000000..e03249193 --- /dev/null +++ b/.changeset/smart-phones-eat.md @@ -0,0 +1,5 @@ +--- +'@shopify/shopify-app-session-storage-redis': minor +--- + +Fixed saving/loading online access info and associated user data for redis 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); }