简体   繁体   English

getServerSideProps 上的 Supbase 策略 - Next.js

[英]Supabase policies on getServerSideProps - Next.js

I'm crafting a Trello clone with Next.js and Supabase as a BaaS.我正在使用 Next.js 和 Supabase 作为 BaaS 制作 Trello 克隆。

In my Supabase table I have this policies:在我的 Supbase 表中,我有以下策略:

在此处输入图像描述<\/a>

Policies are working grate on client side with the following code:策略在客户端使用以下代码运行:

const { data } = await supabase
    .from<BoardType>('board')
    .select('*')
    .eq('id', board.id)
    .single();

I found out it sends the request on the datebase with the anon key and not the authenticated jwt in the getSSProps.我发现它使用匿名密钥而不是 getSSProps 中经过身份验证的 jwt 在数据库上发送请求。 not sure why or if its intentional.不知道为什么或是否是故意的。 If you add the role() = 'anon' rule it works, but that is not what you want in my opinion.如果您添加 role() = 'anon' 规则,它会起作用,但我认为这不是您想要的。

Might not be the best way, but this is how I solved it with some hints from this Github discussion https://github.com/supabase/supabase/discussions/1094#discussioncomment-714633可能不是最好的方法,但这就是我通过这个 Github 讨论的一些提示解决它的方法https://github.com/supabase/supabase/discussions/1094#discussioncomment-714633

From my understanding, the Supabase client is a little different on the server compared to in the browser (no session).据我了解,与浏览器(无会话)相比,服务器上的 Supabase 客户端略有不同。 Pulling the token out of cookies and adding it to the client lets the requests to supabase be from the authenticated user.从 cookie 中提取令牌并将其添加到客户端可以使对 supabase 的请求来自经过身份验证的用户。

export const getServerSideProps: GetServerSideProps = ({ req }) => {
  const { user, token } = await supabase.auth.api.getUserByCookie(req);
  supabase.auth.setAuth(token);

  const { data, error } = await supabase.from(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Next.js GetServerSideProps 自定义包装器 TypeScript - Next.js GetServerSideProps custom wrapper TypeScript Next.js getServerSideProps 始终未定义 - Next.js getServerSideProps is always undefined Next.js:getServerSideProps 无法使用 Next.js 和 Next-redux-wrapper 以及 TypeScript - Next.js: getServerSideProps not working using Next.js with Next-redux-wrapper with TypeScript 无法在服务器端注销用户(Next.js 和 Supabase) - Cannot log out user on the server side (Next.js and Supabase) 导航栏中的用户名 Next.js -Typescript-Supabase - Username in Navbar Next.js -Typescript-Supabase Next.JS typescript 通过 getServerSideProps 将 props 返回给外部组件 - Next.JS typescript pass getServerSideProps returned props to external component 如何正确键入 Next.js getServerSideProps 作为 Function 声明 - How to Correctly Type Next.js getServerSideProps as a Function Declaration Next.js:使用带有 TypeScript 的 next-redux-wrapper 在 getServerSideProps 中调用 Thunks? - Next.js: Calling Thunks in getServerSideProps with next-redux-wrapper with TypeScript? Typescript next.js + i18n 的正确 getServerSideProps 语法 - proper getServerSideProps syntax for Typescript next.js + i18n 对于将 getServerSideProps 类型传递给 Next.Js 页面道具的最佳实践,哪个更好 - Which is better for best practice about passing getServerSideProps type to Next.Js page props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM