UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

16 lines 4.29 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function _extends(){_extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key];}}}return target;};return _extends.apply(this,arguments);}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);});};}import{PipelineRequest,EINVALIDCALL,ELEGACYSGCONNECT,EINCOMPLETELOGIN}from'@shopgate/pwa-core';import{requestLogin,successLogin,errorLogin,errorLegacyConnectRegister}from"../../action-creators/user";import{SHOPGATE_USER_LOGIN_USER}from"../../constants/Pipelines";import{DEFAULT_LOGIN_STRATEGY}from"../../constants/user";import{mutable}from"../../helpers/redux";import appConfig from"../../helpers/config";/** * Login the current user. * @param {Object} parameters The login credentials. * @param {string} parameters.login The username to login. * @param {string} parameters.password The password to login. * @param {string} redirect The location to redirect to when logged in. * @param {string} strategy basic or facebook, amazon, etc * @return {Function} A redux thunk. */function login(parameters,redirect){var strategy=arguments.length>2&&arguments[2]!==undefined?arguments[2]:DEFAULT_LOGIN_STRATEGY;return(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(dispatch){var _ref2,recaptchaEnabled,googleCloudSiteKey,recaptchaToken,request;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_ref2=(appConfig===null||appConfig===void 0?void 0:appConfig.recaptcha)||{},recaptchaEnabled=_ref2.enabled,googleCloudSiteKey=_ref2.googleCloudSiteKey;dispatch(requestLogin(parameters.login,parameters.password,strategy));if(!(recaptchaEnabled&&googleCloudSiteKey)){_context.next=13;break;}_context.prev=3;_context.next=6;return new Promise(function(resolve){return window.grecaptcha.enterprise.ready(resolve);});case 6:_context.next=8;return window.grecaptcha.enterprise.execute(googleCloudSiteKey,{action:'login'});case 8:recaptchaToken=_context.sent;_context.next=13;break;case 11:_context.prev=11;_context.t0=_context["catch"](3);case 13:request=new PipelineRequest(SHOPGATE_USER_LOGIN_USER).setTrusted().setErrorBlacklist([EINVALIDCALL,ELEGACYSGCONNECT,EINCOMPLETELOGIN]).setInput({strategy:strategy,parameters:_extends({},parameters,{},recaptchaToken?{recaptchaToken:recaptchaToken}:null)}).dispatch();request.then(function(result){var success=result.success,messages=result.messages,sessionLifetimeInSeconds=result.sessionLifetimeInSeconds;if(success){dispatch(successLogin(redirect,strategy,sessionLifetimeInSeconds));}else{dispatch(errorLogin(messages));}})["catch"](function(error){var code=error.code;if(code===EINVALIDCALL){/** * This code is thrown when the login request failed, because the user was already logged * in. In that situation the success action can also dispatch to trigger the necessary * processes which has to happen after a successful login. */dispatch(successLogin(redirect,strategy));}else if(code===ELEGACYSGCONNECT){/** * The app is connected to the shop system via the legacy shopgate connect. Login via * the shop system credentials failed and further steps are necessary to login the user. */dispatch(errorLegacyConnectRegister());dispatch(errorLogin([],ELEGACYSGCONNECT));}else{dispatch(errorLogin([],code));}});return _context.abrupt("return",request);case 16:case"end":return _context.stop();}},_callee,null,[[3,11]]);}));return function(_x){return _ref.apply(this,arguments);};}());}/** @mixes {MutableFunction} */export default mutable(login);