question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js SSR and row-level security

See original GitHub issue

First off, thanks the team for this awesome library!

I’m using magic link authentication. I have found that even after a user has been identified, and if row-level security is enabled (to allow only a user with a specific email address), then inside getServerSideProps the data cannot be fetched using supabase client, i.e. this code is returning empty data and null error (and the user has been identified)

const { user } = await supabase.auth.api.getUserByCookie(req);

  if (!user) {
    return { props: {}, redirect: { destination: "/" } };
  }

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

Hope you will help me see what’s going on.

Thanks!!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gr-qftcommented, Nov 15, 2021

@kiwicopple Thanks a lot, supabase team is awesome!

Just a short clarification: I did use setAuthCookie and that was why I was able to call getUserByCookie inside getServerSideProps. As I commented above, I could then get user, but the following call to fetch the tables (i.g. const { data, error } = await supabase.from("")...) just return null data and undefined error when low-level security is set up.

I’m checking this ‘token’ that you just made available and will let you know. Thanks again!

0reactions
kiwicopplecommented, Nov 15, 2021

I may not be understanding your question properly, so if not feel free to comment back

I just don’t know what to do to pass access_token to getServerSideProps

I assume that getServerSideProps is your own route? Ideally when you first authenticate the user you call setAuthCookie in a server route. This will create a cookie, which is then passed with all network requests.

After that, you can pull the user’s details out of the cookie - you don’t need to store access_token anywhere. I just updated the latest version of supabase-js to support getting the token directly from the cookie. It should work like this:

export async function getServerSideProps({ req }: any) {
  const { token, user } = await supabase.auth.api.getUserByCookie(req);

  if (!user) {
    return { props: {}, redirect: { destination: "/" } };
  }

  const { data, error } = await supabase.setAuth(token)
    .from("")
    ... 
   
}

I’ll close this one for now, but if there is a bug that you feel needs fixing just let me know and I’ll reopen

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use row level security in a Next.js and Supabase app
Next.js is the React framework for production by Vercel. ... How to use row level security in a Next.js and Supabase app.
Read more >
Advanced Features: Next.js Compiler
The Next. js Compiler, written in Rust using SWC, allows Next. js to transform and minify your JavaScript code for production. This replaces...
Read more >
Make User State Globally Accessible in Next.js with React ...
Using React Context, we can create global variables that are available throughout our Next.js application. In this video, we create a useUser hook...
Read more >
Next.js Crash Course - SSG, SSR, API Routes, and more
In this short course we will learn about Next. js (the React Framework for Production). A framework that allows you to render a...
Read more >
What is the point of SSR these days? #10437 - GitHub
Next.js is a hybrid framework. We will be recommending static and static site generation as a default. We already output .html files if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found