refreshToken method

Future<AuthToken> refreshToken()

Refreshes the access token

Implementation

Future<AuthToken> refreshToken() async {
  final refresh = tokenStorage.refreshToken;
  if (refresh == null) {
    throw const AuthException('No refresh token available');
  }

  final response = await _client.post(
    Uri.parse('$baseUrl/auth/refresh'),
    headers: {'Content-Type': 'application/json'},
    body: jsonEncode({'refreshToken': refresh}),
  );

  final data = _handleResponse(response);
  final token = AuthToken.fromJson(data);

  await tokenStorage.saveTokens(
    accessToken: token.accessToken,
    refreshToken: token.refreshToken,
  );

  return token;
}