Created
April 15, 2021 18:24
-
-
Save M001T/b7a99f09ce656eabfbed0c5fcad3a77a to your computer and use it in GitHub Desktop.
Sistema de SMS Auth com firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_auth/firebase_auth.dart'; | |
class SMSFunctions { | |
static Future<void> sendCodeToPhoneNumber( | |
String phoneNo, | |
Function onSuccess, | |
Function onFailed, | |
Function onRetrival, | |
) async { | |
FirebaseAuth.instance.signOut(); | |
// Verifica Celular | |
final PhoneVerificationCompleted verificationCompleted = | |
(AuthCredential user) { | |
onRetrival(); | |
}; | |
// Falhou ao verificar o celular | |
final PhoneVerificationFailed verificationFailed = | |
(FirebaseAuthException authException) { | |
onFailed(); | |
}; | |
// Enviar codigo para o celular | |
final PhoneCodeSent codeSent = | |
(String verificationId, [int forceResendingToken]) async { | |
verificationId = verificationId; | |
onSuccess(verificationId); | |
}; | |
// Pegar automaticamente o código enviado | |
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout = | |
(String verificationId) { | |
verificationId = verificationId; | |
onFailed(); | |
}; | |
await FirebaseAuth.instance.verifyPhoneNumber( | |
phoneNumber: phoneNo, | |
timeout: const Duration(seconds: 5), | |
verificationCompleted: verificationCompleted, | |
verificationFailed: verificationFailed, | |
codeSent: codeSent, | |
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout); | |
} | |
static Future<bool> confirmSMS(String smsCode, String verificationId) async { | |
final AuthCredential credential = PhoneAuthProvider.credential( | |
verificationId: verificationId, | |
smsCode: smsCode, | |
); | |
UserCredential authResult; | |
try { | |
authResult = await FirebaseAuth.instance.signInWithCredential(credential); | |
final User currentUser = authResult.user; | |
if (currentUser != null) | |
return true; | |
else | |
return false; | |
} catch (e) {} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment