Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Support sign-in with Github and linking Github accounts to existing users #768

Merged
merged 6 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions packages/firebase_auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.4

* Added support for Github signin and linking Github accounts to existing users.

## 0.6.3

* Add multi app support.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public void onMethodCall(MethodCall call, Result result) {
case "signInWithTwitter":
handleSignInWithTwitter(call, result, getAuth(call));
break;
case "signInWithGithub":
handleSignInWithGithub(call, result, getAuth(call));
break;
case "signOut":
handleSignOut(call, result, getAuth(call));
break;
Expand All @@ -119,6 +122,9 @@ public void onMethodCall(MethodCall call, Result result) {
case "linkWithTwitterCredential":
handleLinkWithTwitterCredential(call, result, getAuth(call));
break;
case "linkWithGithubCredential":
handleLinkWithGithubCredential(call, result, getAuth(call));
break;
case "updateEmail":
handleUpdateEmail(call, result, getAuth(call));
break;
Expand Down Expand Up @@ -404,6 +410,16 @@ private void handleLinkWithTwitterCredential(
.addOnCompleteListener(new SignInCompleteListener(result));
}

private void handleLinkWithGithubCredential(
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
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, FirebaseAuth firebaseAuth) {
@SuppressWarnings("unchecked")
Expand All @@ -425,6 +441,15 @@ private void handleSignInWithTwitter(
.addOnCompleteListener(new SignInCompleteListener(result));
}

private void handleSignInWithGithub(
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
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, FirebaseAuth firebaseAuth) {
Map<String, String> arguments = call.arguments();
Expand Down
15 changes: 15 additions & 0 deletions packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ - (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];
[[self getAuth:call.arguments] 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"];
Expand Down Expand Up @@ -199,6 +206,14 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
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];
[[self getAuth:call.arguments].currentUser
linkWithCredential:credential
completion:^(FIRUser *user, NSError *error) {
[self sendResult:result forUser:user error:error];
}];
} else if ([@"updateEmail" isEqualToString:call.method]) {
NSString *email = call.arguments[@"email"];
[[self getAuth:call.arguments].currentUser updateEmail:email
Expand Down
21 changes: 21 additions & 0 deletions packages/firebase_auth/lib/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ class FirebaseAuth {
return currentUser;
}

Future<FirebaseUser> signInWithGithub({@required String token}) async {
assert(token != null);
final Map<dynamic, dynamic> data =
await channel.invokeMethod('signInWithGithub', <String, String>{
'token': token,
'app': app.name,
});
final FirebaseUser currentUser = FirebaseUser._(data, app);
return currentUser;
}

Future<FirebaseUser> signInWithGoogle({
@required String idToken,
@required String accessToken,
Expand Down Expand Up @@ -469,6 +480,16 @@ class FirebaseAuth {
return currentUser;
}

Future<FirebaseUser> linkWithGithubCredential(
{@required String token}) async {
assert(token != null);
final Map<dynamic, dynamic> data = await channel.invokeMethod(
'linkWithGithubCredential',
<String, String>{'app': app.name, 'token': token});
final FirebaseUser currentUser = FirebaseUser._(data, app);
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.
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: Flutter plugin for Firebase Auth, enabling Android and iOS
like Google, Facebook and Twitter.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_auth
version: 0.6.3
version: 0.6.4

flutter:
plugin:
Expand Down
81 changes: 81 additions & 0 deletions packages/firebase_auth/test/firebase_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 kMockAuthToken = '23456';
const String kMockAuthTokenSecret = '78901';
const String kMockCustomToken = '12345';
Expand Down Expand Up @@ -306,6 +307,86 @@ void main() {
);
});

test('linkWithTwitterCredential', () async {
final FirebaseUser user = await auth.linkWithTwitterCredential(
authToken: kMockAuthToken,
authTokenSecret: kMockAuthTokenSecret,
);
verifyUser(user);
expect(
log,
<Matcher>[
isMethodCall(
'linkWithTwitterCredential',
arguments: <String, String>{
'app': auth.app.name,
'authToken': kMockAuthToken,
'authTokenSecret': kMockAuthTokenSecret,
},
),
],
);
});

test('signInWithTwitter', () async {
final FirebaseUser user = await auth.signInWithTwitter(
authToken: kMockAuthToken,
authTokenSecret: kMockAuthTokenSecret,
);
verifyUser(user);
expect(
log,
<Matcher>[
isMethodCall(
'signInWithTwitter',
arguments: <String, String>{
'app': auth.app.name,
'authToken': kMockAuthToken,
'authTokenSecret': kMockAuthTokenSecret,
},
),
],
);
});

test('linkWithGithubCredential', () async {
final FirebaseUser user = await auth.linkWithGithubCredential(
token: kMockGithubToken,
);
verifyUser(user);
expect(
log,
<Matcher>[
isMethodCall(
'linkWithGithubCredential',
arguments: <String, String>{
'app': auth.app.name,
'token': kMockGithubToken,
},
),
],
);
});

test('signInWithGithub', () async {
final FirebaseUser user = await auth.signInWithGithub(
token: kMockGithubToken,
);
verifyUser(user);
expect(
log,
<Matcher>[
isMethodCall(
'signInWithGithub',
arguments: <String, String>{
'app': auth.app.name,
'token': kMockGithubToken,
},
),
],
);
});

test('linkWithEmailAndPassword', () async {
final FirebaseUser user = await auth.linkWithEmailAndPassword(
email: kMockEmail,
Expand Down