SupabaseS
Supabase4y ago
glen

Custom Supabase jwt returns invalid signature

Hi
Creating and using Supabase jwt returns an invalid signature but I don't know why.
(I have verified my keys)

Create a Supabase access token signed with a Supabase secret

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const payload = {
    userId: req.query.user,
    exp: Math.floor(Date.now() / 1000) + 60 * 60,
  };

  const token = jwt.sign(payload, supabaseKey);

  return res.status(200).json({ token });
}



Initialize a Supabase client with the access token

export const getSupabase = (access_token: string) => {
  const supabase = createClient(supabaseUrl, supabaseAnonKey, {
    global: {
      headers: {
        Authorization: `Bearer ${access_token}`,
      },
    },
  });

  return supabase;
};


Upload file to Supabase storage
 async (file: File) => {

     // Get token from NextJs Api route
     const response = await fetch(
        `http://localhost:3002/api/supabase?user=${user}`
      );
     const { token } = await response.json();

     // Pass token to Supabase client 
     const supabase = getSupabase(token);

     const { data, error } = await supabase.storage
         .from(bucket)
         .upload(file.name, file);
     });
    },


Returns
{statusCode: '400', error: 'invalid signature', message: 'invalid signature'}
,
Was this page helpful?