verifyNewPhoneOTP method
Step 4: Verify OTP for new phone number and complete change
Implementation
Future<Map<String, dynamic>> verifyNewPhoneOTP({
required String tenantId,
required String phoneNumber,
required String otp,
}) async {
const query = r'''
mutation VerifyNewPhoneOTP($input: PhoneChangeInput!) {
verifyNewPhoneOTP(input: $input) {
success
message
otpSent
tenantUser {
id
phoneNumber
updatedAt
}
}
}
''';
final normalizedPhone = _normalizePhone(phoneNumber);
try {
final res = await _api.graphql(query, {
'input': {
'tenantID': tenantId,
'phoneNumber': normalizedPhone,
'otp': otp,
}
});
final data = res['verifyNewPhoneOTP'];
if (data == null) {
return {
'success': false,
'message': 'No data returned from server',
};
}
// If successful, update the saved phone number in SharedPreferences
if (data['success'] == true && data['tenantUser'] != null) {
final prefs = await SharedPreferences.getInstance();
final newPhone = data['tenantUser']['phoneNumber'];
if (newPhone != null) {
await prefs.setString('baas_phone', newPhone);
print(" Phone number updated in session: $newPhone");
}
}
return Map<String, dynamic>.from(data);
} catch (e) {
print("ERROR verifying new phone OTP: $e");
return {
'success': false,
'message': 'Failed to verify OTP: $e',
};
}
}