首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >支持业务逻辑的中间件

支持业务逻辑的中间件
EN

Stack Overflow用户
提问于 2021-11-15 18:52:03
回答 1查看 425关注 0票数 2

对支撑宇宙来说是新的。简单问题

是否有一种方法可以在supabase中设置中间件?苏巴布能完成这个任务吗?

在创建实体时,purchase)

  • Restrict

  • 添加业务逻辑中间件添加特殊的验证(即:根据用户角色在信息之前验证产品是否有库存(即:管理员可以读取其他实体属性,但不能读取普通用户)。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-19 03:05:16

现在可以使用Supabase的边缘函数来解决这个问题:https://supabase.com/docs/guides/functions

这里有一个使用Postgres的行级安全性解决“取决于用户角色的限制信息”的示例:

https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts

代码语言:javascript
运行
复制
/ Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.

import { serve } from 'https://deno.land/std@0.131.0/http/server.ts'
import { supabaseClient } from '../_shared/supabaseClient.ts'
import { corsHeaders } from '../_shared/cors.ts'

console.log(`Function "select-from-table-with-auth-rls" up and running!`)

serve(async (req: Request) => {
  // This is needed if you're planning to invoke your function from a browser.
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders })
  }

  try {
    // Set the Auth context of the user that called the function.
    // This way your row-level-security (RLS) policies are applied.
    supabaseClient.auth.setAuth(req.headers.get('Authorization')!.replace('Bearer ', ''))

    const { data, error } = await supabaseClient.from('users').select('*')
    console.log({ data, error })

    return new Response(JSON.stringify({ data, error }), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      status: 200,
    })
  } catch (error) {
    return new Response(JSON.stringify({ error: error.message }), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
      status: 400,
    })
  }
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69979444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档