-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathmfa.dart
139 lines (118 loc) · 3.63 KB
/
mfa.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
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
// 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 'dart:async';
import 'package:firebase_auth/firebase_auth.dart' as fba;
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:firebase_ui_auth/src/widgets/internal/universal_page_route.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
typedef SMSCodeInputScreenBuilder = Widget Function(
BuildContext context,
List<FirebaseUIAction> actions,
Object flowKey,
AuthAction action,
);
Future<fba.UserCredential?> startMFAVerification({
required BuildContext context,
required fba.MultiFactorResolver resolver,
fba.FirebaseAuth? auth,
SMSCodeInputScreenBuilder? smsCodeInputScreenBuilder,
void Function(FirebaseException e)? onError,
}) async {
if (resolver.hints.first is fba.PhoneMultiFactorInfo) {
return startPhoneMFAVerification(
context: context,
resolver: resolver,
auth: auth,
onError: onError,
);
} else {
throw Exception('Unsupported MFA type');
}
}
Future<fba.UserCredential?> startPhoneMFAVerification({
required BuildContext context,
required fba.MultiFactorResolver resolver,
fba.FirebaseAuth? auth,
SMSCodeInputScreenBuilder? smsCodeInputScreenBuilder,
void Function(FirebaseException e)? onError,
}) async {
final session = resolver.session;
final hint = resolver.hints.first;
var completer = Completer<fba.UserCredential?>();
final navigator = Navigator.of(context);
final provider = PhoneAuthProvider();
provider.auth = auth ?? fba.FirebaseAuth.instance;
final flow = PhoneAuthFlow(
auth: auth ?? fba.FirebaseAuth.instance,
action: AuthAction.none,
provider: PhoneAuthProvider(),
);
provider.authListener = flow;
completer.future.catchError((e) {
onError?.call(e as FirebaseException);
flow.onError(e);
return null;
});
final flowKey = Object();
final actions = [
AuthStateChangeAction<CredentialReceived>((context, inner) {
if (completer.isCompleted) {
completer = Completer<fba.UserCredential>();
completer.future.catchError((e) {
onError?.call(e as FirebaseException);
flow.onError(e);
return null;
});
}
final cred = inner.credential as fba.PhoneAuthCredential;
final assertion = fba.PhoneMultiFactorGenerator.getAssertion(cred);
try {
final cred = resolver.resolveSignIn(assertion);
completer.complete(cred);
} catch (e) {
completer.completeError(e);
}
}),
];
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
provider.sendVerificationCode(
hint: hint as fba.PhoneMultiFactorInfo,
multiFactorSession: session,
action: AuthAction.none,
);
});
Widget builder(BuildContext context) {
Widget child;
if (smsCodeInputScreenBuilder != null) {
child = smsCodeInputScreenBuilder.call(
context,
actions,
flowKey,
AuthAction.none,
);
} else {
child = SMSCodeInputScreen(
flowKey: flowKey,
action: AuthAction.none,
auth: auth,
actions: actions,
);
}
return AuthFlowBuilder<PhoneAuthController>(
flow: flow,
flowKey: flowKey,
child: child,
);
}
final pageRoute = createPageRoute(
context: context,
builder: builder,
);
navigator.push(pageRoute);
final cred = await completer.future;
navigator.pop();
return cred;
}