react-native-firebase/lib/modules/auth/user.js

149 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-03-02 13:09:41 +00:00
import { NativeModules, NativeEventEmitter } from 'react-native';
const FirebaseAuth = NativeModules.RNFirebaseAuth;
2017-02-14 11:41:27 +00:00
// TODO refreshToken property
// TODO reload() method
2017-03-02 13:09:41 +00:00
/**
* @url https://firebase.google.com/docs/reference/js/firebase.User
*/
2017-02-14 11:41:27 +00:00
export default class User {
2017-03-02 13:09:41 +00:00
/**
*
* @param authClass Instance of Authentication class
* @param authObj authentication result object from native
*/
2017-02-14 11:41:27 +00:00
constructor(authClass, authObj) {
this._auth = authClass;
this._user = null;
this._updateValues(authObj);
}
/**
* INTERNALS
*/
/**
*
* @param authObj
* @private
*/
_updateValues(authObj) {
this._authObj = authObj;
if (authObj.user) {
this._user = authObj.user;
} else {
this._user = null;
}
}
/**
* Returns a user property or null if does not exist
* @param prop
* @returns {*}
* @private
*/
_valueOrNull(prop) {
if (!this._user) return null;
if (!Object.hasOwnProperty.call(this._user, prop)) return null;
return this._user[prop];
}
/**
* PROPERTIES
*/
get displayName() {
return this._valueOrNull('displayName');
}
get email() {
return this._valueOrNull('email');
}
get emailVerified() {
return this._valueOrNull('emailVerified');
}
get isAnonymous() {
return !this._valueOrNull('email') && this._valueOrNull('providerId') === 'firebase';
}
get photoURL() {
return this._valueOrNull('photoURL');
}
2017-03-13 20:01:45 +00:00
// TODO no RN android method yet, the SDK does have .getProviderData but returns as a List.
2017-02-14 11:41:27 +00:00
// get providerData() {
// return this._valueOrNull('providerData');
// }
get providerId() {
return this._valueOrNull('providerId');
}
// TODO no android method
// get refreshToken() {
// return this._valueOrNull('refreshToken');
// }
get uid() {
return this._valueOrNull('uid');
}
// noinspection ReservedWordAsName
/**
* METHODS
*/
2017-03-13 20:01:45 +00:00
toJSON() {
return {
uid: this.uid,
email: this.email,
emailVerified: this.emailVerified,
displayName: this.displayName,
providerId: this.providerId,
isAnonymous: this.isAnonymous,
photoURL: this.photoURL,
};
}
2017-02-14 11:41:27 +00:00
delete(...args) {
return this._auth.deleteUser(...args);
}
reload(...args) {
return this._auth.reloadUser(...args);
}
// TODO valueOrNul token - optional promise
getToken(...args) {
return this._auth.getToken(...args);
}
get reauthenticate() {
return this._auth.reauthenticateUser;
}
// TODO match errors to auth/something errors from firebase web api
get updateEmail() {
if (this.isAnonymous) return () => Promise.reject(new Error('Can not update email on an anonymous user.'));
return this._auth.updateEmail;
}
get updateProfile() {
return this._auth.updateProfile;
}
get updatePassword() {
if (this.isAnonymous) return () => Promise.reject(new Error('Can not update password on an anonymous user.'));
return this._auth.updatePassword;
}
get sendEmailVerification() {
if (this.isAnonymous) return () => Promise.reject(new Error('Can not verify email on an anonymous user.'));
return this._auth.sendEmailVerification;
}
}