-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathemail_auth_provider.dart
77 lines (67 loc) · 2.27 KB
/
email_auth_provider.dart
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
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:firebase_auth/firebase_auth.dart' as fba;
import 'package:flutter/material.dart';
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
/// A listener of the [EmailAuthFlow] flow lifecycle.
abstract class EmailAuthListener extends AuthListener {}
/// {@template ui.auth.providers.email_auth_provider}
/// An [AuthProvider] that allows to authenticate using email and password.
/// {@endtemplate}
class EmailAuthProvider
extends AuthProvider<EmailAuthListener, fba.EmailAuthCredential> {
@override
late EmailAuthListener authListener;
@override
final providerId = 'password';
@override
bool supportsPlatform(TargetPlatform platform) => true;
/// Tries to create a new user account with the given [EmailAuthCredential].
void signUpWithCredential(fba.EmailAuthCredential credential) {
authListener.onBeforeSignIn();
auth
.createUserWithEmailAndPassword(
email: credential.email,
password: credential.password!,
)
.then(authListener.onSignedIn)
.catchError(authListener.onError);
}
/// Creates an [EmailAuthCredential] with the given [email] and [password] and
/// performs a corresponding [AuthAction].
void authenticate(
String email,
String password, [
AuthAction action = AuthAction.signIn,
]) {
final credential = fba.EmailAuthProvider.credential(
email: email,
password: password,
) as fba.EmailAuthCredential;
onCredentialReceived(credential, action);
}
@override
void onCredentialReceived(
fba.EmailAuthCredential credential,
AuthAction action,
) {
switch (action) {
case AuthAction.signIn:
signInWithCredential(credential);
break;
case AuthAction.signUp:
if (shouldUpgradeAnonymous) {
return linkWithCredential(credential);
}
signUpWithCredential(credential);
break;
case AuthAction.link:
linkWithCredential(credential);
break;
case AuthAction.none:
super.onCredentialReceived(credential, action);
break;
}
}
}