progNewbie
progNewbie

Reputation: 4832

Supabase: Invalid JWT after having app running for some time

I am calling

await Supabase.initialize(
  url: config.api.url,
  anonKey: config.api.anonKey,
);

in my main.dart and later using this code to call an Edge-Function:

final authHeader = Supabase.instance.client.auth.headers['Authorization'] ?? '';
SseClient(
    http.Request(
      'POST',
      Uri.parse('${getConfigFromEnv().api.url}/functions/v1/chat_stream'),
    )
      ..headers.addAll({
        'Authorization': authHeader,
        'Content-Type': 'application/json',
      })
      ..body = jsonEncode(data),
    httpClientProvider: http.Client.new,
    );
}

This works fine! But if my app is open for some time, it seems that my JWT expires and when I trigger the Edge-Function again, I get Invalid JWT as a response.

What do I have to do to make sure, that the auth.headers are renewed?

Thanks a lot!

Upvotes: 2

Views: 1508

Answers (1)

dshukertjr
dshukertjr

Reputation: 18670

You can call the refreshSession() method to make sure the token is fresh.

await supabase.auth.refreshSession();
final authHeader = Supabase.instance.client.auth.headers['Authorization'] ?? '';
SseClient(
    http.Request(
      'POST',
      Uri.parse('${getConfigFromEnv().api.url}/functions/v1/chat_stream'),
    )
      ..headers.addAll({
        'Authorization': authHeader,
        'Content-Type': 'application/json',
      })
      ..body = jsonEncode(data),
    httpClientProvider: http.Client.new,
    );
}

Upvotes: 1

Related Questions