UNPKG

@smartface/plugin-firebase

Version:

Smartface Firebase Plugin for Smartface Native Framework

538 lines 21.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var invocation_1 = __importDefault(require("@smartface/native/util/iOS/invocation")); var User = /** @class */ (function () { /** * @classdesc Firebase User * @see {@link https://firebase.google.com/docs/reference/js/firebase.User|firebase.User} * @constructor */ function User(FIRUser) { var _this = this; /** * Returns the main email address of the user, as stored in the Firebase project's user database. * * @method getEmail * @android * @ios * @since 0.1 */ this.getEmail = function () { return User.ios.native.email(_this.nativeUser); }; /** * Returns the main display name of this user from the Firebase project's user database. * * @method getDisplayName * @android * @ios * @since 0.1 */ this.getDisplayName = function () { return User.ios.native.displayName(_this.nativeUser); }; /** * Returns the URL of this user's main profile picture, as stored in the Firebase project's user database. * * @method getPhotoURL * @android * @ios * @since 0.1 */ this.getPhotoURL = function () { return User.ios.native.photoURL(_this.nativeUser); }; /** * Returns the phone number of the user, as stored in the Firebase project's user database, or null if none exists * * @method getPhoneNumber * @android * @ios * @since 0.1 */ this.getPhoneNumber = function () { return User.ios.native.phoneNumber(_this.nativeUser); }; /** * Returns a string used to uniquely identify your user in your Firebase project's user database. * * @method getUID * @android * @ios * @since 0.1 */ this.getUID = function () { return User.ios.native.uid(_this.nativeUser); }; /** * Returns true if the user is anonymous. * * @method isAnonymous * @android * @ios * @since 0.1 */ this.isAnonymous = function () { return User.ios.native.isAnonymous(_this.nativeUser); }; /** * Indicates the email address associated with this user has been verified. * * @method isEmailVerified * @return {Boolean} verified * @android * @ios * @since 0.1 */ this.isEmailVerified = function () { return User.ios.native.isEmailVerified(_this.nativeUser); }; /** * Reloads the user’s profile data from the server. * * Possible error code; * `RequiresRecentLogin` - Updating email is a security sensitive operation that requires a recent login from the user. This error indicates the user has not signed in recently enough. * * @method reload * @param {Function} callback * @param {Boolean} callback.isSuccess * @param {Object} callback.error * @param {String} callback.error.code * @param {String} callback.error.description * @android * @ios * @since 0.1 */ this.reload = function (callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error)); } else { callback(true, undefined); } } }; User.ios.native.reloadWithCompletion(_this.nativeUser, callbackHandler); }; /** * Set displayName. * * @method setDisplayName * @param {String} displayName * @param {Function} callback * @param {String} callback.token * @param {String} callback.error * @android * @ios * @since 0.1 */ this.setDisplayName = function (displayName, callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error).description); } else { callback(true, undefined); } } }; User.ios.native.setDisplayName(_this.nativeUser, displayName, callbackHandler); }; /** * Set photoURL. * * @method setPhotoURL * @param {String} photoURL * @param {Function} callback * @param {String} callback.token * @param {String} callback.error * @android * @ios * @since 0.1 */ this.setPhotoURL = function (photoURL, callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error).description); } else { callback(true, undefined); } } }; User.ios.native.setPhotoURL(_this.nativeUser, photoURL, callbackHandler); }; /** * Update email. * * Possible error code; * + `EmailAlreadyInUse` * + `InvalidEmail` * + `RequiresRecentLogin` * * @method updateEmail * @param {String} email * @param {Function} callback * @param {Boolean} callback.isSuccess * @param {Object} callback.error * @param {String} callback.error.code * @param {String} callback.error.description * @android * @ios * @since 0.1 */ this.updateEmail = function (email, callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error)); } else { callback(true, undefined); } } }; User.ios.native.updateEmailCompletion(_this.nativeUser, email, callbackHandler); }; /** * Update password. * * Possible error code; * + `OperationNotAllowed` * + `WeakPassword` * + `RequiresRecentLogin` * * @method updatePassword * @param {String} password * @param {Function} callback * @param {Boolean} callback.isSuccess * @param {Object} callback.error * @param {String} callback.error.code * @param {String} callback.error.description * @android * @ios * @since 0.1 */ this.updatePassword = function (password, callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error)); } else { callback(true, undefined); } } }; User.ios.native.updatePasswordCompletion(_this.nativeUser, password, callbackHandler); }; /** * Reauthenticate. * * Possible error code; * + `OperationNotAllowed` * + `UserDisabled` * + `WrongPassword` * + `UserMismatch` * + `InvalidEmail` * * @method reauthenticate * @param {String} email * @param {String} password * @param {Function} callback * @param {Boolean} callback.isSuccess * @param {Object} callback.error * @param {String} callback.error.code * @param {String} callback.error.description * @android * @ios * @since 0.1 */ this.reauthenticate = function (email, password, callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error)); } else { callback(true, undefined); } } }; User.ios.native.reauthenticateWithCredentialCompletion(_this.nativeUser, email, password, callbackHandler); }; /** * Deletes the user record from your Firebase project's database. * If the operation is successful, the user will be signed out. * * Possible error code; * `RequiresRecentLogin` - Updating email is a security sensitive operation that requires a recent login from the user. This error indicates the user has not signed in recently enough. * * @method delete * @param {Function} callback * @param {Boolean} callback.isSuccess * @param {Object} callback.error * @param {String} callback.error.code * @param {String} callback.error.description * @android * @ios * @since 0.1 */ this.delete = function (callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error)); } else { callback(true, undefined); } } }; User.ios.native.deleteWithCompletion(_this.nativeUser, callbackHandler); }; /** * Initiates email verification for the user. * * Possible error code; * + `UserNotFound` - Indicates the user account was not found. * + `UserDisabled` * + `InvalidUserToken` * + `UserTokenExpired` * * @method sendEmailVerification * @param {Function} callback * @param {Boolean} callback.isSuccess * @param {Object} callback.error * @param {String} callback.error.code * @param {String} callback.error.description * @android * @ios * @since 0.1 */ this.sendEmailVerification = function (callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { callback(false, User.ios.native.getErrorObject(e.error)); } else { callback(true, undefined); } } }; User.ios.native.sendEmailVerificationWithCompletion(_this.nativeUser, callbackHandler); }; /** * Returns token. * * @method getIdToken * @param {Boolean} forceRefresh * @param {Function} callback * @param {String} callback.token * @param {String} callback.error * @android * @ios * @since 0.1 */ this.getIdToken = function (forceRefresh, callback) { var callbackHandler = function (e) { if (typeof callback === 'function') { if (e.error) { var error = User.ios.native.getErrorObject(e.error); callback(undefined, error.description); } else { callback(e.token, undefined); } } }; User.ios.native.getIDTokenForcingRefreshCompletion(_this.nativeUser, forceRefresh, callbackHandler); }; this.nativeUser = FIRUser; } User.ios = { native: { isAnonymous: function (user) { return invocation_1.default.invokeInstanceMethod(user, 'isAnonymous', [], 'BOOL'); }, isEmailVerified: function (user) { return invocation_1.default.invokeInstanceMethod(user, 'isEmailVerified', [], 'BOOL'); }, displayName: function (user) { return invocation_1.default.invokeInstanceMethod(user, 'displayName', [], 'NSString'); }, photoURL: function (user) { var url = invocation_1.default.invokeInstanceMethod(user, 'photoURL', [], 'NSObject'); return invocation_1.default.invokeInstanceMethod(url, 'absoluteString', [], 'NSString'); }, setDisplayName: function (user, displayName, completion) { var profileChangeRequest = invocation_1.default.invokeInstanceMethod(user, 'profileChangeRequest', [], 'NSObject'); // @ts-ignore var argDisplayName = new invocation_1.default.Argument({ type: 'NSString', value: displayName }); // @ts-ignore invocation_1.default.invokeInstanceMethod(profileChangeRequest, 'setDisplayName:', [argDisplayName]); // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(profileChangeRequest, 'commitChangesWithCompletion:', [argCompletion]); }, setPhotoURL: function (user, photoURL, completion) { // @ts-ignore var argURL = new invocation_1.default.Argument({ type: 'NSString', value: photoURL }); var url = invocation_1.default.invokeClassMethod('NSURL', 'URLWithString:', [argURL], 'NSObject'); var profileChangeRequest = invocation_1.default.invokeInstanceMethod(user, 'profileChangeRequest', [], 'NSObject'); // @ts-ignore var argPhotoURL = new invocation_1.default.Argument({ type: 'NSObject', value: url }); // @ts-ignore invocation_1.default.invokeInstanceMethod(profileChangeRequest, 'setPhotoURL:', [argPhotoURL]); // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(profileChangeRequest, 'commitChangesWithCompletion:', [argCompletion]); }, phoneNumber: function (user) { return invocation_1.default.invokeInstanceMethod(user, 'phoneNumber', [], 'NSString'); }, email: function (user) { return invocation_1.default.invokeInstanceMethod(user, 'email', [], 'NSString'); }, uid: function (user) { return invocation_1.default.invokeInstanceMethod(user, 'uid', [], 'NSString'); }, reloadWithCompletion: function (user, completion) { // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(user, 'reloadWithCompletion:', [argCompletion]); }, updateEmailCompletion: function (user, email, completion) { // @ts-ignore var argEmail = new invocation_1.default.Argument({ type: 'NSString', value: email }); // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(user, 'updateEmail:completion:', [argEmail, argCompletion]); }, updatePasswordCompletion: function (user, password, completion) { // @ts-ignore var argPassword = new invocation_1.default.Argument({ type: 'NSString', value: password }); // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(user, 'updatePassword:completion:', [argPassword, argCompletion]); }, getIDTokenForcingRefreshCompletion: function (user, forceRefresh, completion) { // @ts-ignore var argForceRefresh = new invocation_1.default.Argument({ type: 'BOOL', value: forceRefresh }); // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'FIRAuthTokenCallback', value: function (e) { completion(e); } }); // @ts-ignore return invocation_1.default.invokeInstanceMethod(user, 'getIDTokenForcingRefresh:completion:', [argForceRefresh, argCompletion]); }, getErrorObject: function (nativeError) { var code = invocation_1.default.invokeInstanceMethod(nativeError, 'code', [], 'NSInteger'); var localizedDescription = invocation_1.default.invokeInstanceMethod(nativeError, 'localizedDescription', [], 'NSString'); return { code: code, description: localizedDescription }; }, reauthenticateWithCredentialCompletion: function (user, email, password, completion) { // @ts-ignore var argEmail = new invocation_1.default.Argument({ type: 'NSString', value: email }); // @ts-ignore var argPassword = new invocation_1.default.Argument({ type: 'NSString', value: password }); var credential = invocation_1.default.invokeClassMethod('FIREmailAuthProvider', 'credentialWithEmail:password:', [argEmail, argPassword], 'NSObject'); // @ts-ignore var argCredential = new invocation_1.default.Argument({ type: 'NSObject', value: credential }); // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(user, 'reauthenticateWithCredential:completion:', [argCredential, argCompletion]); }, deleteWithCompletion: function (user, completion) { // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(user, 'deleteWithCompletion:', [argCompletion]); }, sendEmailVerificationWithCompletion: function (user, completion) { // @ts-ignore var argCompletion = new invocation_1.default.Argument({ type: 'UserProfileChangeCallback', value: function (e) { completion(e); } }); // @ts-ignore invocation_1.default.invokeInstanceMethod(user, 'sendEmailVerificationWithCompletion:', [argCompletion]); } } }; return User; }()); exports.default = User; module.exports = User; //# sourceMappingURL=user-iOS.js.map