confirmEmailChange method

Future<void> confirmEmailChange(
  1. String emailChangeToken,
  2. String userPassword, {
  3. Map<String, dynamic> body = const {},
  4. Map<String, dynamic> query = const {},
  5. Map<String, String> headers = const {},
})

Confirms auth record new email address.

If the current AuthStore.model matches with the record from the token, then on success the client AuthStore will be also cleared.

Implementation

Future<void> confirmEmailChange(
  String emailChangeToken,
  String userPassword, {
  Map<String, dynamic> body = const {},
  Map<String, dynamic> query = const {},
  Map<String, String> headers = const {},
}) {
  final enrichedBody = Map<String, dynamic>.of(body);
  enrichedBody["token"] = emailChangeToken;
  enrichedBody["password"] = userPassword;

  return client
      .send(
    "$baseCollectionPath/confirm-email-change",
    method: "POST",
    body: enrichedBody,
    query: query,
    headers: headers,
  )
      .then((item) {
    final parts = emailChangeToken.split(".");
    if (parts.length != 3) {
      return;
    }

    final payloadPart = base64.normalize(parts[1]);
    final payload = jsonDecode(utf8.decode(base64Decode(payloadPart)))
        as Map<String, dynamic>;

    if (client.authStore.model != null &&
        client.authStore.model is RecordModel &&
        (client.authStore.model as RecordModel).id == payload["id"] &&
        (client.authStore.model as RecordModel).collectionId ==
            payload["collectionId"]) {
      client.authStore.clear();
    }
  });
}