Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/firebase_auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.12.0

* Added new `AuthResult` and `AdditionalUserInfo` classes.
* **Breaking Change**. Sign-in methods now return `AuthResult` instead of `FirebaseUser`.
Retrieve the `FirebaseUser` using the `user` property of `AuthResult`.

## 0.11.1+12

* Update google-services Android gradle plugin to 4.3.0 in documentation and examples.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.firebase.FirebaseNetworkException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.auth.ActionCodeSettings;
import com.google.firebase.auth.AdditionalUserInfo;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.EmailAuthProvider;
Expand Down Expand Up @@ -669,9 +670,15 @@ public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful() || task.getResult() == null) {
reportException(result, task.getException());
} else {
FirebaseUser user = task.getResult().getUser();
Map<String, Object> userMap = Collections.unmodifiableMap(mapFromUser(user));
result.success(userMap);
AuthResult authResult = task.getResult();
FirebaseUser user = authResult.getUser();
AdditionalUserInfo additionalUserInfo = authResult.getAdditionalUserInfo();
Map<String, Object> userMap = (mapFromUser(user));
Map<String, Object> additionalUserInfoMap = mapFromAdditionalUserInfo(additionalUserInfo);
Map<String, Object> map = new HashMap<>();
map.put("user", userMap);
map.put("additionalUserInfo", additionalUserInfoMap);
result.success(Collections.unmodifiableMap(map));
}
}
}
Expand Down Expand Up @@ -752,6 +759,19 @@ private Map<String, Object> mapFromUser(FirebaseUser user) {
}
}

private Map<String, Object> mapFromAdditionalUserInfo(AdditionalUserInfo info) {
if (info != null) {
Map<String, Object> additionalUserInfoMap = new HashMap<>();
additionalUserInfoMap.put("profile", info.getProfile());
additionalUserInfoMap.put("providerId", info.getProviderId());
additionalUserInfoMap.put("username", info.getUsername());
additionalUserInfoMap.put("isNewUser", info.isNewUser());
return Collections.unmodifiableMap(additionalUserInfoMap);
} else {
return null;
}
}

