verifyPhone method
Verifies a phone.
correlation_id
(optional) transaction id to trace execution through call chain.recipientId
a recipient id of the sms settings to be verified smscode
a verification code for verifying sms Return Future that receives null for success. Throws error.
Implementation
@override
Future verifyPhone(
String? correlationId, String recipientId, String code) async {
var settings = SmsSettingsV1();
// Get existing settings
var data = await persistence.getOneById(correlationId, recipientId);
if (data == null) {
throw NotFoundException(correlationId, 'RECIPIENT_NOT_FOUND',
'Recipient ' + recipientId + ' was not found')
.withDetails('recipient_id', recipientId);
}
settings = data;
// Check and update verification code
var verified = settings.ver_code == code;
verified = verified || (_magicCode != null && code == _magicCode);
verified = verified &&
DateTime.now().millisecondsSinceEpoch <
DateTime.fromMillisecondsSinceEpoch(
settings.ver_expire_time?.millisecondsSinceEpoch ?? 0)
.millisecondsSinceEpoch;
if (!verified) {
throw BadRequestException(correlationId, 'INVALID_CODE',
'Invalid sms verification code ' + code)
.withDetails('recipient_id', recipientId)
.withDetails('code', code);
}
settings.verified = true;
settings.ver_code = null;
settings.ver_expire_time = null;
// Save user
data = await persistence.set(correlationId, settings);
// Asynchronous post-processing
_logActivity(
correlationId, settings, SmsSettingsActivityTypeV1.PhoneVerified);
}