© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
2 replies
boeledi

Edge Function - User authentication from Bearer - how to?

Hello,
I am trying to identify the caller of an Edge Function, but I systematically get a "null" value.
Here is what I do:
* On the client side (Flutter), once the user has signed in, I am invoking the function
FunctionResponse? res;
    final String? accessToken = Supabase.instance.client.auth.session()?.accessToken;

    try {
      res = await Supabase.instance.client.functions.invoke(
        'ef_authentication_test',
        body: {
          "action": "action",
          "data": {
            "hello": "worled",
          },
        },
        headers: {'Authorization': 'Bearer $accessToken'},
      );

      TraceHelper.logE('[SupabaseHelper::invokeFunction] -- Response', error: res);
      
    } catch (e) {
      TraceHelper.logE('[SupabaseHelper::invokeFunction] -- Exception', error: e);
    }
FunctionResponse? res;
    final String? accessToken = Supabase.instance.client.auth.session()?.accessToken;

    try {
      res = await Supabase.instance.client.functions.invoke(
        'ef_authentication_test',
        body: {
          "action": "action",
          "data": {
            "hello": "worled",
          },
        },
        headers: {'Authorization': 'Bearer $accessToken'},
      );

      TraceHelper.logE('[SupabaseHelper::invokeFunction] -- Response', error: res);
      
    } catch (e) {
      TraceHelper.logE('[SupabaseHelper::invokeFunction] -- Exception', error: e);
    }

The call invocation works, the <accessToken> exists.

At the level of the Edge Function, I am running the following:
export const supabaseClient = createClient(
  // Supabase API URL - env var exported by default when deployed.
  'https://KEY.supabase.co',
  // Supabase API ANON KEY - env var exported by default when deployed.
  '<ANON KEY>'
);

export function supabaseUser(req:Request) {
  // Get the authorization header from the request.
  const authHeader = req.headers?.get('Authorization')?.replace("Bearer ", "") || "";
  console.log(`Authorization: ${authHeader}`);
  
  supabaseClient.auth.setAuth(authHeader);

  console.log(`User: ${supabaseClient.auth.user()}`);

  return supabaseClient.auth.user();
};
export const supabaseClient = createClient(
  // Supabase API URL - env var exported by default when deployed.
  'https://KEY.supabase.co',
  // Supabase API ANON KEY - env var exported by default when deployed.
  '<ANON KEY>'
);

export function supabaseUser(req:Request) {
  // Get the authorization header from the request.
  const authHeader = req.headers?.get('Authorization')?.replace("Bearer ", "") || "";
  console.log(`Authorization: ${authHeader}`);
  
  supabaseClient.auth.setAuth(authHeader);

  console.log(`User: ${supabaseClient.auth.user()}`);

  return supabaseClient.auth.user();
};

The <authHeader> corresponds to the <accessToken> from the client side.

The <User> I am getting is always NULL. => I need to get this to validate the requestor.

Could anybody tell me what I am doing wrong?

Thanks in advance,
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Authentication from CRON job -> Edge Function
SupabaseSSupabase / help-and-questions
2mo ago
Connection Pool authentication fails from edge function
SupabaseSSupabase / help-and-questions
4y ago
Edge Function Delete User
SupabaseSSupabase / help-and-questions
4y ago
How to delete Edge function?
SupabaseSSupabase / help-and-questions
3mo ago