@allsquare/react-apple-signin-auth
Version:
Apple signin for React using the official Apple JS SDK
68 lines (52 loc) • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _waitForVar = _interopRequireDefault(require("../utils/waitForVar"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
/**
* Performs an apple ID signIn operation
*/
var signIn = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(function* (_ref) {
var authOptions = _ref.authOptions,
onSuccess = _ref.onSuccess,
onError = _ref.onError;
try {
/** wait for apple sript to load */
yield (0, _waitForVar.default)('AppleID');
/** Handle if appleID script was not loaded -- log + throw error to be caught below */
if (!window.AppleID) {
console.error(new Error('Error loading apple script'));
}
/** Init apple auth */
window.AppleID.auth.init(authOptions);
/** Signin to appleID */
var response = yield window.AppleID.auth.signIn();
/** This is only called in case usePopup is true */
if (onSuccess) {
onSuccess(response);
}
/** resolve with the reponse */
return response;
} catch (err) {
if (onError) {
/** Call onError catching the error */
onError(err);
} else {
/** Log the error to help debug */
console.error(err);
}
}
});
return function signIn(_x) {
return _ref2.apply(this, arguments);
};
}();
var _default = {
signIn: signIn
};
exports.default = _default;