-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #715 from Shopify/liz/store-user-info
Update Prisma Adapter to Store User Info for Online Tokens
- Loading branch information
Showing
8 changed files
with
331 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
"@shopify/shopify-app-session-storage-prisma": major | ||
"@shopify/shopify-app-session-storage-test-utils": patch | ||
--- | ||
|
||
## Store user information as part of the session | ||
|
||
With this change when using online access tokens, the user information is stored as part of the session. Previously only the user ID was stored. This will enable changing of page content or limiting of page visibility by user, as well as unlock logging users actions. This is a breaking change, as the Prisma schema has been updated to include the user information. | ||
|
||
For more information review the [migration guide](../packages/apps/session-storage/shopify-app-session-storage-prisma/MIGRATION_V5.md). | ||
|
||
|
||
<details> | ||
The new session will include the following data: | ||
|
||
```ts | ||
{ | ||
id: 'online_session_id', | ||
shop: 'online-session-shop', | ||
state: 'online-session-state', | ||
isOnline: true, | ||
scope: 'online-session-scope', | ||
accessToken: 'online-session-token', | ||
expires: 2022-01-01T05:00:00.000Z, | ||
onlineAccessInfo: { | ||
associated_user: { | ||
id: 1, | ||
first_name: 'online-session-first-name' | ||
last_name: 'online-session-last-name', | ||
email: 'online-session-email', | ||
locale: 'online-session-locale', | ||
email_verified: false, | ||
account_owner: true, | ||
collaborator: false, | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
You will be able to access the user information on the Session object: | ||
|
||
```ts | ||
const { admin, session } = await authenticate.admin(request); | ||
|
||
console.log("user id", session.onlineAccessInfo.associated_user.id); | ||
console.log("user email", session.onlineAccessInfo.associated_user.email); | ||
console.log("account owner", session.onlineAccessInfo.associated_user.account_owner); | ||
``` | ||
|
||
</details> | ||
|
||
|
57 changes: 57 additions & 0 deletions
57
packages/apps/session-storage/shopify-app-session-storage-prisma/MIGRATION_V5.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Migrating to `v.5.0.0` | ||
|
||
Version `5.0.0` of the `@shopify/shopify-app-session-storage-prisma` introduces a breaking change to the session storage schema. The user information is now stored as part of the session. Previously only the user ID was stored. This change requires updating the Prisma schema to include the user information. | ||
|
||
## Updating the Prisma schema | ||
|
||
Update the `Session` model in the Prisma schema to include the user information: | ||
|
||
```prisma | ||
model Session { | ||
id String @id | ||
shop String | ||
state String | ||
isOnline Boolean @default(false) | ||
scope String? | ||
expires DateTime? | ||
accessToken String | ||
userId BigInt? | ||
// New fields | ||
firstName String? | ||
lastName String? | ||
email String? | ||
accountOwner Boolean @default(false) | ||
locale String? | ||
collaborator Boolean? @default(false) | ||
emailVerified Boolean? @default(false) | ||
} | ||
``` | ||
|
||
Run the following prisma commands to update the database schema: | ||
|
||
```sh | ||
npx prisma migrate dev | ||
``` | ||
|
||
### Update Types | ||
|
||
Update the generated types to include the new fields: | ||
|
||
```ts | ||
npx prisma generate | ||
``` | ||
|
||
## Using the user information | ||
|
||
The user information will now be available on the `Session` object: | ||
|
||
```ts | ||
const {admin, session} = await authenticate.admin(request); | ||
|
||
console.log('user id', session.onlineAccessInfo.associated_user.id); | ||
console.log('user email', session.onlineAccessInfo.associated_user.email); | ||
console.log( | ||
'account owner', | ||
session.onlineAccessInfo.associated_user.account_owner, | ||
); | ||
``` |
24 changes: 24 additions & 0 deletions
24
...y-app-session-storage-prisma/prisma/migrations/20240318195044_add_user_info/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Session" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"shop" TEXT NOT NULL, | ||
"state" TEXT NOT NULL, | ||
"isOnline" BOOLEAN NOT NULL DEFAULT false, | ||
"scope" TEXT, | ||
"expires" DATETIME, | ||
"accessToken" TEXT NOT NULL, | ||
"userId" BIGINT, | ||
"firstName" TEXT, | ||
"lastName" TEXT, | ||
"email" TEXT, | ||
"accountOwner" BOOLEAN, | ||
"locale" TEXT, | ||
"collaborator" BOOLEAN, | ||
"emailVerified" BOOLEAN | ||
); | ||
INSERT INTO "new_Session" ("accessToken", "expires", "id", "isOnline", "scope", "shop", "state", "userId") SELECT "accessToken", "expires", "id", "isOnline", "scope", "shop", "state", "userId" FROM "Session"; | ||
DROP TABLE "Session"; | ||
ALTER TABLE "new_Session" RENAME TO "Session"; | ||
PRAGMA foreign_key_check; | ||
PRAGMA foreign_keys=ON; |
24 changes: 24 additions & 0 deletions
24
...ion-storage-prisma/prisma/migrations/20240319213906_add_user_info_for_tests/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_MySession" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"shop" TEXT NOT NULL, | ||
"state" TEXT NOT NULL, | ||
"isOnline" BOOLEAN NOT NULL DEFAULT false, | ||
"scope" TEXT, | ||
"expires" DATETIME, | ||
"accessToken" TEXT NOT NULL, | ||
"userId" BIGINT, | ||
"firstName" TEXT, | ||
"lastName" TEXT, | ||
"email" TEXT, | ||
"accountOwner" BOOLEAN, | ||
"locale" TEXT, | ||
"collaborator" BOOLEAN, | ||
"emailVerified" BOOLEAN | ||
); | ||
INSERT INTO "new_MySession" ("accessToken", "expires", "id", "isOnline", "scope", "shop", "state", "userId") SELECT "accessToken", "expires", "id", "isOnline", "scope", "shop", "state", "userId" FROM "MySession"; | ||
DROP TABLE "MySession"; | ||
ALTER TABLE "new_MySession" RENAME TO "MySession"; | ||
PRAGMA foreign_key_check; | ||
PRAGMA foreign_keys=ON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.