Skip to content

Performing administration tasks on the server side with a secret key

默认情况下,服务端渲染(SSR)不允许使用 secret 密钥。这个限制是为了防止你的 secret 密钥意外暴露给公众。因为 SSR 在服务端和客户端都运行,所以单独为客户端使用分离密钥变得比较困难。

🌐 By default, server side rendering (SSR) does not permit the use of a secret key. This restriction is in place to prevent the accidental exposure of your secret key to the public. Since SSR runs on both the server and client side, it becomes challenging to separate the key specifically for client-side usage.

不过,有一个解决方法。你可以使用 @supabase/supabase-jscreateClient 方法创建一个单独的 Supabase 客户端,并提供 secret 密钥。在服务器环境中,你还需要禁用某些属性以确保功能正常。下面的示例代码展示了所需的设置。

🌐 However, there is a solution. You can create a separate Supabase client using the createClient method from @supabase/supabase-js and provide it with the secret key. In a server environment, you also need to disable certain properties to ensure proper functionality. See the example code below for the required settings.

通过采用这种方法,你可以安全地使用 secret 密钥,而不会影响安全性或将敏感信息暴露给公众。

🌐 By implementing this approach, you can safely use the secret key without compromising security or exposing sensitive information to the public.

1
import { createClient } from '@supabase/supabase-js'
2
3
const supabase = createClient(supabaseUrl, secretKey, {
4
auth: {
5
persistSession: false,
6
autoRefreshToken: false,
7
detectSessionInUrl: false,
8
},
9
})