Skip to content

Commit

Permalink
feat(authentication): add reload method (#285)
Browse files Browse the repository at this point in the history
* docs

* docs: add changeset

* feat(android): implement reload

* feat(ios): implement reload
  • Loading branch information
trancee authored Dec 17, 2022
1 parent 6842fac commit 8b27765
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-llamas-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": minor
---

feat: add `reload` method
14 changes: 14 additions & 0 deletions packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ const useEmulator = async () => {
* [`linkWithPlayGames(...)`](#linkwithplaygames)
* [`linkWithTwitter(...)`](#linkwithtwitter)
* [`linkWithYahoo(...)`](#linkwithyahoo)
* [`reload()`](#reload)
* [`sendEmailVerification()`](#sendemailverification)
* [`sendPasswordResetEmail(...)`](#sendpasswordresetemail)
* [`sendSignInLinkToEmail(...)`](#sendsigninlinktoemail)
Expand Down Expand Up @@ -839,6 +840,19 @@ The `skipNativeAuth` configuration option has no effect here.
--------------------


### reload()

```typescript
reload() => Promise<void>
```

Reloads user account data, if signed in.

**Since:** 1.3.0

--------------------


### sendEmailVerification()

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ public void linkWithYahoo(final PluginCall call) {
oAuthProviderHandler.link(call, ProviderId.YAHOO);
}

public void reload(FirebaseUser user, @NonNull Runnable callback) {
user
.reload()
.addOnCompleteListener(
task -> {
callback.run();
}
);
}

public void sendEmailVerification(FirebaseUser user, @NonNull Runnable callback) {
user
.sendEmailVerification()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ public void linkWithYahoo(PluginCall call) {
}
}

@PluginMethod
public void reload(PluginCall call) {
try {
FirebaseUser user = implementation.getCurrentUser();
if (user == null) {
call.reject(ERROR_NO_USER_SIGNED_IN);
return;
}
implementation.reload(user, () -> call.resolve());
} catch (Exception ex) {
call.reject(ex.getLocalizedMessage());
}
}

@PluginMethod
public void sendEmailVerification(PluginCall call) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ public typealias AuthStateChangedObserver = () -> Void
self.oAuthProviderHandler?.link(call: call, providerId: ProviderId.yahoo)
}

@objc func reload(user: User, completion: @escaping (Error?) -> Void) {
user.reload { error in
completion(error)
}
}

@objc func sendEmailVerification(user: User, completion: @escaping (Error?) -> Void) {
user.sendEmailVerification(completion: { error in
completion(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CAP_PLUGIN_METHOD(linkWithPlayGames, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(linkWithTwitter, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(linkWithYahoo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(reload, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(sendEmailVerification, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(sendPasswordResetEmail, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(sendSignInLinkToEmail, CAPPluginReturnPromise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ public class FirebaseAuthenticationPlugin: CAPPlugin {
implementation?.linkWithYahoo(call)
}

@objc func reload(_ call: CAPPluginCall) {
guard let user = implementation?.getCurrentUser() else {
call.reject(errorNoUserSignedIn)
return
}

implementation?.reload(user: user, completion: { error in
if let error = error {
call.reject(error.localizedDescription)
return
}
call.resolve()
})
}

@objc func sendEmailVerification(_ call: CAPPluginCall) {
guard let user = implementation?.getCurrentUser() else {
call.reject(errorNoUserSignedIn)
Expand Down
6 changes: 6 additions & 0 deletions packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ export interface FirebaseAuthenticationPlugin {
* @since 1.1.0
*/
linkWithYahoo(options?: LinkWithOAuthOptions): Promise<LinkResult>;
/**
* Reloads user account data, if signed in.
*
* @since 1.3.0
*/
reload(): Promise<void>;
/**
* Sends a verification email to the currently signed in user.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/authentication/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
linkWithCredential,
linkWithPopup,
linkWithRedirect,
reload,
sendEmailVerification,
sendPasswordResetEmail,
sendSignInLinkToEmail,
Expand Down Expand Up @@ -309,6 +310,15 @@ export class FirebaseAuthenticationWeb
return this.createSignInResult(userCredential, authCredential);
}

public async reload(): Promise<void> {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error(FirebaseAuthenticationWeb.ERROR_NO_USER_SIGNED_IN);
}
return reload(currentUser);
}

public async sendEmailVerification(): Promise<void> {
const auth = getAuth();
const currentUser = auth.currentUser;
Expand Down

0 comments on commit 8b27765

Please sign in to comment.