Created
June 30, 2020 10:07
-
-
Save loicgeek/8441ac93d0ce45c90f71dc5c4fe93c02 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
class AuthService { | |
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; | |
final GoogleSignIn _googleSignIn = GoogleSignIn(); | |
Stream<FirebaseUser> onAuthChanged() { | |
return _firebaseAuth.onAuthStateChanged; | |
} | |
Future<String> signInWithEmailAndPassword( | |
String email, String password) async { | |
AuthResult result = await _firebaseAuth.signInWithEmailAndPassword( | |
email: email, password: password); | |
return result.user.uid; | |
} | |
Future<String> signInWithGoogle() async { | |
await _googleSignIn.signOut(); | |
final GoogleSignInAccount googleUser = await _googleSignIn.signIn(); | |
final GoogleSignInAuthentication googleAuth = | |
await googleUser.authentication; | |
final AuthCredential credential = GoogleAuthProvider.getCredential( | |
accessToken: googleAuth.accessToken, | |
idToken: googleAuth.idToken, | |
); | |
await _firebaseAuth.signInWithCredential(credential); | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
return user.uid; | |
} | |
Future<bool> isSignedIn() async { | |
final currentUser = await _firebaseAuth.currentUser(); | |
return currentUser != null; | |
} | |
Future<String> signUp( | |
String email, | |
String password, { | |
String username, | |
}) async { | |
AuthResult result = await _firebaseAuth.createUserWithEmailAndPassword( | |
email: email, | |
password: password, | |
); | |
return result.user.uid; | |
} | |
Future<FirebaseUser> getCurrentUser() async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
return user; | |
} | |
Future<String> getAccessToken() async { | |
FirebaseUser user = await getCurrentUser(); | |
IdTokenResult tokenResult = await user.getIdToken(); | |
return tokenResult.token; | |
} | |
Future<String> getRefreshToken() async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
IdTokenResult tokenResult = await user.getIdToken(refresh: true); | |
return tokenResult.token; | |
} | |
Future<void> signOut() async { | |
return Future.wait([ | |
_firebaseAuth.signOut(), | |
_googleSignIn.signOut(), | |
]); | |
} | |
Future<void> sendEmailVerification() async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
user.sendEmailVerification(); | |
} | |
Future<bool> isEmailVerified() async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
return user.isEmailVerified; | |
} | |
@override | |
Future<void> changeEmail(String email) async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
return user.updateEmail(email); | |
} | |
@override | |
Future<void> changePassword(String password) async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
user.updatePassword(password).then((_) { | |
print("Succesfull changed password"); | |
}).catchError((error) { | |
print("Password can't be changed" + error.toString()); | |
}); | |
return null; | |
} | |
@override | |
Future<void> deleteUser() async { | |
FirebaseUser user = await _firebaseAuth.currentUser(); | |
user.delete().then((_) { | |
print("Succesfull user deleted"); | |
}).catchError((error) { | |
print("user can't be delete" + error.toString()); | |
}); | |
return null; | |
} | |
@override | |
Future<void> sendPasswordResetMail(String email) async { | |
await _firebaseAuth.sendPasswordResetEmail(email: email); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment