Skip to content

Instantly share code, notes, and snippets.

@M001T
Created April 15, 2021 18:24
Show Gist options
  • Save M001T/b7a99f09ce656eabfbed0c5fcad3a77a to your computer and use it in GitHub Desktop.
Save M001T/b7a99f09ce656eabfbed0c5fcad3a77a to your computer and use it in GitHub Desktop.
Sistema de SMS Auth com firebase
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