signInWithCode method

Future<T> signInWithCode(
  1. String url,
  2. String code
)

Authenticates a user using a code.

Sends a POST request to a specified endpoint and expects a token in return.

  • url: The suffix to append to the base URL.
  • code: The authentication code.

Returns a Future that completes with the authenticated user data.

Implementation

Future<T> signInWithCode(String url, String code) async {
  final parsedUrl = Uri.parse("${CoffeeUtil.concatUrl(CoffeeUtil.concatUrl(_baseEndpoint, 'user'), url)}?$code");
  final response = await _httpClient.post(parsedUrl);

  CoffeeUtil.handleErrorMessage(response);

  final data = jsonDecode(response.body) as Map<String, dynamic>;

  if(data['token'] != null) {
    CoffeeStorage.setJwtToken(data['token']);
  }

  if(data['user'] != null) {
    currentUser = fromJson(data['user']);
    CoffeeStorage.setLoggedUser(currentUser!);
  }

  await CoffeeStorage.clearAllCache();

  return currentUser!;
}