Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1298 from Shopify/liz/from-property-array-flag
Browse files Browse the repository at this point in the history
Return user session data behind flag
  • Loading branch information
lizkenyon authored Mar 26, 2024
2 parents bd2afe4 + 3ba534a commit eae4573
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .changeset/strong-falcons-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
4 changes: 3 additions & 1 deletion packages/shopify-api/docs/guides/session-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ Once the `Session` is found, the app must ensure that it converts it from the st

If the `.toPropertyArray` method was used to obtain the session data, the `Session` class has a `.fromPropertyArray` static method that can be used to convert the array data back into a session.

`FromPropertyArray` has an optional parameter `returnUserData`, defaulted to false, when set to true it will return the associated user data if it is included in the property array.

```ts
const sessionProperties = session.toPropertyArray(true);
/*
Expand All @@ -223,7 +225,7 @@ const sessionProperties = session.toPropertyArray(true);
],
*/

const session = Session.fromPropertyArray(sessionProperties);
const session = Session.fromPropertyArray(sessionProperties, true);
/*
... then session will have the following data...
{
Expand Down
1 change: 1 addition & 0 deletions packages/shopify-api/lib/session/__tests__/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ describe('toPropertyArray and fromPropertyArray', () => {
const session = new Session(test.session);
const sessionCopy = Session.fromPropertyArray(
session.toPropertyArray(test.returnUserData),
test.returnUserData,
);
expect(session.id).toStrictEqual(sessionCopy.id);
expect(session.shop).toStrictEqual(sessionCopy.shop);
Expand Down
92 changes: 56 additions & 36 deletions packages/shopify-api/lib/session/session.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-fallthrough */
import {InvalidSession} from '../error';
import {OnlineAccessInfo, OnlineAccessUser} from '../auth/oauth/types';
import {AuthScopes} from '../auth/scopes';
Expand All @@ -24,6 +25,7 @@ interface AssociatedUserObject {
export class Session {
public static fromPropertyArray(
entries: [string, string | number | boolean][],
returnUserData = false,
): Session {
if (!Array.isArray(entries)) {
throw new InvalidSession(
Expand Down Expand Up @@ -85,55 +87,73 @@ export class Session {
},
];
case 'userId':
return [
key,
(associatedUserObj.associated_user.id = Number(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.id = Number(value)),
];
}
case 'firstName':
return [
key,
(associatedUserObj.associated_user.first_name = String(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.first_name =
String(value)),
];
}
case 'lastName':
return [
key,
(associatedUserObj.associated_user.last_name = String(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.last_name = String(value)),
];
}
case 'email':
return [
key,
(associatedUserObj.associated_user.email = String(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.email = String(value)),
];
}
case 'accountOwner':
return [
key,
(associatedUserObj.associated_user.account_owner =
Boolean(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.account_owner =
Boolean(value)),
];
}
case 'locale':
return [
key,
(associatedUserObj.associated_user.locale = String(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.locale = String(value)),
];
}
case 'collaborator':
return [
key,
(associatedUserObj.associated_user.collaborator =
Boolean(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.collaborator =
Boolean(value)),
];
}
case 'emailVerified':
return [
key,
(associatedUserObj.associated_user.email_verified =
Boolean(value)),
];
if (returnUserData) {
return [
key,
(associatedUserObj.associated_user.email_verified =
Boolean(value)),
];
}
// If returnUserData is false, return any user keys as passed in
default:
return [key, value];
}
}),
) as any;
// If the onlineAccessInfo is not present, we are using the new session info and add it to the object
if (!obj.onlineAccessInfo && Object.keys(associatedUserObj).length !== 0) {
if (returnUserData) {
obj.onlineAccessInfo = associatedUserObj;
}
Object.setPrototypeOf(obj, Session.prototype);
Expand Down

0 comments on commit eae4573

Please sign in to comment.