private void markUserRequired(Result result) {
result.error("USER_REQUIRED", "Please authenticate with Firebase first", null);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/firebase_auth/example/lib/register_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ class RegisterPageState extends State<RegisterPage> {

// Example code for registration.
void _register() async {
final FirebaseUser user = await _auth.createUserWithEmailAndPassword(
final FirebaseUser user = (await _auth.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
))
.user;
if (user != null) {
setState(() {
_success = true;
Expand Down
27 changes: 17 additions & 10 deletions packages/firebase_auth/example/lib/signin_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ class _EmailPasswordFormState extends State<_EmailPasswordForm> {

// Example code of how to sign in with email and password.
void _signInWithEmailAndPassword() async {
final FirebaseUser user = await _auth.signInWithEmailAndPassword(
final FirebaseUser user = (await _auth.signInWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
))
.user;
if (user != null) {
setState(() {
_success = true;
Expand Down Expand Up @@ -193,10 +194,11 @@ class _EmailLinkSignInSectionState extends State<_EmailLinkSignInSection>
final Uri link = await _retrieveDynamicLink();

if (link != null) {
final FirebaseUser user = await _auth.signInWithEmailAndLink(
final FirebaseUser user = (await _auth.signInWithEmailAndLink(
email: _userEmail,
link: link.toString(),
);
))
.user;

if (user != null) {
_userID = user.uid;
Expand Down Expand Up @@ -330,7 +332,7 @@ class _AnonymouslySignInSectionState extends State<_AnonymouslySignInSection> {

// Example code of how to sign in anonymously.
void _signInAnonymously() async {
final FirebaseUser user = await _auth.signInAnonymously();
final FirebaseUser user = (await _auth.signInAnonymously()).user;
assert(user != null);
assert(user.isAnonymous);
assert(!user.isEmailVerified);
Expand Down Expand Up @@ -413,7 +415,8 @@ class _GoogleSignInSectionState extends State<_GoogleSignInSection> {
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
final FirebaseUser user =
(await _auth.signInWithCredential(credential)).user;
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
Expand Down Expand Up @@ -554,7 +557,8 @@ class _PhoneSignInSectionState extends State<_PhoneSignInSection> {
verificationId: _verificationId,
smsCode: _smsController.text,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
final FirebaseUser user =
(await _auth.signInWithCredential(credential)).user;
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
setState(() {
Expand Down Expand Up @@ -698,7 +702,8 @@ class _OtherProvidersSignInSectionState
final AuthCredential credential = GithubAuthProvider.getCredential(
token: _tokenController.text,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
final FirebaseUser user =
(await _auth.signInWithCredential(credential)).user;
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
Expand All @@ -720,7 +725,8 @@ class _OtherProvidersSignInSectionState
final AuthCredential credential = FacebookAuthProvider.getCredential(
accessToken: _tokenController.text,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
final FirebaseUser user =
(await _auth.signInWithCredential(credential)).user;
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
Expand All @@ -742,7 +748,8 @@ class _OtherProvidersSignInSectionState
final AuthCredential credential = TwitterAuthProvider.getCredential(
authToken: _tokenController.text,
authTokenSecret: _tokenSecretController.text);
final FirebaseUser user = await _auth.signInWithCredential(credential);
final FirebaseUser user =
(await _auth.signInWithCredential(credential)).user;
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_auth/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies:
firebase_auth:
path: ../
google_sign_in: ^4.0.0
firebase_core: ^0.4.0
firebase_core: ^0.4.0+8
firebase_dynamic_links: ^0.3.0

dev_dependencies:
Expand Down
8 changes: 7 additions & 1 deletion packages/firebase_auth/example/test/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ void main() {
final FirebaseAuth auth = FirebaseAuth.instance;

test('signInAnonymously', () async {
final FirebaseUser user = await auth.signInAnonymously();
final AuthResult result = await auth.signInAnonymously();
final FirebaseUser user = result.user;
expect(user.uid, isNotNull);
expect(user.isAnonymous, isTrue);
final AdditionalUserInfo additionalUserInfo = result.additionalUserInfo;
expect(additionalUserInfo.username, isNull);
expect(additionalUserInfo.isNewUser, isNotNull);
expect(additionalUserInfo.profile, isNull);
expect(additionalUserInfo.providerId, 'firebase');
});

test('isSignInWithEmailLink', () async {
Expand Down
43 changes: 33 additions & 10 deletions packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
} else if ([@"signInAnonymously" isEqualToString:call.method]) {
[[self getAuth:call.arguments]
signInAnonymouslyWithCompletion:^(FIRAuthDataResult *authResult, NSError *error) {
[self sendResult:result forUser:authResult.user error:error];
[self sendResult:result forAuthDataResult:authResult error:error];
}];
} else if ([@"signInWithCredential" isEqualToString:call.method]) {
[[self getAuth:call.arguments]
signInAndRetrieveDataWithCredential:[self getCredential:call.arguments]
completion:^(FIRAuthDataResult *authResult, NSError *error) {
[self sendResult:result forUser:authResult.user error:error];
[self sendResult:result
forAuthDataResult:authResult
error:error];
}];
} else if ([@"createUserWithEmailAndPassword" isEqualToString:call.method]) {
NSString *email = call.arguments[@"email"];
Expand All @@ -122,7 +124,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
createUserWithEmail:email
password:password
completion:^(FIRAuthDataResult *authResult, NSError *error) {
[self sendResult:result forUser:authResult.user error:error];
[self sendResult:result forAuthDataResult:authResult error:error];
}];
} else if ([@"fetchSignInMethodsForEmail" isEqualToString:call.method]) {
NSString *email = call.arguments[@"email"];
Expand Down Expand Up @@ -177,7 +179,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
signInWithEmail:email
link:link
completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) {
[self sendResult:result forUser:authResult.user error:error];
[self sendResult:result forAuthDataResult:authResult error:error];
}];
} else if ([@"signInWithEmailAndPassword" isEqualToString:call.method]) {
NSString *email = call.arguments[@"email"];
Expand All @@ -186,7 +188,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
signInWithEmail:email
password:password
completion:^(FIRAuthDataResult *authResult, NSError *error) {
[self sendResult:result forUser:authResult.user error:error];
[self sendResult:result forAuthDataResult:authResult error:error];
}];
} else if ([@"signOut" isEqualToString:call.method]) {
NSError *signOutError;
Expand Down Expand Up @@ -215,8 +217,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
} else if ([@"linkWithCredential" isEqualToString:call.method]) {
[[self getAuth:call.arguments].currentUser
linkAndRetrieveDataWithCredential:[self getCredential:call.arguments]
completion:^(FIRAuthDataResult *r, NSError *error) {
[self sendResult:result forUser:r.user error:error];
completion:^(FIRAuthDataResult *authResult, NSError *error) {
[self sendResult:result forAuthDataResult:authResult error:error];
}];
} else if ([@"unlinkFromProvider" isEqualToString:call.method]) {
NSString *provider = call.arguments[@"provider"];
Expand Down Expand Up @@ -266,7 +268,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[[self getAuth:call.arguments]
signInWithCustomToken:token
completion:^(FIRAuthDataResult *authResult, NSError *error) {
[self sendResult:result forUser:authResult.user error:error];
[self sendResult:result forAuthDataResult:authResult error:error];
}];

} else if ([@"startListeningAuthState" isEqualToString:call.method]) {
Expand Down Expand Up @@ -329,8 +331,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
verificationCode:smsCode];
[[self getAuth:call.arguments]
signInAndRetrieveDataWithCredential:credential
completion:^(FIRAuthDataResult *r, NSError *_Nullable error) {
[self sendResult:result forUser:r.user error:error];
completion:^(FIRAuthDataResult *authResult,
NSError *_Nullable error) {
[self sendResult:result
forAuthDataResult:authResult
error:error];
}];
} else if ([@"setLanguageCode" isEqualToString:call.method]) {
NSString *language = call.arguments[@"language"];
Expand Down Expand Up @@ -361,6 +366,24 @@ - (NSMutableDictionary *)dictionaryFromUser:(FIRUser *)user {
}
#pragma clang diagnostic pop

- (void)sendResult:(FlutterResult)result
forAuthDataResult:(FIRAuthDataResult *)authResult
error:(NSError *)error {
FIRUser *user = result.user;
FIRAdditionalUserInfo *additionalUserInfo = result.additionalUserInfo;
[self sendResult:authResult
forObject:@{
@"user" : (user != nil ? [self dictionaryFromUser:user] : nil),
@"additionalUserInfo" : @{
@"isNewUser" : [NSNumber numberWithBool:additionalUserInfo.isNewUser],
@"username" : additionalUserInfo.username,
@"providerId" : additionalUserInfo.providerId,
@"profile" : additionalUserInfo.profile,
}
}
error:error];
}

- (void)sendResult:(FlutterResult)result forUser:(FIRUser *)user error:(NSError *)error {
[self sendResult:result
forObject:(user != nil ? [self dictionaryFromUser:user] : nil)
Expand Down
2 changes: 2 additions & 0 deletions packages/firebase_auth/lib/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ part 'src/auth_provider/github_auth_provider.dart';
part 'src/auth_provider/google_auth_provider.dart';
part 'src/auth_provider/phone_auth_provider.dart';
part 'src/auth_provider/twitter_auth_provider.dart';
part 'src/additional_user_info.dart';
part 'src/auth_credential.dart';
part 'src/auth_exception.dart';
part 'src/auth_result.dart';
part 'src/firebase_auth.dart';
part 'src/firebase_user.dart';
part 'src/user_info.dart';
Expand Down
26 changes: 26 additions & 0 deletions packages/firebase_auth/lib/src/additional_user_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of firebase_auth;

/// Interface representing a user's additional information
class AdditionalUserInfo {
AdditionalUserInfo._(this._data);

final Map<dynamic, dynamic> _data;

/// Returns whether the user is new or existing
bool get isNewUser => _data['isNewUser'];

/// Returns the username if the provider is GitHub or Twitter
String get username => _data['username'];

/// Returns the provider ID for specifying which provider the
/// information in [profile] is for.
String get providerId => _data['providerId'];

/// Returns a Map containing IDP-specific user data if the provider
/// is one of Facebook, GitHub, Google, Twitter, Microsoft, or Yahoo.
Map<String, dynamic> get profile => _data['profile'];
}
29 changes: 29 additions & 0 deletions packages/firebase_auth/lib/src/auth_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of firebase_auth;

/// Result object obtained from operations that can affect the authentication
/// state. Contains a method that returns the currently signed-in user after
/// the operation has completed.
class AuthResult {
AuthResult._(this._data, FirebaseApp app)
: user = FirebaseUser._(_data['user'].cast<String, dynamic>(), app);

final Map<String, dynamic> _data;

/// Returns the currently signed-in [FirebaseUser], or `null` if there isn't
/// any (i.e. the user is signed out).
final FirebaseUser user;

/// Returns IDP-specific information for the user if the provider is one of
/// Facebook, Github, Google, or Twitter.
AdditionalUserInfo get additionalUserInfo => _data['additionalUserInfo'] == null ? null :
AdditionalUserInfo._(_data['additionalUserInfo']);

@override
String toString() {
return '$runtimeType($_data)';
}
}
Loading