feat(authentication): add google sign_in support (closes #59)
This commit is contained in:
		
							parent
							
								
									94d573a584
								
							
						
					
					
						commit
						c7b241de2d
					
				@ -14,7 +14,6 @@
 | 
				
			|||||||
// You should have received a copy of the GNU General Public License
 | 
					// You should have received a copy of the GNU General Public License
 | 
				
			||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
					// along with this program. If not, see <https://www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import 'package:firebase_auth/firebase_auth.dart';
 | 
					 | 
				
			||||||
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
 | 
					import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
 | 
				
			||||||
import 'package:wyatt_type_utils/wyatt_type_utils.dart';
 | 
					import 'package:wyatt_type_utils/wyatt_type_utils.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -182,6 +181,39 @@ class AuthenticationFirebaseDataSourceImpl
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  Future<Account> signInWithGoogle() async {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      // Trigger the authentication flow
 | 
				
			||||||
 | 
					      final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // Obtain the auth details from the request
 | 
				
			||||||
 | 
					      final GoogleSignInAuthentication? googleAuth =
 | 
				
			||||||
 | 
					          await googleUser?.authentication;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // Create a new credential
 | 
				
			||||||
 | 
					      final credential = GoogleAuthProvider.credential(
 | 
				
			||||||
 | 
					        accessToken: googleAuth?.accessToken,
 | 
				
			||||||
 | 
					        idToken: googleAuth?.idToken,
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      final userCredential =
 | 
				
			||||||
 | 
					          await _firebaseAuth.signInWithCredential(credential);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      _latestCreds = userCredential;
 | 
				
			||||||
 | 
					      final user = userCredential.user;
 | 
				
			||||||
 | 
					      if (user.isNotNull) {
 | 
				
			||||||
 | 
					        return _mapper(user!);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        throw Exception(); // Get caught just after.
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } on FirebaseAuthException catch (e) {
 | 
				
			||||||
 | 
					      throw SignInWithGoogleFailureFirebase.fromCode(e.code);
 | 
				
			||||||
 | 
					    } catch (_) {
 | 
				
			||||||
 | 
					      throw SignInWithGoogleFailureFirebase();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Future<bool> verifyPasswordResetCode({required String code}) async {
 | 
					  Future<bool> verifyPasswordResetCode({required String code}) async {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
 | 
				
			|||||||
@ -123,6 +123,25 @@ class AuthenticationMockDataSourceImpl extends AuthenticationRemoteDataSource {
 | 
				
			|||||||
    return Future.value(mock);
 | 
					    return Future.value(mock);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  Future<Account> signInWithGoogle() async {
 | 
				
			||||||
 | 
					    await _randomDelay();
 | 
				
			||||||
 | 
					    final creation = DateTime.now();
 | 
				
			||||||
 | 
					    final mock = AccountModel(
 | 
				
			||||||
 | 
					      uid: 'mock-id-google',
 | 
				
			||||||
 | 
					      emailVerified: true,
 | 
				
			||||||
 | 
					      isAnonymous: false,
 | 
				
			||||||
 | 
					      providerId: 'google',
 | 
				
			||||||
 | 
					      creationTime: creation,
 | 
				
			||||||
 | 
					      lastSignInTime: creation,
 | 
				
			||||||
 | 
					      isNewUser: creation == creation,
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    _streamAccount.add(mock);
 | 
				
			||||||
 | 
					    _connectedMock = _connectedMock?.copyWith(left: mock);
 | 
				
			||||||
 | 
					    _lastSignInTime = DateTime.now();
 | 
				
			||||||
 | 
					    return Future.value(mock);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Future<Account> signInWithEmailAndPassword({
 | 
					  Future<Account> signInWithEmailAndPassword({
 | 
				
			||||||
    required String email,
 | 
					    required String email,
 | 
				
			||||||
 | 
				
			|||||||
@ -295,6 +295,17 @@ class AuthenticationRepositoryImpl<T extends Object>
 | 
				
			|||||||
        (error) => error,
 | 
					        (error) => error,
 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  FutureOrResult<Account> signInWithGoogle() =>
 | 
				
			||||||
 | 
					      Result.tryCatchAsync<Account, AppException, AppException>(
 | 
				
			||||||
 | 
					        () async {
 | 
				
			||||||
 | 
					          final account =
 | 
				
			||||||
 | 
					              await _authenticationRemoteDataSource.signInWithGoogle();
 | 
				
			||||||
 | 
					          return account;
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        (error) => error,
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  FutureOrResult<bool> verifyPasswordResetCode({required String code}) =>
 | 
					  FutureOrResult<bool> verifyPasswordResetCode({required String code}) =>
 | 
				
			||||||
      Result.tryCatchAsync<bool, AppException, AppException>(
 | 
					      Result.tryCatchAsync<bool, AppException, AppException>(
 | 
				
			||||||
 | 
				
			|||||||
@ -49,6 +49,8 @@ abstract class AuthenticationRemoteDataSource extends BaseRemoteDataSource {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  Future<Account> signInAnonymously();
 | 
					  Future<Account> signInAnonymously();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<Account> signInWithGoogle();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<Account> updateEmail({required String email});
 | 
					  Future<Account> updateEmail({required String email});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<Account> updatePassword({required String password});
 | 
					  Future<Account> updatePassword({required String password});
 | 
				
			||||||
 | 
				
			|||||||
@ -73,6 +73,13 @@ abstract class AuthenticationRepository<T> extends BaseRepository {
 | 
				
			|||||||
  /// {@endtemplate}
 | 
					  /// {@endtemplate}
 | 
				
			||||||
  FutureOrResult<Account> signInAnonymously();
 | 
					  FutureOrResult<Account> signInAnonymously();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// {@template signin_google}
 | 
				
			||||||
 | 
					  /// Starts the Sign In with Google Flow.
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  /// Throws a SignInWithGoogleFailureInterface if an exception occurs.
 | 
				
			||||||
 | 
					  /// {@endtemplate}
 | 
				
			||||||
 | 
					  FutureOrResult<Account> signInWithGoogle();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// {@template signin_pwd}
 | 
					  /// {@template signin_pwd}
 | 
				
			||||||
  /// Signs in with the provided [email] and [password].
 | 
					  /// Signs in with the provided [email] and [password].
 | 
				
			||||||
  ///
 | 
					  ///
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user