diff --git a/lib/screens/authentication_screen/authentication_screen.dart b/lib/screens/authentication_screen/authentication_screen.dart index a85e522..9c76053 100644 --- a/lib/screens/authentication_screen/authentication_screen.dart +++ b/lib/screens/authentication_screen/authentication_screen.dart @@ -2,8 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:smart_link/common/standard_app_widgets.dart'; import 'package:sign_button/sign_button.dart'; -import '../../config/index.dart'; -import '../authentication_screen/cubit/authentication_screen_cubit.dart'; +import 'package:smart_link/config/config.dart'; +import 'package:smart_link/screens/authentication_screen/cubit/authentication_screen_cubit.dart'; class AuthenticationScreen extends StatelessWidget with StandardAppWidgets { const AuthenticationScreen({super.key}); @@ -62,7 +62,7 @@ class AuthenticationScreen extends StatelessWidget with StandardAppWidgets { case Authenticated(): Navigator.pushNamedAndRemoveUntil( context, - Routes.bluetoothHome, + AppRoutes.bluetoothHome, (route) => false, ); showSnackBarWidget(context, state.message); diff --git a/lib/screens/authentication_screen/cubit/authentication_screen_cubit.dart b/lib/screens/authentication_screen/cubit/authentication_screen_cubit.dart index 04d0dc3..3c4c5f0 100644 --- a/lib/screens/authentication_screen/cubit/authentication_screen_cubit.dart +++ b/lib/screens/authentication_screen/cubit/authentication_screen_cubit.dart @@ -1,28 +1,22 @@ import 'package:bloc/bloc.dart'; -import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:equatable/equatable.dart'; import 'package:firebase_auth/firebase_auth.dart'; -import '../../../services/auth_service.dart'; -import '../../../config/index.dart'; +import 'package:smart_link/services/services.dart'; +import 'package:smart_link/config/config.dart'; part 'authentication_screen_state.dart'; class AuthenticationScreenCubit extends Cubit { - final Connectivity internetConnectivity; - final AuthenticationService authService; + final IConnectivityService connectivityService; + final GoogleAuthService authService; AuthenticationScreenCubit({ required this.authService, - required this.internetConnectivity, + required this.connectivityService, }) : super(Initial()); - Future _isInternetAvailable() async { - var connectivityResult = await internetConnectivity.checkConnectivity(); - return (connectivityResult != ConnectivityResult.none); - } - Future signIn() async { - if (!await _isInternetAvailable()) { + if (await connectivityService.isOffline()) { emit(NoInternet(message: AppStrings.noInternet)); emit(Initial()); return; @@ -47,6 +41,7 @@ class AuthenticationScreenCubit extends Cubit { default: emit(Initial()); + break; } } } diff --git a/lib/services/auth_service.dart b/lib/services/auth_service.dart index 195ee1e..0e7c6e5 100644 --- a/lib/services/auth_service.dart +++ b/lib/services/auth_service.dart @@ -5,16 +5,26 @@ import 'package:google_sign_in/google_sign_in.dart'; enum SignInState { authenticated, disabled, + error, } abstract interface class IAuthenticationService { Future signIn(); } -class AuthenticationService implements IAuthenticationService { - final FirebaseAuth _auth = FirebaseAuth.instance; +class GoogleAuthService implements IAuthenticationService { + final FirebaseAuth firebaseAuth; - User? get getCurrentUser => _auth.currentUser; + GoogleAuthService({required this.firebaseAuth}); + + User? get getCurrentUser => firebaseAuth.currentUser; + + OAuthCredential _getOAuthCredential(GoogleSignInAuthentication? auth) { + return GoogleAuthProvider.credential( + idToken: auth?.idToken, + accessToken: auth?.accessToken, + ); + } @override Future signIn() async { @@ -22,21 +32,18 @@ class AuthenticationService implements IAuthenticationService { GoogleSignIn googleSignIn = GoogleSignIn(); GoogleSignInAccount? user = await googleSignIn.signIn(); - if (user != null) { - GoogleSignInAuthentication auth = await user.authentication; + GoogleSignInAuthentication? auth = await user?.authentication; + AuthCredential credential = _getOAuthCredential(auth); - AuthCredential credential = GoogleAuthProvider.credential( - idToken: auth.idToken, - accessToken: auth.accessToken, - ); - - await _auth.signInWithCredential(credential); - } + await firebaseAuth.signInWithCredential(credential); } on FirebaseAuthException catch (e) { log(e.message!); if (e.code == "user-disabled") { return SignInState.disabled; } + } catch (e) { + log('SignStateError: ${e.toString()}'); + return SignInState.error; } return SignInState.authenticated; }