-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_otp_verification_input_widget.dart
133 lines (123 loc) · 4.19 KB
/
3_otp_verification_input_widget.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import 'package:firebase_multi_factor_auth/firebase_multi_factor_auth.dart';
import 'package:firebase_multi_factor_auth/provider/auth_provider.dart';
import 'package:firebase_multi_factor_auth/provider/user_provider.dart';
import 'package:firebase_multi_factor_auth/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class OTPVerificationWidget extends StatefulWidget {
const OTPVerificationWidget({Key? key}) : super(key: key);
@override
_OTPVerificationWidgetState createState() => _OTPVerificationWidgetState();
}
class _OTPVerificationWidgetState extends State<OTPVerificationWidget> {
final _otpController = TextEditingController();
var isLoading = false;
Future<Function?> verifyOtpInput() async {
setLoading(true);
if (_otpController.text.length > 6 ||
_otpController.text.isEmpty ||
!_otpController.text.contains(RegExp("[0-9]"))) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Please enter a valid OTP")));
setLoading(false);
return null;
}
_otpController.text = _otpController.text.replaceAll(" ", "").trim();
await FirebaseMultiFactorAuth.inputPhoneOTP(
context: this.context, otpCode: _otpController.text)
.then(
(isSuccessful) {
if (!isSuccessful) {
setLoading(false);
}
},
);
}
@override
Widget build(BuildContext context) {
var userProvider = Provider.of<UserProvider>(context, listen: false);
if (isLoading) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (FirebaseMultiFactorAuth.isPhoneNumberInputed(context: context)) {
if (userProvider.userFetched != null &&
(userProvider.userFetched!.uidProviderFirst !=
userProvider.userLocal.uidProviderFirst ||
userProvider.userLocal.phoneNumber !=
userProvider.userFetched!.phoneNumber)) {
logger.d(
"Not match ${userProvider.userFetched!.uidProviderFirst} != ${userProvider.userLocal.uidProviderFirst}");
logger.d(
"Not match ${userProvider.userFetched!.phoneNumber} != ${userProvider.userLocal.phoneNumber}");
return Scaffold(
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Please contact the support. Your login method and your phone number did not match.",
textAlign: TextAlign.center,
),
OutlinedButton(
onPressed: () async =>
FirebaseMultiFactorAuth.signOut(context: context),
child: Text("Change Provider")),
],
),
),
);
}
}
//}
return Container(
margin: const EdgeInsets.all(16),
child: Column(
children: [
const Spacer(),
TextFormField(
keyboardType: TextInputType.number,
controller: _otpController,
autofocus: true,
cursorColor: Theme.of(context).colorScheme.primary,
maxLength: 6,
onFieldSubmitted: (value) async {
await verifyOtpInput();
},
// inputFormatters: [],
decoration: const InputDecoration(
icon: Icon(Icons.sms),
labelText: "Enter OTP",
//labelStyle: TextStyle(color: Color(0xFF6200EE))
helperText:
"OTP has been send per sms.", // TOdo translation & better text
enabledBorder: UnderlineInputBorder(),
),
),
const SizedBox(
height: 16,
),
TextButton(
onPressed: () async {
await verifyOtpInput();
},
child: const Text("VERIFY"),
),
const Spacer(),
],
),
);
}
void setLoading(bool loading) {
setState(() {
isLoading = loading;
});
}
@override
void dispose() {
_otpController.dispose();
super.dispose();
}
}