英文:
Supabase and Cloudflare Workers
问题
我可以帮您翻译以下部分:
"How do i protect a Cloudflare Worker route to authorize only if the user is authenticated on Supabase?"
我如何保护 Cloudflare Worker 路由,仅在用户在 Supabase 上经过身份验证时才授权?
"I'm using Cloudflare Pages Function to create a worker inside the Cloudflare Pages: https://developers.cloudflare.com/pages/platform/functions/"
我正在使用 Cloudflare Pages 函数在 Cloudflare Pages 内创建一个 worker:https://developers.cloudflare.com/pages/platform/functions/
import { createClient } from '@supabase/supabase-js'
export async function onRequest(context) {
const token = context.request.headers.get('Cookie')
const supabase = createClient(
'url',
'key')
const {user, error} = await supabase.auth.getUser(token)
if(user){
return new Response("YES!")
} else {
return new Response("Access Denied")
}
}
import { createClient } from '@supabase/supabase-js'
export async function onRequest(context) {
const token = context.request.headers.get('Cookie')
const supabase = createClient(
'url',
'key')
const {user, error} = await supabase.auth.getUser(token)
if(user){
return new Response("YES!")
} else {
return new Response("Access Denied")
}
}
请注意,上述代码是一个示例,用于在 Cloudflare Worker 中验证用户是否在 Supabase 上经过身份验证。
英文:
How do i protect a Cloudflare Worker route to authorize only if the user is authenticated on Supabase?
I'm using Cloudflare Pages Function to create a worker inside the Cloudflare Pages: https://developers.cloudflare.com/pages/platform/functions/
import { createClient } from '@supabase/supabase-js'
export async function onRequest(context) {
const token = context.request.headers.get('Cookie')
const supabase = createClient(
'url',
'key')
const {user, error} = await supabase.auth.getUser(token)
if(user){
return new Response("YES!")
} else {
return new Response("Access Denied")
}
}
答案1
得分: 3
你可以按照这篇文章中描述的相同方法进行操作,只需稍作修改,因为它不是Deno Edge函数。
const token = context.request.headers.get('Cookie');
const options = { global: { headers: { Authorization: 'Bearer ' + token } } };
const supabaseClient = createClient('url', 'key', options);
英文:
You can follow the same approach described in this post with some tweaks as it is not a Deno Edge Function.
const token = context.request.headers.get('Cookie')
const options = { global: { headers: { Authorization: 'Bearer '+token } } };
const supabaseClient = createClient('url', 'key', options);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论