无法在启用 RLS 和策略的情况下插入到 Supabase 表

问题描述 投票:0回答:1

我有一个名为

petition
且启用了 RLS 的表,并在其上执行以下两个策略:

INSERT

Enable insert for authenticated users only

Applied to:authenticated role

SELECT

Enable read access for all users

Applied to:public role

我能够在我的应用程序上进行身份验证登录,并且在禁用 RLS 时能够很好地写入表。

这是返回的精确错误消息:

Error creating petition: new row violates row-level security policy for table "petition"

另外,这是我的代码:

    const { data, error } = await supabase
      .from("petition") // Replace with your table name
      .insert([
        {
          title: formData.title,
          recipient: formData.recipient,
          problem: formData.problem,
          solution: formData.solution,
          authorId: userId,
        },
      ])
      .select();
authentication supabase
1个回答
0
投票

我能够通过更改创建 SUPABASE 客户端的方式解决我的问题。

我一直在做:

import { supabase } from "../../../lib/supabaseClient";

而不是导入在以下官方文档之后创建的以下助手:

import { createClient } from "@/utils/supabase/client";

之后,我可以通过以下方式创建 SUPABASE 客户端:

const supabase = createClient();

现在我的经过身份验证的插入工作没有错误。

据我了解,我之前使用的实用程序未能正确传递自动令牌。

© www.soinside.com 2019 - 2024. All rights reserved.