auth-js icon indicating copy to clipboard operation
auth-js copied to clipboard

supabase.auth.api.setAuthCookie(req, res) not working in nextjs API route. Invalid JWT.

Open roker15 opened this issue 2 years ago • 2 comments

I am trying to fetch image from storage through nextjs API. I am using -

"next": "^12.1.6",
"@supabase/supabase-js": "1.35.2",
"react": "^18.1.0",

  1. If RLS is not set then I am able to fetch Image.
  2. If RLS is set I am not able to fetch image. With following error -

{"statusCode":"401","error":"Invalid JWT","message":"new row violates row-level security policy for table \"objects\""}

I have already set auth cookies by calling following code inside auth route

import { NextApiRequest, NextApiResponse } from "next";
import { supabase } from "../../lib/supabaseClient";
export default function handler(req: NextApiRequest, res: NextApiResponse) {
 
 supabase.auth.api.setAuthCookie(req, res);
}

Code for calling my auth api is inside _app.tsx

supabase.auth.onAuthStateChange((event, session) => {
   

    fetch("/api/auth", {
      method: "POST",
      headers: new Headers({ "Content-Type": "application/json" }),
      credentials: "same-origin",
      body: JSON.stringify({ event, session }),
    })
  });

roker15 avatar Jun 13 '22 05:06 roker15

Hey @roker15 — what are your RLS settings?

saltcod avatar Jun 20 '22 23:06 saltcod

Hey all,

Transferring this to the Supabase Auth repo so we can best track this. @roker15 do let us know if you are still facing this issue -- happy to jump in and help.

J0 avatar Jul 18 '22 06:07 J0

Hey @roker15 this sounds like a problem with your RLS policies. Please open a support ticket if you need assistance.

hf avatar Dec 30 '22 18:12 hf