Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -607,19 +607,16 @@ class GoTrueClient {
}

/// Returns a new session, regardless of expiry status.
/// Takes in an optional current session. If not passed in, then refreshSession() will attempt to retrieve it from getSession().
/// If the current session's refresh token is invalid, an error will be thrown.
/// Takes in an optional [refreshToken]. If not provided, then refreshSession() will attempt to retrieve it from the current session.
/// If no refresh token is available (neither provided nor in current session), an error will be thrown.
Future<AuthResponse> refreshSession([String? refreshToken]) async {
if (currentSession?.accessToken == null) {
_log.warning("Can't refresh session, no current session found.");
throw AuthSessionMissingException();
}
_log.info('Refresh session');

final currentSessionRefreshToken =
refreshToken ?? _currentSession?.refreshToken;

if (currentSessionRefreshToken == null) {
_log.warning("Can't refresh session, no refresh token found.");
throw AuthSessionMissingException();
}

Expand Down
24 changes: 24 additions & 0 deletions packages/gotrue/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,30 @@ void main() {
}
});

test('Refresh session with refreshToken when no current session exists',
() async {
await client.signInWithPassword(email: email1, password: password);

final refreshToken = client.currentSession?.refreshToken ?? '';
expect(refreshToken, isNotEmpty);

final newClient = GoTrueClient(
url: gotrueUrl,
headers: {
'apikey': anonToken,
},
);

expect(newClient.currentSession, isNull);

// This should work even though there's no current session,
// because we're providing a refreshToken parameter
final response = await newClient.refreshSession(refreshToken);
expect(response.session, isNotNull);
expect(response.session?.accessToken, isNotEmpty);
expect(newClient.currentSession?.accessToken, isNotEmpty);
});

test('Update user', () async {
await client.signInWithPassword(email: email1, password: password);

Expand Down