Skip to content

Commit

Permalink
fix(realtime_client, supabase): pass apikey as the initial access tok…
Browse files Browse the repository at this point in the history
…en for realtime client (#596)

* pass apikey as the initial access token for realtime client

* ignore lint error for new AppLifecycleState
  • Loading branch information
dshukertjr committed Aug 22, 2023
1 parent a349ae0 commit af8e368
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/realtime_client/lib/src/realtime_client.dart
Expand Up @@ -104,7 +104,8 @@ class RealtimeClient {
eventsPerSecondLimitMs = (1000 / int.parse(eventsPerSecond)).floor();
}

accessToken = this.headers['Authorization']?.split(' ').last;
final customJWT = this.headers['Authorization']?.split(' ').last;
accessToken = customJWT ?? params['apikey'];

this.reconnectAfterMs =
reconnectAfterMs ?? RetryTimer.createRetryFunction();
Expand Down
4 changes: 3 additions & 1 deletion packages/realtime_client/test/socket_test.dart
Expand Up @@ -39,7 +39,8 @@ void main() {

group('constructor', () {
test('sets defaults', () async {
final socket = RealtimeClient('wss://example.com/socket');
final socket =
RealtimeClient('wss://example.com/socket', params: {'apikey': '123'});
expect(socket.channels.length, 0);
expect(socket.sendBuffer.length, 0);
expect(socket.ref, 0);
Expand All @@ -65,6 +66,7 @@ void main() {
socket.headers['X-Client-Info']!.split('/').first,
'realtime-dart',
);
expect(socket.accessToken, '123');
});

test('overrides some defaults with options', () async {
Expand Down
4 changes: 4 additions & 0 deletions packages/supabase/test/client_test.dart
Expand Up @@ -55,6 +55,10 @@ void main() {
containsPair('log_level', 'info'),
);
});

test('realtime access token is set properly', () {
expect(client.realtime.accessToken, supabaseKey);
});
});

group('auth', () {
Expand Down
1 change: 1 addition & 0 deletions packages/supabase_flutter/lib/src/supabase_auth.dart
Expand Up @@ -159,6 +159,7 @@ class SupabaseAuth with WidgetsBindingObserver {

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// ignore:missing_enum_constant_in_switch
switch (state) {
case AppLifecycleState.resumed:
_recoverSupabaseSession();
Expand Down

0 comments on commit af8e368

Please sign in to comment.