Skip to content

Commit eabf8af

Browse files
filleduchaosandreidiaconu
authored andcommitted
Support sign-in with Github and linking Github accounts to existing users (flutter#768)
1 parent d415076 commit eabf8af

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

packages/firebase_auth/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.4
2+
3+
* Added support for Github signin and linking Github accounts to existing users.
4+
15
## 0.6.3
26

37
* Add multi app support.

packages/firebase_auth/android/src/main/java/io/flutter/plugins/firebaseauth/FirebaseAuthPlugin.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public void onMethodCall(MethodCall call, Result result) {
101101
case "signInWithTwitter":
102102
handleSignInWithTwitter(call, result, getAuth(call));
103103
break;
104+
case "signInWithGithub":
105+
handleSignInWithGithub(call, result, getAuth(call));
106+
break;
104107
case "signOut":
105108
handleSignOut(call, result, getAuth(call));
106109
break;
@@ -119,6 +122,9 @@ public void onMethodCall(MethodCall call, Result result) {
119122
case "linkWithTwitterCredential":
120123
handleLinkWithTwitterCredential(call, result, getAuth(call));
121124
break;
125+
case "linkWithGithubCredential":
126+
handleLinkWithGithubCredential(call, result, getAuth(call));
127+
break;
122128
case "updateEmail":
123129
handleUpdateEmail(call, result, getAuth(call));
124130
break;
@@ -404,6 +410,16 @@ private void handleLinkWithTwitterCredential(
404410
.addOnCompleteListener(new SignInCompleteListener(result));
405411
}
406412

413+
private void handleLinkWithGithubCredential(
414+
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
415+
String token = call.argument("token");
416+
AuthCredential credential = GithubAuthProvider.getCredential(token);
417+
firebaseAuth
418+
.getCurrentUser()
419+
.linkWithCredential(credential)
420+
.addOnCompleteListener(new SignInCompleteListener(result));
421+
}
422+
407423
private void handleSignInWithFacebook(
408424
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
409425
@SuppressWarnings("unchecked")
@@ -425,6 +441,15 @@ private void handleSignInWithTwitter(
425441
.addOnCompleteListener(new SignInCompleteListener(result));
426442
}
427443

444+
private void handleSignInWithGithub(
445+
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
446+
String token = call.argument("token");
447+
AuthCredential credential = GithubAuthProvider.getCredential(token);
448+
firebaseAuth
449+
.signInWithCredential(credential)
450+
.addOnCompleteListener(new SignInCompleteListener(result));
451+
}
452+
428453
private void handleSignInWithCustomToken(
429454
MethodCall call, final Result result, FirebaseAuth firebaseAuth) {
430455
Map<String, String> arguments = call.arguments();

packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
101101
completion:^(FIRUser *user, NSError *error) {
102102
[self sendResult:result forUser:user error:error];
103103
}];
104+
} else if ([@"signInWithGithub" isEqualToString:call.method]) {
105+
NSString *token = call.arguments[@"token"];
106+
FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:token];
107+
[[self getAuth:call.arguments] signInWithCredential:credential
108+
completion:^(FIRUser *user, NSError *error) {
109+
[self sendResult:result forUser:user error:error];
110+
}];
104111
} else if ([@"createUserWithEmailAndPassword" isEqualToString:call.method]) {
105112
NSString *email = call.arguments[@"email"];
106113
NSString *password = call.arguments[@"password"];
@@ -199,6 +206,14 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
199206
completion:^(FIRUser *user, NSError *error) {
200207
[self sendResult:result forUser:user error:error];
201208
}];
209+
} else if ([@"linkWithGithubCredential" isEqualToString:call.method]) {
210+
NSString *token = call.arguments[@"token"];
211+
FIRAuthCredential *credential = [FIRGitHubAuthProvider credentialWithToken:token];
212+
[[self getAuth:call.arguments].currentUser
213+
linkWithCredential:credential
214+
completion:^(FIRUser *user, NSError *error) {
215+
[self sendResult:result forUser:user error:error];
216+
}];
202217
} else if ([@"updateEmail" isEqualToString:call.method]) {
203218
NSString *email = call.arguments[@"email"];
204219
[[self getAuth:call.arguments].currentUser updateEmail:email

packages/firebase_auth/lib/firebase_auth.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ class FirebaseAuth {
308308
return currentUser;
309309
}
310310

311+
Future<FirebaseUser> signInWithGithub({@required String token}) async {
312+
assert(token != null);
313+
final Map<dynamic, dynamic> data =
314+
await channel.invokeMethod('signInWithGithub', <String, String>{
315+
'token': token,
316+
'app': app.name,
317+
});
318+
final FirebaseUser currentUser = FirebaseUser._(data, app);
319+
return currentUser;
320+
}
321+
311322
Future<FirebaseUser> signInWithGoogle({
312323
@required String idToken,
313324
@required String accessToken,
@@ -469,6 +480,16 @@ class FirebaseAuth {
469480
return currentUser;
470481
}
471482

483+
Future<FirebaseUser> linkWithGithubCredential(
484+
{@required String token}) async {
485+
assert(token != null);
486+
final Map<dynamic, dynamic> data = await channel.invokeMethod(
487+
'linkWithGithubCredential',
488+
<String, String>{'app': app.name, 'token': token});
489+
final FirebaseUser currentUser = FirebaseUser._(data, app);
490+
return currentUser;
491+
}
492+
472493
/// Sets the user-facing language code for auth operations that can be
473494
/// internationalized, such as [sendEmailVerification]. This language
474495
/// code should follow the conventions defined by the IETF in BCP47.

packages/firebase_auth/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Flutter plugin for Firebase Auth, enabling Android and iOS
44
like Google, Facebook and Twitter.
55
author: Flutter Team <[email protected]>
66
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_auth
7-
version: 0.6.3
7+
version: 0.6.4
88

99
flutter:
1010
plugin:

packages/firebase_auth/test/firebase_auth_test.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const String kMockEmail = '[email protected]';
1717
const String kMockPassword = 'passw0rd';
1818
const String kMockIdToken = '12345';
1919
const String kMockAccessToken = '67890';
20+
const String kMockGithubToken = 'github';
2021
const String kMockAuthToken = '23456';
2122
const String kMockAuthTokenSecret = '78901';
2223
const String kMockCustomToken = '12345';
@@ -306,6 +307,86 @@ void main() {
306307
);
307308
});
308309

310+
test('linkWithTwitterCredential', () async {
311+
final FirebaseUser user = await auth.linkWithTwitterCredential(
312+
authToken: kMockAuthToken,
313+
authTokenSecret: kMockAuthTokenSecret,
314+
);
315+
verifyUser(user);
316+
expect(
317+
log,
318+
<Matcher>[
319+
isMethodCall(
320+
'linkWithTwitterCredential',
321+
arguments: <String, String>{
322+
'app': auth.app.name,
323+
'authToken': kMockAuthToken,
324+
'authTokenSecret': kMockAuthTokenSecret,
325+
},
326+
),
327+
],
328+
);
329+
});
330+
331+
test('signInWithTwitter', () async {
332+
final FirebaseUser user = await auth.signInWithTwitter(
333+
authToken: kMockAuthToken,
334+
authTokenSecret: kMockAuthTokenSecret,
335+
);
336+
verifyUser(user);
337+
expect(
338+
log,
339+
<Matcher>[
340+
isMethodCall(
341+
'signInWithTwitter',
342+
arguments: <String, String>{
343+
'app': auth.app.name,
344+
'authToken': kMockAuthToken,
345+
'authTokenSecret': kMockAuthTokenSecret,
346+
},
347+
),
348+
],
349+
);
350+
});
351+
352+
test('linkWithGithubCredential', () async {
353+
final FirebaseUser user = await auth.linkWithGithubCredential(
354+
token: kMockGithubToken,
355+
);
356+
verifyUser(user);
357+
expect(
358+
log,
359+
<Matcher>[
360+
isMethodCall(
361+
'linkWithGithubCredential',
362+
arguments: <String, String>{
363+
'app': auth.app.name,
364+
'token': kMockGithubToken,
365+
},
366+
),
367+
],
368+
);
369+
});
370+
371+
test('signInWithGithub', () async {
372+
final FirebaseUser user = await auth.signInWithGithub(
373+
token: kMockGithubToken,
374+
);
375+
verifyUser(user);
376+
expect(
377+
log,
378+
<Matcher>[
379+
isMethodCall(
380+
'signInWithGithub',
381+
arguments: <String, String>{
382+
'app': auth.app.name,
383+
'token': kMockGithubToken,
384+
},
385+
),
386+
],
387+
);
388+
});
389+
309390
test('linkWithEmailAndPassword', () async {
310391
final FirebaseUser user = await auth.linkWithEmailAndPassword(
311392
email: kMockEmail,

0 commit comments

Comments
 (0)