aws-amplify-react-custom-ui
Version:
417 lines (362 loc) • 11.4 kB
JavaScript
import React, { Component } from 'react';
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var _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;
};
var inherits = function (subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
};
var possibleConstructorReturn = function (self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};
var isFunctionComponent = function isFunctionComponent(component) {
return typeof component === "function" && String(component).includes("return React.createElement") ? true : false;
};
var DisplayComponentArg = function (_Component) {
inherits(DisplayComponentArg, _Component);
function DisplayComponentArg() {
var _ref;
var _temp, _this, _ret;
classCallCheck(this, DisplayComponentArg);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = possibleConstructorReturn(this, (_ref = DisplayComponentArg.__proto__ || Object.getPrototypeOf(DisplayComponentArg)).call.apply(_ref, [this].concat(args))), _this), _this.errorMessage = function (err) {
if (typeof err === "string") {
return err;
}
return err.message ? err.message : JSON.stringify(err);
}, _temp), possibleConstructorReturn(_this, _ret);
}
createClass(DisplayComponentArg, [{
key: "render",
value: function render() {
var _this2 = this;
var _props = this.props,
Content = _props.content,
type = _props.type,
onAuthEvent = _props.onAuthEvent,
authState = _props.authState,
onStateChange = _props.onStateChange;
var changeState = function changeState(state, data) {
if (onStateChange) {
onStateChange(state, data);
}
onAuthEvent(state, {
type: "stateChange",
data: state
});
};
var error = function error(err) {
var state = authState;
onAuthEvent(state, {
type: "error",
data: _this2.errorMessage(err)
});
};
if (isFunctionComponent(Content)) {
return Content({
type: type,
onAuthEvent: onAuthEvent,
authState: authState,
onStateChange: onStateChange,
changeAuthState: changeState,
authError: error
});
}
var props = {
type: type,
onAuthEvent: onAuthEvent,
authState: authState,
onStateChange: onStateChange,
changeAuthState: changeState,
authError: error
};
return React.createElement(Content, props);
}
}]);
return DisplayComponentArg;
}(Component);
var HocAuthComponent = function () {
return function (_Component2) {
inherits(_class, _Component2);
function _class() {
classCallCheck(this, _class);
return possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
}
createClass(_class, [{
key: "render",
value: function render() {
var props = this.props;
var _props2 = this.props,
authState = _props2.authState,
content = _props2.content,
onStateChange = _props2.onStateChange,
type = _props2.type,
onAuthEvent = _props2.onAuthEvent;
if (authState && authState.includes(type)) {
return React.createElement(DisplayComponentArg, this.props);
}
return null;
}
}]);
return _class;
}(Component);
}();
var Greetings = function Greetings() {
return React.createElement(
"div",
null,
"please provied : Greetings"
);
},
SignIn = function SignIn() {
return React.createElement(
"div",
null,
"please provied : SignIn"
);
},
ConfirmSignIn = function ConfirmSignIn() {
return React.createElement(
"div",
null,
"please provied : ConfirmSignIn"
);
},
RequireNewPassword = function RequireNewPassword() {
return React.createElement(
"div",
null,
"please provied : RequireNewPassword"
);
},
SignUp = function SignUp() {
return React.createElement(
"div",
null,
" please provied : SignUp"
);
},
ConfirmSignUp = function ConfirmSignUp() {
return React.createElement(
"div",
null,
" please provied : ConfirmSignUp"
);
},
VerifyContact = function VerifyContact() {
return React.createElement(
"div",
null,
"please provied : VerifyContact"
);
},
ForgotPassword = function ForgotPassword() {
return React.createElement(
"div",
null,
"please provied : ForgotPassword"
);
},
TOTPSetup = function TOTPSetup() {
return React.createElement(
"div",
null,
"please provied : TOTPSetup"
);
};
var AmplifyAuthenticator = null;
var configure = function configure(aws_amplify_react) {
AmplifyAuthenticator = aws_amplify_react.Authenticator;
Greetings = aws_amplify_react.Greetings;
setSignIn(aws_amplify_react.SignIn);
setTOTPSetup(aws_amplify_react.TOTPSetup);
setForgotPassword(aws_amplify_react.ForgotPassword);
setConfirmSignUp(aws_amplify_react.ConfirmSignUp);
setVerifyContact(aws_amplify_react.VerifyContact);
setSignUp(aws_amplify_react.SignUp);
setRequireNewPassword(aws_amplify_react.RequireNewPassword);
setConfirmSignIn(aws_amplify_react.ConfirmSignIn);
};
var configCustomUi = [{
type: "greetings",
component: Greetings
}, {
type: "signIn",
component: SignIn
}, {
type: "confirmSignIn",
component: ConfirmSignIn
}, {
type: "requireNewPassword",
component: RequireNewPassword
}, {
type: "signUp",
component: SignUp
}, {
type: "confirmSignUp",
component: ConfirmSignUp
}, {
type: "verifyContact",
component: VerifyContact
}, {
type: "forgotPassword",
component: ForgotPassword
}, {
type: "TOTPSetup",
component: TOTPSetup
}];
var setComponent = function setComponent(componentType, component) {
if (component) {
var index = configCustomUi.map(function (_ref2) {
var type = _ref2.type;
return type;
}).indexOf(componentType);
configCustomUi[index].component = component;
} else {
throw "please provide a valid component";
}
};
var generateCustomUi = function generateCustomUi() {
var costumUiLocal = [];
configCustomUi.map(function (Item) {
costumUiLocal.push(React.createElement(HocAuthComponent, { content: Item.component, type: Item.type }));
});
console.log("costumUiLocal", costumUiLocal);
return costumUiLocal;
};
var setSignIn = function setSignIn(component) {
setComponent("signIn", component);
};
var setConfirmSignIn = function setConfirmSignIn(component) {
setComponent("confirmSignIn", component);
};
var setRequireNewPassword = function setRequireNewPassword(component) {
setComponent("requireNewPassword", component);
};
var setSignUp = function setSignUp(component) {
setComponent("signUp", component);
};
var setVerifyContact = function setVerifyContact(component) {
setComponent("verifyContact", component);
};
var setConfirmSignUp = function setConfirmSignUp(component) {
setComponent("confirmSignUp", component);
};
var setForgotPassword = function setForgotPassword(component) {
setComponent("forgotPassword", component);
};
var setTOTPSetup = function setTOTPSetup(component) {
setComponent("TOTPSetup", component);
};
var withAuthenticator = function withAuthenticator(Comp) {
var federated = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var theme = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
return function (_Component3) {
inherits(_class3, _Component3);
function _class3(props) {
classCallCheck(this, _class3);
var _this4 = possibleConstructorReturn(this, (_class3.__proto__ || Object.getPrototypeOf(_class3)).call(this, props));
_this4.authenticatorComponents = [];
_this4.handleAuthStateChange = function (state, data) {
_this4.setState({ authState: state, authData: data });
};
_this4.state = {
authState: props.authState || null,
authData: props.authData || null
};
return _this4;
}
createClass(_class3, [{
key: "componentDidMount",
value: function componentDidMount() {
this.authenticatorComponents = generateCustomUi();
}
}, {
key: "render",
value: function render() {
var _state = this.state,
authState = _state.authState,
authData = _state.authData;
var signedIn = authState === "signedIn";
if (signedIn) {
return React.createElement(
"div",
null,
React.createElement(Comp, _extends({}, this.props, {
authState: authState,
authData: authData,
onStateChange: this.handleAuthStateChange
}))
);
}
return React.createElement(AmplifyAuthenticator, _extends({}, this.props, {
theme: theme,
federated: federated || this.props.federated,
hideDefault: this.authenticatorComponents.length > 0,
onStateChange: this.handleAuthStateChange,
children: this.authenticatorComponents
}));
}
}]);
return _class3;
}(Component);
};
var index = {
withAuthenticator: withAuthenticator,
configure: configure,
//Component Setter
setSignIn: setSignIn,
setTOTPSetup: setTOTPSetup,
setForgotPassword: setForgotPassword,
setConfirmSignUp: setConfirmSignUp,
setVerifyContact: setVerifyContact,
setSignUp: setSignUp,
setRequireNewPassword: setRequireNewPassword,
setConfirmSignIn: setConfirmSignIn
};
export default index;
//# sourceMappingURL=index.es.js.map