Finished Implementation of the google sign in provider and its tests, moved mock class definitions to the end of the file in all tests
This commit is contained in:
parent
e2ff9f8f56
commit
8c3a8d5274
4 changed files with 121 additions and 46 deletions
|
|
@ -1,28 +0,0 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
|
||||
class GoogleSignInProvider {
|
||||
final GoogleSignIn _googleSignIn;
|
||||
final FirebaseAuth _auth;
|
||||
|
||||
GoogleSignInProvider([GoogleSignIn googleSignIn, FirebaseAuth firebaseAuth])
|
||||
: _googleSignIn = googleSignIn ?? GoogleSignIn(),
|
||||
_auth = firebaseAuth ?? FirebaseAuth.instance;
|
||||
|
||||
Future<FirebaseUser> signIn() async {
|
||||
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
|
||||
final GoogleSignInAuthentication googleAuth =
|
||||
await googleUser.authentication;
|
||||
|
||||
final AuthCredential credential = GoogleAuthProvider.getCredential(
|
||||
accessToken: googleAuth.accessToken,
|
||||
idToken: googleAuth.idToken,
|
||||
);
|
||||
|
||||
final FirebaseUser user = await _auth.signInWithCredential(credential);
|
||||
print("signed in " + user.displayName);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
44
lib/src/resources/google_sign_in_provider.dart
Normal file
44
lib/src/resources/google_sign_in_provider.dart
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
class GoogleSignInProvider {
|
||||
final GoogleSignIn _googleSignIn;
|
||||
final FirebaseAuth _auth;
|
||||
|
||||
GoogleSignInProvider([GoogleSignIn googleSignIn, FirebaseAuth firebaseAuth])
|
||||
: _googleSignIn = googleSignIn ?? GoogleSignIn(),
|
||||
_auth = firebaseAuth ?? FirebaseAuth.instance;
|
||||
|
||||
Observable<FirebaseUser> get onAuthStateChange =>
|
||||
Observable(_auth.onAuthStateChanged);
|
||||
|
||||
Future<FirebaseUser> signIn() async {
|
||||
try {
|
||||
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
|
||||
final GoogleSignInAuthentication googleAuth =
|
||||
await googleUser.authentication;
|
||||
|
||||
final AuthCredential credential = GoogleAuthProvider.getCredential(
|
||||
accessToken: googleAuth.accessToken,
|
||||
idToken: googleAuth.idToken,
|
||||
);
|
||||
|
||||
final FirebaseUser user = await _auth.signInWithCredential(credential);
|
||||
return user;
|
||||
} catch (e) {
|
||||
print('Error signing in with Google: $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<FirebaseUser> getCurrentUser() async {
|
||||
return await _auth.currentUser();
|
||||
}
|
||||
|
||||
Future<void> signOut() async {
|
||||
await _auth.signOut();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue