Skip to content

Commit 9ac7127

Browse files
feat: add sign in with credential
1 parent 4f08020 commit 9ac7127

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

lib/src/application/auth/sign_in_handler/sign_in_handler_state_notifier.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:dartz/dartz.dart';
2+
import 'package:firebase_auth/firebase_auth.dart';
23
import 'package:flutter/foundation.dart';
34
import 'package:state_notifier/state_notifier.dart';
45

@@ -55,6 +56,7 @@ class SignInHandlerStateNotifier extends StateNotifier<SignInHandlerState>
5556
isSubmitting: true,
5657
authFailureOrSuccessOption: none(),
5758
);
59+
5860
final auth = await _authFacade.signInWithGoogle();
5961

6062
if (mounted) {
@@ -65,6 +67,22 @@ class SignInHandlerStateNotifier extends StateNotifier<SignInHandlerState>
6567
}
6668
}
6769

70+
Future<void> signInWithCredential(AuthCredential credential) async {
71+
state = state.copyWith(
72+
isSubmitting: true,
73+
authFailureOrSuccessOption: none(),
74+
);
75+
76+
final auth = await _authFacade.signInWithCredential(credential);
77+
78+
if (mounted) {
79+
state = state.copyWith(
80+
isSubmitting: false,
81+
authFailureOrSuccessOption: some(auth),
82+
);
83+
}
84+
}
85+
6886
Future<void> signInAnonymously() async {
6987
if (!_authProviders.anonymous) {
7088
throw AuthProviderNotEnabled('Anonymous');

lib/src/domain/auth/i_auth_facade.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:firebase_auth/firebase_auth.dart';
12
import 'package:flutter/foundation.dart';
23

34
import 'auth.dart';
@@ -18,6 +19,7 @@ abstract class AuthFacade {
1819
@required Password password,
1920
});
2021
Future<Auth> signInWithGoogle();
22+
Future<Auth> signInWithCredential(AuthCredential credential);
2123
Future<Auth> signInAnonymously();
2224
Future<void> signOut();
2325
}

lib/src/infrastructure/firebase_auth_facade.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,16 @@ class FirebaseAuthFacade implements AuthFacade {
250250
}
251251
}
252252

253+
@override
254+
Future<Auth> signInWithCredential(AuthCredential credential) async {
255+
try {
256+
await _firebaseAuth.signInWithCredential(credential);
257+
return const Auth.success();
258+
} on PlatformException catch (_) {
259+
return const Auth.failure(AuthFailure.serverError());
260+
}
261+
}
262+
253263
@override
254264
Future<void> signOut() async {
255265
return Future.wait([

lib/src/presentation/core/extensions.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:firebase_auth/firebase_auth.dart';
12
import 'package:flutter/widgets.dart';
23
import 'package:provider/provider.dart';
34

@@ -40,6 +41,14 @@ extension AuthContext on BuildContext {
4041
.signInWithGoogle();
4142
}
4243

44+
/// Sign in with Credentials
45+
///
46+
/// Should only be used if your implementing your own third party sign-in
47+
Future<void> signInWithCredential(AuthCredential credential) async {
48+
Provider.of<SignInHandlerStateNotifier>(this, listen: false)
49+
.signInWithCredential(credential);
50+
}
51+
4352
/// Perform Firebase anonymous sign-in
4453
///
4554
/// Should only be used if you're creating your own custom sign-in form

0 commit comments

Comments
 (0)