Supabase 和 Cloudflare Workers

huangapple go评论60阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年5月30日 05:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定