Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bad_jwt when auth.getUser #1196

Open
2 tasks done
FerranVidalB opened this issue May 26, 2024 · 1 comment
Open
2 tasks done

bad_jwt when auth.getUser #1196

FerranVidalB opened this issue May 26, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@FerranVidalB
Copy link

Bug report

Created an Edge Function for user-self-deletion but when I create the supabaseClient and try to getUser(jwt) I recieve this error

code: "bad_jwt"
name: "AuthApiError"
status: 403
__isAuthError: true

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

import { serve } from 'https://deno.land/[email protected]/http/server.ts'
import { createClient } from 'https://esm.sh/@supabase/[email protected]'
import { corsHeaders } from '../_shared/cors.ts'

console.log(`Function "user-self-deletion" up and running!`)

serve(async (req: Request) => {
  // This is needed if you're planning to invoke your function from a browser.
  if (req.method === 'OPTIONS') {
    return new Response('ok', { headers: corsHeaders })
  }
  try {
    const authorization = req.headers.get('Authorization');
    if (!authorization) { 
      return new Response(JSON.stringify({ error: "No authorization header passed" }), 
      { status: 500, headers: { 'Content-Type': 'application/json' }, }); 
    }

    // Create a Supabase client with the Auth context of the logged in user.
    const supabaseClient = createClient(
      // Supabase API URL - env var exported by default.
      Deno.env.get('SUPABASE_URL') ?? '',
      // Supabase API ANON KEY - env var exported by default.
      Deno.env.get('SUPABASE_ANON_KEY') ?? '',
      // Create client with Auth context of the user that called the function.
      // This way your row-level-security (RLS) policies are applied.
      { global: { headers: { Authorization: req.headers.get('Authorization')! } } }
    )


    // const user_id =await supabaseClient.auth.getUser(authorization)
    // Get JWT from auth header
    const jwt = authorization.replace("Bearer ", "");

    // Get the user object
    const {  data: { user },} = await supabaseClient.auth.getUser(jwt);

Expected behavior

I want to know why I'm getting this error.

Screenshots

System information

[email protected]

Additional context

I checked the SUPABASE_URL, ANON_KEY and they are ok. The jwt is not empty.
@FerranVidalB FerranVidalB added the bug Something isn't working label May 26, 2024
@Electronica-dev
Copy link

I have a very similar error. Did you manage to find out what was wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants