From c725e54b847314c130658e77a36ce1445cb756d4 Mon Sep 17 00:00:00 2001 From: PJ Essien Date: Thu, 6 Sep 2018 13:40:53 +0100 Subject: [PATCH 1/5] Implement signing in with Github --- .../firebaseauth/FirebaseAuthPlugin.java | 11 +++++++++++ .../ios/Classes/FirebaseAuthPlugin.m | 8 ++++++++ packages/firebase_auth/lib/firebase_auth.dart | 12 ++++++++++++ .../test/firebase_auth_test.dart | 19 +++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java b/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java index 87f5c552f1f8..914e6d71b7e3 100755 --- a/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java +++ b/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java @@ -96,6 +96,9 @@ public void onMethodCall(MethodCall call, Result result) { case "signInWithTwitter": handleSignInWithTwitter(call, result); break; + case "signInWithGithub": + handleSignInWithGithub(call, result); + break; case "signOut": handleSignOut(call, result); break; @@ -391,6 +394,14 @@ private void handleSignInWithTwitter(MethodCall call, final Result result) { .addOnCompleteListener(new SignInCompleteListener(result)); } + private void handleSignInWithGithub(MethodCall call, final Result result) { + String token = call.argument("token"); + AuthCredential credential = GithubAuthProvider.getCredential(token); + firebaseAuth + .signInWithCredential(credential) + .addOnCompleteListener(new SignInCompleteListener(result)); + } + private void handleSignInWithCustomToken(MethodCall call, final Result result) { Map arguments = call.arguments(); String token = arguments.get("token"); diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m index a1452990d08f..e96be86e3a78 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m @@ -96,6 +96,14 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result completion:^(FIRUser *user, NSError *error) { [self sendResult:result forUser:user error:error]; }]; + } else if ([@"signInWithGithub" isEqualToString:call.method]) { + NSString *token = call.arguments[@"token"]; + FIRAuthCredential *credential = + [FIRGitHubAuthProvider credentialWithToken:token]; + [[FIRAuth auth] signInWithCredential:credential + completion:^(FIRUser *user, NSError *error) { + [self sendResult:result forUser:user error:error]; + }]; } else if ([@"createUserWithEmailAndPassword" isEqualToString:call.method]) { NSString *email = call.arguments[@"email"]; NSString *password = call.arguments[@"password"]; diff --git a/packages/firebase_auth/lib/firebase_auth.dart b/packages/firebase_auth/lib/firebase_auth.dart index 5574bc5ee1b9..a66c44b46325 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -261,6 +261,18 @@ class FirebaseAuth { return currentUser; } + Future signInWithGithub({ + @required String token + }) async { + assert(token != null); + final Map data = await channel.invokeMethod( + 'signInWithGithub', + { 'token': token } + ); + final FirebaseUser currentUser = new FirebaseUser._(data); + return currentUser; + } + Future signInWithGoogle({ @required String idToken, @required String accessToken, diff --git a/packages/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/test/firebase_auth_test.dart index 95bb3f23c525..f0acb629114f 100755 --- a/packages/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/test/firebase_auth_test.dart @@ -16,6 +16,7 @@ const String kMockEmail = 'test@example.com'; const String kMockPassword = 'passw0rd'; const String kMockIdToken = '12345'; const String kMockAccessToken = '67890'; +const String kMockGithubToken = 'github'; const String kMockCustomToken = '12345'; const String kMockPhoneNumber = '5555555555'; const String kMockVerificationId = '12345'; @@ -265,6 +266,24 @@ void main() { ); }); + test('signInWithGithub', () async { + final FirebaseUser user = await auth.signInWithGithub( + token: kMockGithubToken, + ); + verifyUser(user); + expect( + log, + [ + isMethodCall( + 'signInWithGithub', + arguments: { + 'token': kMockGithubToken, + }, + ), + ], + ); + }); + test('linkWithEmailAndPassword', () async { final FirebaseUser user = await auth.linkWithEmailAndPassword( email: kMockEmail, From 1498c36812adb3fca19a3e553c438c81b4b5e50e Mon Sep 17 00:00:00 2001 From: PJ Essien Date: Thu, 6 Sep 2018 13:58:11 +0100 Subject: [PATCH 2/5] Add linking Github and Twitter accounts to existing user --- .../firebaseauth/FirebaseAuthPlugin.java | 25 ++++++++ .../ios/Classes/FirebaseAuthPlugin.m | 19 +++++- packages/firebase_auth/lib/firebase_auth.dart | 29 +++++++++ .../test/firebase_auth_test.dart | 60 +++++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) diff --git a/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java b/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java index 914e6d71b7e3..bc737877f688 100755 --- a/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java +++ b/packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java @@ -114,6 +114,12 @@ public void onMethodCall(MethodCall call, Result result) { case "linkWithFacebookCredential": handleLinkWithFacebookCredential(call, result); break; + case "linkWithTwitterCredential": + handleLinkWithTwitterCredential(call, result); + break; + case "linkWithGithubCredential": + handleLinkWithGithubCredential(call, result); + break; case "updateProfile": handleUpdateProfile(call, result); break; @@ -375,6 +381,25 @@ private void handleLinkWithFacebookCredential(MethodCall call, final Result resu .addOnCompleteListener(new SignInCompleteListener(result)); } + private void handleLinkWithTwitterCredential(MethodCall call, final Result result) { + String authToken = call.argument("authToken"); + String authTokenSecret = call.argument("authTokenSecret"); + AuthCredential credential = TwitterAuthProvider.getCredential(authToken, authTokenSecret); + firebaseAuth + .getCurrentUser() + .linkWithCredential(credential) + .addOnCompleteListener(new SignInCompleteListener(result)); + } + + private void handleLinkWithGithubCredential(MethodCall call, final Result result) { + String token = call.argument("token"); + AuthCredential credential = GithubAuthProvider.getCredential(token); + firebaseAuth + .getCurrentUser() + .linkWithCredential(credential) + .addOnCompleteListener(new SignInCompleteListener(result)); + } + private void handleSignInWithFacebook(MethodCall call, final Result result) { @SuppressWarnings("unchecked") Map arguments = (Map) call.arguments; diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m index e96be86e3a78..836373218de3 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m @@ -98,8 +98,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result }]; } else if ([@"signInWithGithub" isEqualToString:call.method]) { NSString *token = call.arguments[@"token"]; - FIRAuthCredential *credential = - [FIRGitHubAuthProvider credentialWithToken:token]; + FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:token]; [[FIRAuth auth] signInWithCredential:credential completion:^(FIRUser *user, NSError *error) { [self sendResult:result forUser:user error:error]; @@ -184,6 +183,22 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result completion:^(FIRUser *user, NSError *error) { [self sendResult:result forUser:user error:error]; }]; + } else if ([@"linkWithTwitterCredential" isEqualToString:call.method]) { + NSString *authToken = call.arguments[@"authToken"]; + NSString *authTokenSecret = call.arguments[@"authTokenSecret"]; + FIRAuthCredential *credential = + [FIRTwitterAuthProvider credentialWithToken:authToken secret:authTokenSecret]; + [[FIRAuth auth].currentUser linkWithCredential:credential + completion:^(FIRUser *user, NSError *error) { + [self sendResult:result forUser:user error:error]; + }]; + } else if ([@"linkWithGithubCredential" isEqualToString:call.method]) { + NSString *token = call.arguments[@"token"]; + FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:token]; + [[FIRAuth auth].currentUser linkWithCredential:credential + completion:^(FIRUser *user, NSError *error) { + [self sendResult:result forUser:user error:error]; + }]; } else if ([@"updateProfile" isEqualToString:call.method]) { FIRUserProfileChangeRequest *changeRequest = [[FIRAuth auth].currentUser profileChangeRequest]; if (call.arguments[@"displayName"]) { diff --git a/packages/firebase_auth/lib/firebase_auth.dart b/packages/firebase_auth/lib/firebase_auth.dart index a66c44b46325..9dbeb55e504d 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -440,6 +440,35 @@ class FirebaseAuth { return currentUser; } + Future linkWithTwitterCredential({ + @required String authToken, + @required String authTokenSecret, + }) async { + assert(authToken != null); + assert(authTokenSecret != null); + final Map data = await channel.invokeMethod( + 'linkWithTwitterCredential', + { + 'authToken': authToken, + 'authTokenSecret': authTokenSecret, + }, + ); + final FirebaseUser currentUser = new FirebaseUser._(data); + return currentUser; + } + + Future linkWithGithubCredential({ + @required String token + }) async { + assert(token != null); + final Map data = await channel.invokeMethod( + 'linkWithGithubCredential', + { 'token': token } + ); + final FirebaseUser currentUser = new FirebaseUser._(data); + return currentUser; + } + /// Sets the user-facing language code for auth operations that can be /// internationalized, such as [sendEmailVerification]. This language /// code should follow the conventions defined by the IETF in BCP47. diff --git a/packages/firebase_auth/test/firebase_auth_test.dart b/packages/firebase_auth/test/firebase_auth_test.dart index f0acb629114f..f4cdda390501 100755 --- a/packages/firebase_auth/test/firebase_auth_test.dart +++ b/packages/firebase_auth/test/firebase_auth_test.dart @@ -16,6 +16,8 @@ const String kMockEmail = 'test@example.com'; const String kMockPassword = 'passw0rd'; const String kMockIdToken = '12345'; const String kMockAccessToken = '67890'; +const String kMockAuthToken = 'twitter123'; +const String kMockAuthSecret = 'twittersecret'; const String kMockGithubToken = 'github'; const String kMockCustomToken = '12345'; const String kMockPhoneNumber = '5555555555'; @@ -266,6 +268,64 @@ void main() { ); }); + test('linkWithTwitterCredential', () async { + final FirebaseUser user = await auth.linkWithTwitterCredential( + authToken: kMockAuthToken, + authTokenSecret: kMockAuthSecret, + ); + verifyUser(user); + expect( + log, + [ + isMethodCall( + 'linkWithTwitterCredential', + arguments: { + 'authToken': kMockAuthToken, + 'authTokenSecret': kMockAuthSecret, + }, + ), + ], + ); + }); + + test('signInWithTwitter', () async { + final FirebaseUser user = await auth.signInWithTwitter( + authToken: kMockAuthToken, + authTokenSecret: kMockAuthSecret, + ); + verifyUser(user); + expect( + log, + [ + isMethodCall( + 'signInWithTwitter', + arguments: { + 'authToken': kMockAuthToken, + 'authTokenSecret': kMockAuthSecret, + }, + ), + ], + ); + }); + + test('linkWithGithubCredential', () async { + final FirebaseUser user = await auth.linkWithGithubCredential( + token: kMockGithubToken, + ); + verifyUser(user); + expect( + log, + [ + isMethodCall( + 'linkWithGithubCredential', + arguments: { + 'token': kMockGithubToken, + }, + ), + ], + ); + }); + test('signInWithGithub', () async { final FirebaseUser user = await auth.signInWithGithub( token: kMockGithubToken, From a7d6daa6713e159a840d0475aad0d5a61fa0f872 Mon Sep 17 00:00:00 2001 From: PJ Essien Date: Thu, 6 Sep 2018 14:13:37 +0100 Subject: [PATCH 3/5] fix formatting --- .../ios/Classes/FirebaseAuthPlugin.h | 2 +- packages/firebase_auth/lib/firebase_auth.dart | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h index ba6e71f1236f..49061239c581 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h @@ -6,5 +6,5 @@ #import "Firebase/Firebase.h" -@interface FLTFirebaseAuthPlugin : NSObject +@interface FLTFirebaseAuthPlugin : NSObject @end diff --git a/packages/firebase_auth/lib/firebase_auth.dart b/packages/firebase_auth/lib/firebase_auth.dart index 9dbeb55e504d..63be426016a9 100755 --- a/packages/firebase_auth/lib/firebase_auth.dart +++ b/packages/firebase_auth/lib/firebase_auth.dart @@ -261,14 +261,10 @@ class FirebaseAuth { return currentUser; } - Future signInWithGithub({ - @required String token - }) async { + Future signInWithGithub({@required String token}) async { assert(token != null); - final Map data = await channel.invokeMethod( - 'signInWithGithub', - { 'token': token } - ); + final Map data = await channel + .invokeMethod('signInWithGithub', {'token': token}); final FirebaseUser currentUser = new FirebaseUser._(data); return currentUser; } @@ -457,14 +453,11 @@ class FirebaseAuth { return currentUser; } - Future linkWithGithubCredential({ - @required String token - }) async { + Future linkWithGithubCredential( + {@required String token}) async { assert(token != null); final Map data = await channel.invokeMethod( - 'linkWithGithubCredential', - { 'token': token } - ); + 'linkWithGithubCredential', {'token': token}); final FirebaseUser currentUser = new FirebaseUser._(data); return currentUser; } From 467ff10ba02db3b4d0aa67ec0f8f63f27183dcc9 Mon Sep 17 00:00:00 2001 From: PJ Essien Date: Thu, 6 Sep 2018 14:25:17 +0100 Subject: [PATCH 4/5] bump version --- packages/firebase_auth/CHANGELOG.md | 5 +++++ packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h | 2 +- packages/firebase_auth/pubspec.yaml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/firebase_auth/CHANGELOG.md b/packages/firebase_auth/CHANGELOG.md index b4f905b06d1e..53e86fa15145 100644 --- a/packages/firebase_auth/CHANGELOG.md +++ b/packages/firebase_auth/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.5.21 + +* Added support for Github signin and linking Github/Twitter accounts to + existing users + ## 0.5.20 * Replaced usages of guava's: ImmutableList and ImmutableMap with platform diff --git a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h index 49061239c581..ba6e71f1236f 100644 --- a/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h +++ b/packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.h @@ -6,5 +6,5 @@ #import "Firebase/Firebase.h" -@interface FLTFirebaseAuthPlugin : NSObject +@interface FLTFirebaseAuthPlugin : NSObject @end diff --git a/packages/firebase_auth/pubspec.yaml b/packages/firebase_auth/pubspec.yaml index 0d80a597032a..6a8797329415 100755 --- a/packages/firebase_auth/pubspec.yaml +++ b/packages/firebase_auth/pubspec.yaml @@ -4,7 +4,7 @@ description: Flutter plugin for Firebase Auth, enabling Android and iOS like Google, Facebook and Twitter. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_auth -version: 0.5.20 +version: 0.5.21 flutter: plugin: From 28e31b7b9a6830d93d28a23100abc30e4df46f70 Mon Sep 17 00:00:00 2001 From: Maurice Parrish Date: Wed, 7 Nov 2018 16:05:35 -0800 Subject: [PATCH 5/5] test commit