mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-24 04:24:52 +08:00
[auth] Add onIdTokenChanged method
This commit is contained in:
@@ -26,12 +26,19 @@ export default class Auth extends ModuleBase {
|
||||
this.authenticated = false;
|
||||
this.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public event name: onAuthStateChangedPublic
|
||||
this._getAppEventName('onAuthStateChanged'),
|
||||
// public event name: onAuthStateChanged
|
||||
this._getAppEventName('auth_state_changed'),
|
||||
this._onAuthStateChanged.bind(this),
|
||||
);
|
||||
this.addListener(
|
||||
// sub to internal native event - this fans out to
|
||||
// public event name: onIdTokenChanged
|
||||
this._getAppEventName('auth_id_token_changed'),
|
||||
this._onIdTokenChanged.bind(this),
|
||||
);
|
||||
|
||||
this._native.addAuthStateListener();
|
||||
this._native.addIdTokenListener();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,18 +53,42 @@ export default class Auth extends ModuleBase {
|
||||
if (auth && auth.user && !this._user) this._user = new User(this, auth);
|
||||
else if ((!auth || !auth.user) && this._user) this._user = null;
|
||||
else if (this._user) this._user._updateValues(auth);
|
||||
if (emit) this.emit(this._getAppEventName('onAuthStateChangedPublic'), this._user);
|
||||
if (emit) this.emit(this._getAppEventName('onAuthStateChanged'), this._user);
|
||||
return auth ? this._user : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove auth change listener
|
||||
* @param listener
|
||||
*/
|
||||
_offAuthStateChanged(listener: Function) {
|
||||
this.log.info('Removing onAuthStateChanged listener');
|
||||
this.removeListener(this._getAppEventName('onAuthStateChangedPublic'), listener);
|
||||
this.removeListener(this._getAppEventName('onAuthStateChanged'), listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal auth changed listener
|
||||
* @param auth
|
||||
* @param emit
|
||||
* @private
|
||||
*/
|
||||
_onIdTokenChanged(auth: AuthResultType, emit: boolean = true) {
|
||||
this._authResult = auth;
|
||||
this.authenticated = auth ? auth.authenticated || false : false;
|
||||
if (auth && auth.user && !this._user) this._user = new User(this, auth);
|
||||
else if ((!auth || !auth.user) && this._user) this._user = null;
|
||||
else if (this._user) this._user._updateValues(auth);
|
||||
if (emit) this.emit(this._getAppEventName('onIdTokenChanged'), this._user);
|
||||
return auth ? this._user : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove id token change listener
|
||||
* @param listener
|
||||
*/
|
||||
_offIdTokenChanged(listener: Function) {
|
||||
this.log.info('Removing onIdTokenChanged listener');
|
||||
this.removeListener(this._getAppEventName('onIdTokenChanged'), listener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,11 +117,22 @@ export default class Auth extends ModuleBase {
|
||||
*/
|
||||
onAuthStateChanged(listener: Function) {
|
||||
this.log.info('Creating onAuthStateChanged listener');
|
||||
this.on(this._getAppEventName('onAuthStateChangedPublic'), listener);
|
||||
this.on(this._getAppEventName('onAuthStateChanged'), listener);
|
||||
if (this._authResult) listener(this._user || null);
|
||||
return this._offAuthStateChanged.bind(this, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen for id token changes.
|
||||
* @param listener
|
||||
*/
|
||||
onIdTokenChanged(listener: Function) {
|
||||
this.log.info('Creating onIdTokenChanged listener');
|
||||
this.on(this._getAppEventName('onIdTokenChanged'), listener);
|
||||
if (this._authResult) listener(this._user || null);
|
||||
return this._offIdTokenChanged.bind(this, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign the current user out
|
||||
* @return {Promise}
|
||||
|
||||
Reference in New Issue
Block a user