-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Firebase auth services to utilize in Flutter
- Loading branch information
1 parent
89b0908
commit 733f438
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
@immutable | ||
class User { | ||
final String uid; | ||
final String email; | ||
final String photoUrl; | ||
final String displayName; | ||
|
||
const User({ | ||
@required this.uid, | ||
this.email, | ||
this.photoUrl, | ||
this.displayName | ||
}); | ||
} |
52 changes: 52 additions & 0 deletions
52
frontend/memoree_client/lib/app/services/firebase_auth.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'package:firebase_auth/firebase_auth.dart' as auth; | ||
import 'package:firebase_core/firebase_core.dart'; | ||
import 'package:google_sign_in/google_sign_in.dart'; | ||
import 'package:memoree_client/app/models/user.dart'; | ||
|
||
class FirebaseAuthService { | ||
final auth.FirebaseAuth _firebaseAuth; | ||
final GoogleSignIn _googleSignIn; | ||
|
||
FirebaseAuthService({auth.FirebaseAuth firebaseAuth, GoogleSignIn googleSignin}) | ||
: _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance, | ||
_googleSignIn = googleSignin ?? GoogleSignIn(); | ||
|
||
User _userFromFirebase(auth.User user) { | ||
if (user == null) { | ||
return null; | ||
} | ||
return User( | ||
uid: user.uid, | ||
email: user.email, | ||
displayName: user.displayName, | ||
photoUrl: user.photoURL, | ||
); | ||
} | ||
|
||
Stream<User> get onAuthStateChanged { | ||
return _firebaseAuth.authStateChanges().map(_userFromFirebase); | ||
} | ||
|
||
Future<User> signInWithGoogle() async { | ||
await Firebase.initializeApp(); | ||
|
||
final googleUser = await _googleSignIn.signIn(); | ||
final googleAuth = await googleUser.authentication; | ||
final credential = auth.GoogleAuthProvider.credential( | ||
accessToken: googleAuth.accessToken, | ||
idToken: googleAuth.idToken, | ||
); | ||
final authResult = await _firebaseAuth.signInWithCredential(credential); | ||
return _userFromFirebase(authResult.user); | ||
} | ||
|
||
Future<void> signOut() async { | ||
await _googleSignIn.disconnect(); | ||
return _firebaseAuth.signOut(); | ||
} | ||
|
||
Future<User> currentUser() async { | ||
final user = _firebaseAuth.currentUser; | ||
return _userFromFirebase(user); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.