-
Notifications
You must be signed in to change notification settings - Fork 125
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 #676 from Shopify/add_more_authentication_examples
Add more thorough authentication examples
- Loading branch information
Showing
15 changed files
with
336 additions
and
2 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,2 @@ | ||
--- | ||
--- |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
module.exports = { | ||
ignorePatterns: ['docs/', '**/*.example.tsx?'], | ||
ignorePatterns: [ | ||
'docs/', | ||
'*.example.ts', | ||
'*.example.tsx', | ||
'*.example.*.ts', | ||
'*.example.*.tsx', | ||
], | ||
}; |
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
34 changes: 34 additions & 0 deletions
34
packages/shopify-app-remix/src/server/authenticate/admin/authenticate.admin.doc.example.ts
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,34 @@ | ||
import {type ActionFunctionArgs, json} from '@remix-run/node'; | ||
import {GraphqlQueryError} from '@shopify/shopify-api'; | ||
|
||
import {authenticate} from '../shopify.server'; | ||
|
||
export const action = async ({request}: ActionFunctionArgs) => { | ||
const {admin, redirect} = await authenticate.admin(request); | ||
|
||
try { | ||
await admin.graphql( | ||
`#graphql | ||
mutation updateProductTitle($input: ProductInput!) { | ||
productUpdate(input: $input) { | ||
product { | ||
id | ||
} | ||
} | ||
}`, | ||
{ | ||
variables: { | ||
input: {id: '123', title: 'New title'}, | ||
}, | ||
}, | ||
); | ||
|
||
return redirect('/app/product-updated'); | ||
} catch (error) { | ||
if (error instanceof GraphqlQueryError) { | ||
return json({errors: error.body?.errors}, {status: 500}); | ||
} | ||
|
||
return new Response('Failed to update product title', {status: 500}); | ||
} | ||
}; |
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
39 changes: 39 additions & 0 deletions
39
packages/shopify-app-remix/src/server/authenticate/flow/authenticate.flow.doc.example.ts
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,39 @@ | ||
import {type ActionFunctionArgs} from '@remix-run/node'; | ||
|
||
import {authenticate} from '../shopify.server'; | ||
|
||
export const action = async ({request}: ActionFunctionArgs) => { | ||
const {admin, payload} = await authenticate.flow(request); | ||
|
||
const customerId = payload.properties.customer_id; | ||
|
||
const response = await admin.graphql( | ||
`#graphql | ||
mutation setMetafield($customerId: ID!, $time: String!) { | ||
metafieldsSet(metafields: { | ||
ownerId: $customerId | ||
namespace: "my-app", | ||
key: "last_flow_update", | ||
value: $time, | ||
type: "string", | ||
}) { | ||
metafields { | ||
key | ||
value | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
variables: { | ||
customerId, | ||
time: new Date().toISOString(), | ||
}, | ||
}, | ||
); | ||
const body = await response.json(); | ||
|
||
console.log('Updated value', body.data!.metafieldsSet!.metafields![0].value); | ||
|
||
return new Response(); | ||
}; |
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
27 changes: 27 additions & 0 deletions
27
...emix/src/server/authenticate/public/appProxy/authenticate.public.app-proxy.doc.example.ts
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,27 @@ | ||
import type {LoaderFunctionArgs} from '@remix-run/node'; | ||
|
||
import {authenticate} from '../shopify.server'; | ||
|
||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
const {storefront, liquid} = await authenticate.public.appProxy(request); | ||
|
||
if (!storefront) { | ||
return new Response(); | ||
} | ||
|
||
const response = await storefront.graphql( | ||
`#graphql | ||
query productTitle { | ||
products(first: 1) { | ||
nodes { | ||
title | ||
} | ||
} | ||
}`, | ||
); | ||
const body = await response.json(); | ||
|
||
const title = body.data.products.nodes[0].title; | ||
|
||
return liquid(`Found product ${title} from {{shop.name}}`); | ||
}; |
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
21 changes: 21 additions & 0 deletions
21
...rc/server/authenticate/public/checkout/authenticate.public.checkout.doc.example.offers.ts
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,21 @@ | ||
// Most apps would load this from their database | ||
export function getOffers(shop: string) { | ||
const offers: Record<any, any[]> = { | ||
'shop.com': [ | ||
{ | ||
id: '1', | ||
title: '10% off', | ||
price: 10, | ||
type: 'percentage', | ||
}, | ||
{ | ||
id: '2', | ||
title: 'Free shipping', | ||
price: 0, | ||
type: 'shipping', | ||
}, | ||
], | ||
}; | ||
|
||
return offers[shop]; | ||
} |
17 changes: 17 additions & 0 deletions
17
...remix/src/server/authenticate/public/checkout/authenticate.public.checkout.doc.example.ts
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,17 @@ | ||
import type {ActionFunctionArgs, LoaderFunctionArgs} from '@remix-run/node'; | ||
import {json} from '@remix-run/node'; | ||
|
||
import {authenticate} from '../shopify.server'; | ||
import {getOffers} from '../offers.server'; | ||
|
||
// The loader responds to preflight requests from Shopify | ||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
await authenticate.public.checkout(request); | ||
}; | ||
|
||
export const action = async ({request}: ActionFunctionArgs) => { | ||
const {cors, sessionToken} = await authenticate.public.checkout(request); | ||
|
||
const offers = getOffers(sessionToken.dest); | ||
return cors(json({offers})); | ||
}; |
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
39 changes: 39 additions & 0 deletions
39
...s/shopify-app-remix/src/server/authenticate/webhooks/authenticate.webhooks.doc.example.ts
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,39 @@ | ||
import {type ActionFunctionArgs} from '@remix-run/node'; | ||
|
||
import {authenticate} from '../shopify.server'; | ||
|
||
export const action = async ({request}: ActionFunctionArgs) => { | ||
const {topic, admin, payload} = await authenticate.webhook(request); | ||
|
||
switch (topic) { | ||
case 'PRODUCTS_UPDATE': | ||
await admin.graphql( | ||
`#graphql | ||
mutation setMetafield($productId: ID!, $time: String!) { | ||
metafieldsSet(metafields: { | ||
ownerId: $productId | ||
namespace: "my-app", | ||
key: "webhook_received_at", | ||
value: $time, | ||
type: "string", | ||
}) { | ||
metafields { | ||
key | ||
value | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
variables: { | ||
productId: payload.admin_graphql_api_id, | ||
time: new Date().toISOString(), | ||
}, | ||
}, | ||
); | ||
|
||
return new Response(); | ||
} | ||
|
||
throw new Response(); | ||
}; |
Oops, something went wrong.