Skip to content

Commit

Permalink
Add support for deleting user. (Closes #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0829 committed Mar 2, 2020
1 parent b978362 commit 5c01363
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ void logout() async {
// ...
}
```
##### Delete
```
void delete() async {
final result = await FirebaseAuthUi.instance().delete();
// ...
}
```

Plugin returns `FirebaseUser` with following details:

Expand All @@ -50,6 +57,7 @@ Field | Description |
phoneNumber | Phone number of user |
photoUri | URI of user's photo |
providerId | Indicates through which provider user was authenticated. |
isNewUser | Indicates if user is new |
metaData | Object of MetaData |

`MetaData`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ class FirebaseAuthUiPlugin(private val activity: Activity) : MethodCallHandler,
.addOnFailureListener {
result.success(false)
}
"delete" -> AuthUI.getInstance()
.delete(activity)
.addOnCompleteListener {
result.success(it.isSuccessful)
}
.addOnFailureListener {
result.success(false)
}
else -> result.notImplemented()
}
}
Expand Down
16 changes: 16 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class _MyAppState extends State<MyApp> {
),
),
_getErrorText(),
_user != null
? FlatButton(
child: Text('Delete'),
textColor: Colors.red,
onPressed: () => _deleteUser(),
)
: Container()
],
),
),
Expand Down Expand Up @@ -79,6 +86,15 @@ class _MyAppState extends State<MyApp> {
}
}

void _deleteUser() async {
final result = await FirebaseAuthUi.instance().delete();
if (result) {
setState(() {
_user = null;
});
}
}

void _onActionTapped() {
if (_user == null) {
// User is null, initiate auth
Expand Down
9 changes: 9 additions & 0 deletions ios/Classes/SwiftFirebaseAuthUiPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public class SwiftFirebaseAuthUiPlugin: NSObject, FlutterPlugin, FUIAuthDelegate
} catch {
result(false)
}
} else if (call.method == "delete") {
let user = authUI?.auth?.currentUser
if user != nil {
user?.delete(completion: { (error) in
result(error == nil)
})
} else {
result(false)
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions lib/src/firebase_auth_ui/firebase_auth_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,18 @@ class FirebaseAuthUi {
return Future.value(result);
}

/// Delete user.
Future<bool> delete() async {
bool result = await _channel.invokeMethod('delete');
return Future.value(result);
}

FirebaseUser _getFirebaseUser(Map<dynamic, dynamic> userMap) {
MetaData metaData;
if (userMap?.containsKey("metadata") ?? false) {
metaData = MetaData(creationTimestamp: userMap["metadata"]["creation_timestamp"],
lastSignInTimestamp: userMap["metadata"]["last_sign_in_timestamp"]);
metaData = MetaData(
creationTimestamp: userMap["metadata"]["creation_timestamp"],
lastSignInTimestamp: userMap["metadata"]["last_sign_in_timestamp"]);
}
return FirebaseUser(
userMap["uid"] ?? "",
Expand Down

0 comments on commit 5c01363

Please sign in to comment.