Skip to content

Commit

Permalink
refactor: handle and addSignInState.error enum flag for exceptions …
Browse files Browse the repository at this point in the history
…if authentication fails
  • Loading branch information
mediocre9 committed Jan 6, 2024
1 parent 966cca2 commit f8a7bc5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/screens/authentication_screen/authentication_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AuthenticationScreenState> {
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<bool> _isInternetAvailable() async {
var connectivityResult = await internetConnectivity.checkConnectivity();
return (connectivityResult != ConnectivityResult.none);
}

Future<void> signIn() async {
if (!await _isInternetAvailable()) {
if (await connectivityService.isOffline()) {
emit(NoInternet(message: AppStrings.noInternet));
emit(Initial());
return;
Expand All @@ -47,6 +41,7 @@ class AuthenticationScreenCubit extends Cubit<AuthenticationScreenState> {

default:
emit(Initial());
break;
}
}
}
31 changes: 19 additions & 12 deletions lib/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,45 @@ import 'package:google_sign_in/google_sign_in.dart';
enum SignInState {
authenticated,
disabled,
error,
}

abstract interface class IAuthenticationService {
Future<SignInState> 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<SignInState> signIn() async {
try {
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;
}
Expand Down

0 comments on commit f8a7bc5

Please sign in to comment.