Last active
June 22, 2021 04:50
-
-
Save funwithflutter/27662393d7523b0adb68977d13d6d1ff to your computer and use it in GitHub Desktop.
Flutter Web Firebase Auth
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/firebase.dart'; | |
import 'package:meta/meta.dart'; | |
@immutable | |
class UserRepository { | |
UserRepository({Auth firebaseAuth, GoogleAuthProvider googleSignin}) | |
: _firebaseAuth = firebaseAuth ?? auth(), | |
_googleSignIn = googleSignin ?? GoogleAuthProvider(); | |
final Auth _firebaseAuth; | |
final GoogleAuthProvider _googleSignIn; | |
Future<UserCredential> signInWithGoogle() async { | |
try { | |
return await _firebaseAuth.signInWithPopup(_googleSignIn); | |
} catch (e) { | |
print('Error in sign in with google: $e'); | |
throw '$e'; | |
} | |
} | |
Future<UserCredential> signInWithCredentials( | |
String email, String password) async { | |
try { | |
return await _firebaseAuth.signInWithEmailAndPassword(email, password); | |
} catch (e) { | |
print('Error in sign in with credentials: $e'); | |
// return e; | |
throw '$e'; | |
} | |
} | |
Future<UserCredential> signUp({String email, String password}) async { | |
try { | |
return await _firebaseAuth.createUserWithEmailAndPassword( | |
email, | |
password, | |
); | |
} catch (e) { | |
print('Error siging in with credentials: $e'); | |
throw '$e'; | |
// throw Error('Error signing up with credentials: $e'); | |
// return e; | |
} | |
} | |
Future<dynamic> signOut() async { | |
try { | |
return Future.wait([ | |
_firebaseAuth.signOut(), | |
]); | |
} catch (e) { | |
print ('Error signin out: $e'); | |
// return e; | |
throw '$e'; | |
} | |
} | |
Future<bool> isSignedIn() async { | |
final currentUser = _firebaseAuth.currentUser; | |
return currentUser != null; | |
} | |
Future<String> getUser() async { | |
return (_firebaseAuth.currentUser).email; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment