Skip to content

Commit

Permalink
feat: extend authentication to support id token change event (#710)
Browse files Browse the repository at this point in the history
* implements suggested changes
  • Loading branch information
ebarooni committed Nov 7, 2024
1 parent fda61fd commit c7f90cf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .changeset/chilled-pumpkins-kick.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
'@capacitor-firebase/authentication': patch
'@capacitor-firebase/authentication': minor
---

feat: add `idTokenChange` event listener
8 changes: 4 additions & 4 deletions packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1584,9 +1584,9 @@ Listen for the user's sign-in state changes.
addListener(eventName: 'idTokenChange', listenerFunc: IdTokenChangeListener) => Promise<PluginListenerHandle>
```

Listen for changes to the signed-in user's ID token changes.
Listen to ID token changes for the currently signed-in user.

**Attention:** This listener will not be triggered automatically upon ID token expiration.
**Attention:** This listener is not triggered when the `skipNativeAuth` is used. Use the Firebase JavaScript SDK instead.

| Param | Type |
| ------------------ | ----------------------------------------------------------------------- |
Expand All @@ -1595,7 +1595,7 @@ Listen for changes to the signed-in user's ID token changes.

**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>

**Since:** 6.2.0
**Since:** 6.3.0

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

Expand Down Expand Up @@ -2115,7 +2115,7 @@ Callback to receive the user's sign-in state change notifications.

#### IdTokenChangeListener

Callback to receive the id token change notifications.
Callback to receive the ID token change notifications.

<code>(change: <a href="#getidtokenresult">GetIdTokenResult</a>): void</code>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,7 @@ public void success(Result result) {

@Override
public void error(Exception exception) {
JSObject changeResult = new JSObject();
changeResult.put("token", "");
notifyListeners(ID_TOKEN_CHANGE_EVENT, changeResult, true);
Logger.error(TAG, exception.getMessage(), exception);
}
};
implementation.getIdToken(false, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,13 @@ public class FirebaseAuthenticationPlugin: CAPPlugin {

@objc func handleIdTokenChange() {
implementation?.getIdToken(false, completion: { result, error in
var changeResult = JSObject()
if let error = error {
CAPLog.print("[", self.tag, "] ", error)
changeResult["token"] = ""
return
}
if let result = result {
changeResult = result.toJSObject()
self.notifyListeners(self.idTokenChangeEvent, data: result.toJSObject(), retainUntilConsumed: true)
}
self.notifyListeners(self.idTokenChangeEvent, data: changeResult, retainUntilConsumed: true)
})
}

Expand Down
6 changes: 3 additions & 3 deletions packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ export interface FirebaseAuthenticationPlugin {
/**
* Listen to ID token changes for the currently signed-in user.
*
* **Attention:** This listener will not be triggered automatically upon ID token expiration.
* **Attention:** This listener is not triggered when the `skipNativeAuth` is used. Use the Firebase JavaScript SDK instead.
*
* @since 6.2.0
* @since 6.3.0
*/
addListener(
eventName: 'idTokenChange',
Expand Down Expand Up @@ -1423,7 +1423,7 @@ export type AuthStateChangeListener = (change: AuthStateChange) => void;
/**
* Callback to receive the ID token change notifications.
*
* @since 6.2.0
* @since 6.3.0
*/
export type IdTokenChangeListener = (change: GetIdTokenResult) => void;

Expand Down
20 changes: 9 additions & 11 deletions packages/authentication/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class FirebaseAuthenticationWeb
super();
const auth = getAuth();
auth.onAuthStateChanged(user => this.handleAuthStateChange(user));
auth.onIdTokenChanged(() => void this.handleIdTokenChange());
auth.onIdTokenChanged(user => void this.handleIdTokenChange(user));
}

public async applyActionCode(options: ApplyActionCodeOptions): Promise<void> {
Expand Down Expand Up @@ -792,19 +792,17 @@ export class FirebaseAuthenticationWeb
);
}

private async handleIdTokenChange(): Promise<void> {
const change: GetIdTokenResult = {
token: '',
};
try {
const { token } = await this.getIdToken({ forceRefresh: false });
change.token = token;
} catch {
change.token = '';
private async handleIdTokenChange(user: FirebaseUser | null): Promise<void> {
if (!user) {
return;
}
const idToken = await user.getIdToken(false);
const result: GetIdTokenResult = {
token: idToken,
};
this.notifyListeners(
FirebaseAuthenticationWeb.ID_TOKEN_CHANGE_EVENT,
change,
result,
true,
);
}
Expand Down

0 comments on commit c7f90cf

Please sign in to comment.