Skip to content

Commit

Permalink
Adding support for more Firebase methods (flutter#83)
Browse files Browse the repository at this point in the history
* Adding support for createUserWithEmailAndPassword, signInWithEmailAndPassword, and signOut

* Update CHANGELOG.md

* Update pubspec.yaml

* Fixing formatting of my changes
  • Loading branch information
killermonk authored and collinjackson committed May 27, 2017
1 parent 2bad1bb commit d54486e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
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.0.3

* Added support for createUserWithEmailAndPassword, signInWithEmailAndPassword, and signOut Firebase methods

## 0.0.2+1

* Updated README.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,18 @@ public void onMethodCall(MethodCall call, Result result) {
case "signInAnonymously":
handleSignInAnonymously(call, result);
break;
case "createUserWithEmailAndPassword":
handleCreateUserWithEmailAndPassword(call, result);
break;
case "signInWithEmailAndPassword":
handleSignInWithEmailAndPassword(call, result);
break;
case "signInWithGoogle":
handleSignInWithGoogle(call, result);
break;
case "signOut":
handleSignOut(call, result);
break;
default:
result.notImplemented();
break;
Expand All @@ -64,6 +73,28 @@ private void handleSignInAnonymously(MethodCall call, final Result result) {
.addOnCompleteListener(activity, new SignInCompleteListener(result));
}

private void handleCreateUserWithEmailAndPassword(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
String email = arguments.get("email");
String password = arguments.get("password");

firebaseAuth
.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(activity, new SignInCompleteListener(result));
}

private void handleSignInWithEmailAndPassword(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
String email = arguments.get("email");
String password = arguments.get("password");

firebaseAuth
.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(activity, new SignInCompleteListener(result));
}

private void handleSignInWithGoogle(MethodCall call, final Result result) {
@SuppressWarnings("unchecked")
Map<String, String> arguments = (Map<String, String>) call.arguments;
Expand All @@ -75,6 +106,11 @@ private void handleSignInWithGoogle(MethodCall call, final Result result) {
.addOnCompleteListener(activity, new SignInCompleteListener(result));
}

private void handleSignOut(MethodCall call, final Result result) {
firebaseAuth.signOut();
result.success(null);
}

private class SignInCompleteListener implements OnCompleteListener<AuthResult> {
private final Result result;

Expand Down
25 changes: 25 additions & 0 deletions packages/firebase_auth/ios/Classes/FirebaseAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
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"];
[[FIRAuth auth] createUserWithEmail:email
password:password
completion:^(FIRUser *user, NSError *error) {
[self sendResult:result forUser:user error:error];
}];
} else if ([@"signInWithEmailAndPassword" isEqualToString:call.method]) {
NSString *email = call.arguments[@"email"];
NSString *password = call.arguments[@"password"];
[[FIRAuth auth] signInWithEmail:email
password:password
completion:^(FIRUser *user, NSError *error) {
[self sendResult:result forUser:user error:error];
}];
} else if ([@"signOut" isEqualToString:call.method]) {
NSError *signOutError;
BOOL status = [[FIRAuth auth] signOut:&signOutError];
if (!status) {
NSLog(@"Error signing out: %@", signOutError);
[self sendResult:result forUser:nil error:signOutError];
} else {
[self sendResult:result forUser:nil error:nil];
}
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
39 changes: 39 additions & 0 deletions packages/firebase_auth/lib/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,40 @@ class FirebaseAuth {
return _currentUser;
}

Future<FirebaseUser> createUserWithEmailAndPassword({
@required String email,
@required String password,
}) async {
assert(email != null);
assert(password != null);
Map<String, dynamic> data = await _channel.invokeMethod(
'createUserWithEmailAndPassword',
<String, String>{
'email': email,
'password': password,
},
);
_currentUser = new FirebaseUser._(data);
return _currentUser;
}

Future<FirebaseUser> signInWithEmailAndPassword({
@required String email,
@required String password,
}) async {
assert(email != null);
assert(password != null);
Map<String, dynamic> data = await _channel.invokeMethod(
'signInWithEmailAndPassword',
<String, String>{
'email': email,
'password': password,
},
);
_currentUser = new FirebaseUser._(data);
return _currentUser;
}

Future<FirebaseUser> signInWithGoogle({
@required String idToken,
@required String accessToken,
Expand All @@ -103,6 +137,11 @@ class FirebaseAuth {
return _currentUser;
}

Future<Null> signOut() async {
await _channel.invokeMethod("signOut");
_currentUser = null;
}

FirebaseUser _currentUser;

/// Synchronously gets the cached current user, or `null` if there is none.
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 @@ -3,7 +3,7 @@ name: firebase_auth
description: Firebase Auth plugin for Flutter.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_auth
version: 0.0.2+1
version: 0.0.3

flutter:
plugin:
Expand Down

0 comments on commit d54486e

Please sign in to comment.