admin 管理员组文章数量: 1086019
I have a Liquid file in Shopify with a form on it. When the form is submitted, I make a request to a route in my Remix app:
fetch('', {
method: 'post',
body: JSON.stringify({...}),
headers: {
'Content-Type': 'multipart/form-data',
}
});
If I don't use authenticate
in my route, this works fine.
export const action = async ({ request }: ActionFunctionArgs) => {
// set headers
const headers = new Headers();
headers.set('Access-Control-Allow-Origin', '*');
// preflight
if (request.method === 'OPTIONS') {
return new Response(null, {
headers,
});
}
if (request.method === 'POST') {
const body = await request.json();
return new Response(
JSON.stringify({
status: 200,
data: body,
}),
{
headers,
}
);
}
});
If I include authenticate
, the route is redirected to /auth/login
.
// server
import { authenticate } from 'app/shopify.server';
export const action = async ({ request }: ActionFunctionArgs) => {
// this will cause the route to redirect to /auth/login
const { admin } = await authenticate.admin(request);
}
I understand that the fetch()
request in Liquid is not sending any Authorization
header, so the request is not authenticated. How can I add authentication to the fetch()
request from the Liquid theme?
I have a Liquid file in Shopify with a form on it. When the form is submitted, I make a request to a route in my Remix app:
fetch('https://my-proxy-url.trycloudflare/api/entrants', {
method: 'post',
body: JSON.stringify({...}),
headers: {
'Content-Type': 'multipart/form-data',
}
});
If I don't use authenticate
in my route, this works fine.
export const action = async ({ request }: ActionFunctionArgs) => {
// set headers
const headers = new Headers();
headers.set('Access-Control-Allow-Origin', '*');
// preflight
if (request.method === 'OPTIONS') {
return new Response(null, {
headers,
});
}
if (request.method === 'POST') {
const body = await request.json();
return new Response(
JSON.stringify({
status: 200,
data: body,
}),
{
headers,
}
);
}
});
If I include authenticate
, the route is redirected to /auth/login
.
// server
import { authenticate } from 'app/shopify.server';
export const action = async ({ request }: ActionFunctionArgs) => {
// this will cause the route to redirect to /auth/login
const { admin } = await authenticate.admin(request);
}
I understand that the fetch()
request in Liquid is not sending any Authorization
header, so the request is not authenticated. How can I add authentication to the fetch()
request from the Liquid theme?
1 Answer
Reset to default 0If you want the most secure way to authenticate your request, using an App Proxy is the best option. It provides high security and is ideal for Shopify apps, though it requires some setup.
If you're working with an embedded Shopify app, a JWT Token is a decent option, offering medium security and complexity.
However, if you're looking for a quick and easy solution, passing a token through the URL is the simplest method, but it's not very secure and should only be used temporarily or in low-risk situations.
Steps to Set Up Shopify App Proxy:
Go to Shopify Admin > Apps > Manage Private Apps (or create a new app).
Enable an App Proxy:
Prefix:
/apps/my-proxy
Proxy URL:
https://my-proxy-url.trycloudflare/api/entrants
Use the Proxy in Liquid:
<script>
fetch('/apps/my-proxy', {
method: 'POST',
body: JSON.stringify({...}),
headers: {
'Content-Type': 'application/json',
}
}).then(response => response.json())
.then(data => console.log(data));
</script>
Modify the Remix Action to Validate the Request: Shopify will send an X-Shopify-Shop-Domain
header in the request. Your Remix app should validate this to allow only legitimate Shopify requests.
import { verifyShopifyRequest } from 'app/shopify.server';
export const action = async ({ request }: ActionFunctionArgs) => {
const headers = new Headers();
headers.set('Access-Control-Allow-Origin', '*');
// Validate request
const shopDomain = request.headers.get('X-Shopify-Shop-Domain');
if (!verifyShopifyRequest(shopDomain)) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 });
}
// Process POST request
if (request.method === 'POST') {
const body = await request.json();
return new Response(JSON.stringify({ status: 200, data: body }), { headers });
}
};
本文标签:
版权声明:本文标题:authentication - How can I make a request from a Shopify Liquid theme to an authenticated route in a Remix app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744017706a2519226.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论