-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathfirebase_auth_facade.dart
More file actions
278 lines (254 loc) · 8.2 KB
/
Copy pathfirebase_auth_facade.dart
File metadata and controls
278 lines (254 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_auth_oauth/firebase_auth_oauth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:meta/meta.dart';
import '../domain/auth/auth.dart';
import '../domain/auth/auth_failure.dart';
import '../domain/auth/exceptions.dart';
import '../domain/auth/i_auth_facade.dart';
import '../domain/auth/user.dart';
import '../domain/auth/value_objects.dart';
class FirebaseAuthFacade implements AuthFacade {
final FirebaseAuth _firebaseAuth;
final GoogleSignIn _googleSignIn;
final FirebaseApp _app;
final bool googleSignInEnabled;
FirebaseAuthFacade({
FirebaseAuth firebaseAuth,
GoogleSignIn googleSignIn,
FirebaseApp app,
this.googleSignInEnabled = false,
}) : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance,
_googleSignIn = googleSignIn ?? GoogleSignIn(),
_app = app ?? Firebase.app();
@override
LitUser getSignedInUser() => _mapUser(_firebaseAuth.currentUser);
@override
Stream<LitUser> get onAuthStateChanged {
return _firebaseAuth.authStateChanges().map(_mapUser);
}
LitUser _mapUser(User user) {
if (user == null) {
return const LitUser.empty();
}
return LitUser(user: user);
}
@override
Future<Auth> registerWithEmailAndPassword({
@required EmailAddress emailAddress,
@required Password password,
}) async {
final emailAddressStr = emailAddress.getOrCrash();
final passwordStr = password.getOrCrash();
if (kIsWeb) {
return _webRegisterWithEmailAndPassword(
email: emailAddressStr,
password: passwordStr,
);
} else {
return _registerWithEmailAndPassword(
email: emailAddressStr,
password: passwordStr,
);
}
}
Future<Auth> _webRegisterWithEmailAndPassword(
{String email, String password}) async {
try {
await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
return const Auth.success();
} catch (e) {
debugPrint(e.code);
switch (e.code) {
case "auth/email-already-in-use":
return const Auth.failure(AuthFailure.emailAlreadyInUse());
break;
default:
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
}
}
}
Future<Auth> _registerWithEmailAndPassword(
{String email, String password}) async {
try {
await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
return const Auth.success();
} on FirebaseAuthException catch (e) {
if (e.code == 'email-already-in-use') {
return const Auth.failure(AuthFailure.emailAlreadyInUse());
} else {
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
}
}
}
@override
Future<Auth> signInWithEmailAndPassword({
@required EmailAddress emailAddress,
@required Password password,
}) async {
final emailAddressStr = emailAddress.getOrCrash();
final passwordStr = password.getOrCrash();
try {
await _firebaseAuth.signInWithEmailAndPassword(
email: emailAddressStr,
password: passwordStr,
);
return const Auth.success();
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "wrong-password":
case "user-not-found":
return const Auth.failure(
AuthFailure.invalidEmailAndPasswordCombination(),
);
break;
case "invalid-email":
return const Auth.failure(AuthFailure.malformed());
break;
case "user-disabled":
return const Auth.failure(AuthFailure.userDisabled());
break;
case "too-many-requests":
return const Auth.failure(AuthFailure.tooManyRequests());
break;
default:
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
}
}
}
@override
Future<Auth> signInWithGoogle() async {
if (!googleSignInEnabled) {
throw AuthProviderNotEnabled('Google');
}
if (kIsWeb) {
return _webSignInWithGoogle();
} else {
return _signInWithGoogle();
}
}
Future<Auth> _webSignInWithGoogle() async {
// TODO investigate alternative solutions to handle these exceptions
// Will possibly be solved once the Firebase Auth rework is complete
// See: https://github.com/FirebaseExtended/flutterfire/issues/2582
GoogleSignInAccount googleUser;
try {
googleUser = await _googleSignIn.signIn();
} catch (e) {
if (e.toString().contains('appClientId != null')) {
debugPrint(e);
return const Auth.failure(AuthFailure.serverError());
}
return const Auth.failure(AuthFailure.cancelledByUser());
}
try {
final googleAuthentication = await googleUser.authentication;
final authCredential = GoogleAuthProvider.credential(
idToken: googleAuthentication.idToken,
accessToken: googleAuthentication.accessToken,
);
await _firebaseAuth.signInWithCredential(authCredential);
return const Auth.success();
} catch (e) {
debugPrint(e);
return const Auth.failure(AuthFailure.serverError());
}
}
Future<Auth> _signInWithGoogle() async {
try {
final googleUser = await _googleSignIn.signIn();
if (googleUser == null) {
return const Auth.failure(AuthFailure.cancelledByUser());
}
final googleAuthentication = await googleUser.authentication;
final authCredential = GoogleAuthProvider.credential(
idToken: googleAuthentication.idToken,
accessToken: googleAuthentication.accessToken,
);
await _firebaseAuth.signInWithCredential(authCredential);
return const Auth.success();
} on FirebaseAuthException catch (e) {
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
}
}
@override
Future<Auth> signInAnonymously() async {
try {
await _firebaseAuth.signInAnonymously();
return const Auth.success();
} on FirebaseAuthException catch (e) {
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
}
}
@override
Future<Auth> signInWithCredential(AuthCredential credential) async {
try {
await _firebaseAuth.signInWithCredential(credential);
return const Auth.success();
} on FirebaseAuthException catch (e) {
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
} catch (e) {
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError()); // todo improve
}
}
@override
Future<Auth> signInWithOAuth(String provider, List<String> scopes,
Map<String, String> parameters) async {
try {
await FirebaseAuthOAuth(app: _app)
.openSignInFlow(provider, scopes, parameters);
return const Auth.success();
} on PlatformException catch (e) {
/**
* The plugin has the following error codes:
* 1. FirebaseAuthError: FirebaseAuth related error
* 2. PlatformError: An platform related error
* 3. PluginError: An error from this plugin
*/
debugPrint("${e.code}: ${e.message}");
if (e.message == 'The interaction was cancelled by the user.') {
return const Auth.failure(AuthFailure.cancelledByUser());
}
return const Auth.failure(AuthFailure.serverError());
} catch (e) {
debugPrint(e.toString());
if (e.toString().contains('auth/popup-closed-by-user')) {
return const Auth.failure(AuthFailure.cancelledByUser());
}
debugPrint(e.toString());
return const Auth.failure(AuthFailure.serverError());
}
}
@override
Future<void> signOut() async {
return Future.wait([
_signOutGoogle(),
_firebaseAuth.signOut(),
]);
}
Future<void> _signOutGoogle() async {
if (!googleSignInEnabled) return;
try {
await _googleSignIn.signOut();
} catch (e) {
rethrow;
}
}
}