refreshToken method

Future<AuthResponse> refreshToken({
  1. required String refreshToken,
})

Refresh access token

Implementation

Future<AuthResponse> refreshToken({required String refreshToken}) async {
  try {
    final response = await _client.guestRequest<Map<String, dynamic>>(
      '/rest/V1/integration/customer/token',
      data: {'refresh_token': refreshToken},
    );

    if (response.statusCode == 200) {
      final authResponse = AuthResponse.fromJson(response.data!);

      // Store new tokens
      await _client.storeTokens(
        accessToken: authResponse.accessToken,
        refreshToken: authResponse.refreshToken,
        customerId: authResponse.customer.id,
      );

      return authResponse;
    } else {
      throw Exception('Token refresh failed: ${response.statusMessage}');
    }
  } on DioException catch (e) {
    throw Exception('Token refresh failed: ${e.message}');
  } catch (e) {
    throw Exception('Token refresh failed: $e');
  }
}