[英]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 表中,我有以下策略:
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.