diff --git a/.changeset/strong-falcons-develop.md b/.changeset/strong-falcons-develop.md new file mode 100644 index 000000000..a845151cc --- /dev/null +++ b/.changeset/strong-falcons-develop.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/shopify-api/docs/guides/session-storage.md b/packages/shopify-api/docs/guides/session-storage.md index 2ec295b17..fe4ab7299 100644 --- a/packages/shopify-api/docs/guides/session-storage.md +++ b/packages/shopify-api/docs/guides/session-storage.md @@ -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); /* @@ -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... { diff --git a/packages/shopify-api/lib/session/__tests__/session.test.ts b/packages/shopify-api/lib/session/__tests__/session.test.ts index 59c224f75..880898451 100644 --- a/packages/shopify-api/lib/session/__tests__/session.test.ts +++ b/packages/shopify-api/lib/session/__tests__/session.test.ts @@ -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); diff --git a/packages/shopify-api/lib/session/session.ts b/packages/shopify-api/lib/session/session.ts index 5ceaa8d92..b377c165a 100644 --- a/packages/shopify-api/lib/session/session.ts +++ b/packages/shopify-api/lib/session/session.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-fallthrough */ import {InvalidSession} from '../error'; import {OnlineAccessInfo, OnlineAccessUser} from '../auth/oauth/types'; import {AuthScopes} from '../auth/scopes'; @@ -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( @@ -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);