-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
exception.dart
145 lines (127 loc) 路 4.67 KB
/
exception.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
140
141
142
143
144
145
// ignore_for_file: require_trailing_commas
// Copyright 2020, 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:collection/collection.dart';
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/method_channel_multi_factor.dart';
import 'package:firebase_auth_platform_interface/src/method_channel/utils/pigeon_helper.dart';
import 'package:firebase_auth_platform_interface/src/pigeon/messages.pigeon.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/services.dart';
/// Catches a [PlatformException] and converts it into a [FirebaseAuthException]
/// if it was intentionally caught on the native platform.
Never convertPlatformException(
Object exception,
StackTrace stackTrace, {
bool fromPigeon = false,
}) {
if (exception is! PlatformException) {
Error.throwWithStackTrace(exception, stackTrace);
}
Error.throwWithStackTrace(
platformExceptionToFirebaseAuthException(exception, fromPigeon: fromPigeon),
stackTrace,
);
}
/// Converts a [PlatformException] into a [FirebaseAuthException].
///
/// A [PlatformException] can only be converted to a [FirebaseAuthException] if
/// the `details` of the exception exist. Firebase returns specific codes and
/// messages which can be converted into user friendly exceptions.
// TODO(rousselGit): Should this return a FirebaseAuthException to avoid having to cast?
FirebaseException platformExceptionToFirebaseAuthException(
PlatformException platformException, {
bool fromPigeon = false,
}) {
if (fromPigeon) {
return FirebaseAuthException(
code: platformException.code,
// Remove leading classname from message
message: platformException.message?.split(': ').last,
);
}
Map<String, dynamic>? details = platformException.details != null
? Map<String, dynamic>.from(platformException.details)
: null;
String code = 'unknown';
String? message = platformException.message;
String? email;
AuthCredential? credential;
if (details != null) {
code = details['code'] ?? code;
if (code == 'second-factor-required') {
return parseMultiFactorError(details);
}
message = details['message'] ?? message;
if (details['additionalData'] != null) {
if (details['additionalData']['authCredential'] != null) {
credential = AuthCredential(
providerId: details['additionalData']['authCredential']['providerId'],
signInMethod: details['additionalData']['authCredential']
['signInMethod'],
token: details['additionalData']['authCredential']['token'],
);
}
if (details['additionalData']['email'] != null) {
email = details['additionalData']['email'];
}
}
}
return FirebaseAuthException(
code: code,
message: message,
email: email,
credential: credential,
);
}
FirebaseAuthMultiFactorExceptionPlatform parseMultiFactorError(
Map<String, Object?> details) {
final code = details['code'] as String?;
final message = details['message'] as String?;
final additionalData = details['additionalData'] as Map<Object?, Object?>?;
if (additionalData == null) {
throw FirebaseAuthException(
code: "Can't parse multi factor error",
message: message,
);
}
final pigeonMultiFactorInfo =
(additionalData['multiFactorHints'] as List<Object?>? ?? [])
.whereNotNull()
.map(
PigeonMultiFactorInfo.decode,
)
.toList();
final multiFactorInfo = multiFactorInfoPigeonToObject(
pigeonMultiFactorInfo,
);
final auth = MethodChannelFirebaseAuth
.methodChannelFirebaseAuthInstances[additionalData['appName']];
if (auth == null) {
throw FirebaseAuthException(
code: code ?? 'Unknown',
message: message,
);
}
final sessionId = additionalData['multiFactorSessionId'] as String?;
final resolverId = additionalData['multiFactorResolverId'] as String?;
if (sessionId == null || resolverId == null) {
throw FirebaseAuthException(
code: "Can't parse multi factor error",
message: message,
);
}
final multiFactorResolver = MethodChannelMultiFactorResolver(
multiFactorInfo,
MultiFactorSession(sessionId),
resolverId,
auth,
);
return FirebaseAuthMultiFactorExceptionPlatform(
code: code ?? 'Unknown',
message: message,
resolver: multiFactorResolver,
);
}