Flutter plugin for authenticating phone number. this plugin send OTP to mobile numbers and verify it and return verified mobile number to user.
Install
dependencies:
phone_email_auth: ^0.0.6
Import
import 'package:phone_email_auth/phone_email_auth.dart';
Initialize phone email plugin
PhoneEmail.initializeApp(clientId: 'YOUR_CLIENT_ID');
clientId : Set clientId which you obtained from Profile Details section of Admin Dashboard of Phone Email.
child: PhoneLoginButton(
borderRadius: 8,
buttonColor: Colors.teal,
label: 'Sign in with Phone',
onSuccess: (String accessToken, String jwtToken) {
if (accessToken.isNotEmpty) {
setState(() {
userAccessToken = accessToken;
jwtUserToken = jwtToken;
hasUserLogin = true;
});
}
},
)The PhoneLoginButton will return the accessToken and jwtToken, which are necessary for obtaining the verified phone number.
Once you've obtained the accessToken, get verified phone number by calling the getUserInfo() function. Use the following code snippet to retrieve the verified phone number and user details.
PhoneEmail.getUserInfo(
accessToken: userAccessToken,
clientId: phoneEmail.clientId,
onSuccess: (userData) {
setState(() {
phoneEmailUserModel = userData;
var countryCode = phoneEmailUserModel?.countryCode;
var phoneNumber = phoneEmailUserModel?.phoneNumber;
var firstName = phoneEmailUserModel?.firstName;
var lastName = phoneEmailUserModel?.lastName;
// Use this verified phone number to register user and create your session
});
},
);Integrate an email alert icon on your screen for a successfully authenticated user. Use the following code snippet to fetch the unread email count and display the email icon.
floatingActionButton: hasUserLogin
? EmailAlertButton(
jwtToken: jwtUserToken,
) : const Offstage(),
);