用于业务逻辑的 Supabase 中间件
Supabase middleware for business logic
supabase 世界的新手。简单问题
有没有办法在 supabase 中设置中间件?。 Supabase 能做到吗?
- 创建实体时添加业务逻辑中间件
- 添加特殊验证(即:在购买前验证产品是否有库存)
- 根据用户角色限制信息(即:管理员可以读取其他实体属性,但普通用户不能)。
谢谢
现在可以使用 Supabase 的边缘函数解决这个问题:https://supabase.com/docs/guides/functions
这里有一个使用 Postgres 的行级别安全性解决“根据用户角色限制信息”的示例:
/ 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,
})
}
})
supabase 世界的新手。简单问题
有没有办法在 supabase 中设置中间件?。 Supabase 能做到吗?
- 创建实体时添加业务逻辑中间件
- 添加特殊验证(即:在购买前验证产品是否有库存)
- 根据用户角色限制信息(即:管理员可以读取其他实体属性,但普通用户不能)。
谢谢
现在可以使用 Supabase 的边缘函数解决这个问题:https://supabase.com/docs/guides/functions
这里有一个使用 Postgres 的行级别安全性解决“根据用户角色限制信息”的示例:
/ 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,
})
}
})