Skip to content

Commit

Permalink
Merge pull request #1563 from MarcMogdanz/fix/allow-online-access-info
Browse files Browse the repository at this point in the history
fix: enable `returnUserData` when storing/loading sessions
  • Loading branch information
matteodepalo authored Sep 26, 2024
2 parents 4895f0d + efbb145 commit 460ccd3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-phones-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/shopify-app-session-storage-redis': minor
---

Fixed saving/loading online access info and associated user data for redis
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<boolean> {
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 460ccd3

Please sign in to comment.