Skip to content

Instantly share code, notes, and snippets.

@iampato
Last active May 3, 2021 17:04
Show Gist options
  • Save iampato/64e31ef5b1f5a1ba8bed10bfafe8dab2 to your computer and use it in GitHub Desktop.
Save iampato/64e31ef5b1f5a1ba8bed10bfafe8dab2 to your computer and use it in GitHub Desktop.
Firebase Auth operations
part of '_repository.dart';
class UserRepository {
// Params
HttpNetworkUtil _networkUtil;
// Setup a singleton
static final UserRepository _userRepository = UserRepository._internal();
factory UserRepository({HttpNetworkUtil networkUtil}) {
_userRepository._networkUtil = networkUtil;
return _userRepository;
}
UserRepository._internal();
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
Logger _logger = Logger();
StreamController<String> _streamController = StreamController.broadcast();
/// signInWithGoogle
/// Params -> none
/// returns a future firebase User object
Future<User> signInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await _firebaseAuth.signInWithCredential(credential);
return _firebaseAuth.currentUser;
}
/// signInWithPhoneNumber
/// Params -> User phone number
/// return a future void
Future<void> signInWithPhoneNumber({@required String phone}) async {
try {
await FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phone,
verificationCompleted: null,
// (PhoneAuthCredential credential) async {
// // ANDROID ONLY!
// // Sign the user in (or link) with the auto-generated credential
// await _firebaseAuth.signInWithCredential(credential);
// return _firebaseAuth.currentUser.uid;
// },
verificationFailed: (FirebaseAuthException e) {
_logger.e("FirebaseAuthException\n$e");
throw PhoneNumberVerificationFailure();
},
codeSent: (String verificationId, int resendToken) {
_logger.i(
"VerificationId: $verificationId\nResenToken: $resendToken",
);
_streamController.sink.add(verificationId);
},
codeAutoRetrievalTimeout: (String verificationId) {
throw PhoneNumberCodeFailure();
},
);
} catch (e) {
_logger.e("signInWithPhoneNumber Exception\n$e");
}
}
Stream<String> getSignInStream() {
return _streamController.stream;
}
Future<User> verifyOTPCodes(String verificationId, String smsCode) async {
_logger.i("Verification id: $verificationId ... smsCode: $smsCode");
try {
final PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: verificationId,
smsCode: smsCode,
);
await _firebaseAuth.signInWithCredential(credential);
return _firebaseAuth.currentUser;
} catch (e) {
_logger.e("verifyOTPCodes Exception\n$e");
return null;
}
}
// logInWithEmailAndPassword
// login with email and password
Future<UserCredential> logInWithEmailAndPassword({
String email,
String password,
}) async {
assert(email != null && password != null);
try {
UserCredential result = await _firebaseAuth.signInWithEmailAndPassword(
email: email,
password: password,
);
return result;
} on SocketException {
throw InternetException();
} catch (e) {
_logger.e("logInWithEmailAndPassword Exception\n$e");
throw LogInWithEmailAndPasswordFailure();
}
}
// getUserId
// get the current userid of the logged in user
String getUserId() {
final currentUser = _firebaseAuth.currentUser;
return currentUser.uid;
}
// isSignedIn
// check if the current user is logged in
User getCurrentUser() {
final currentUser = _firebaseAuth.currentUser;
return currentUser;
}
// signOut
Future<void> signOut() async {
return Future.wait([
_firebaseAuth.signOut(),
]);
}
// forgetPassword
Future<void> forgetPassword(String email) async {
await _firebaseAuth.sendPasswordResetEmail(
email: email ?? _firebaseAuth.currentUser.email,
);
}
void disposeStream() {
_streamController.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment