UNPKG

@nitor/aws-react-components

Version:
209 lines 8.99 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var AWS = require("aws-sdk"); var amazon_cognito_identity_js_1 = require("amazon-cognito-identity-js"); var DefaultLoginForm_1 = require("./DefaultLoginForm"); var DefaultResetPasswordForm_1 = require("./DefaultResetPasswordForm"); var AWSCognitoWrapper = /** @class */ (function (_super) { __extends(AWSCognitoWrapper, _super); function AWSCognitoWrapper(props) { var _this = _super.call(this, props) || this; _this.checkForPropsAndWarn(props); _this.state = { requiredPasswordChange: false }; AWS.config.region = props.awsRegion; var poolData = { UserPoolId: props.awsUserPoolId, ClientId: props.awsClientId }; _this.userPool = new amazon_cognito_identity_js_1.CognitoUserPool(poolData); _this.loginHandler = _this.loginHandler.bind(_this); _this.setAwsCredentials = _this.setAwsCredentials.bind(_this); _this.getSessionData = _this.getSessionData.bind(_this); _this.submitForcedNewPassword = _this.submitForcedNewPassword.bind(_this); return _this; } AWSCognitoWrapper.prototype.checkForPropsAndWarn = function (props) { var propsToCheck = [ "awsRegion", "awsUserPoolId", "awsClientId", "awsIdentityPoolId" ]; var missingProps = []; propsToCheck.forEach(function (propName) { var prop = props[propName]; if (prop === null || typeof prop === "undefined") { missingProps.push(propName); } }); if (missingProps.length > 0) { missingProps.forEach(function (propName) { console.error("ERROR: Property '" + propName + "' not passed to AWSCognitoWrapper, it will not work as expected."); }); } }; AWSCognitoWrapper.prototype.setAwsCredentials = function (token) { var _this = this; var loginObject = {}; loginObject['cognito-idp.' + this.props.awsRegion + '.amazonaws.com/' + this.props.awsUserPoolId] = token; AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: this.props.awsIdentityPoolId, Logins: loginObject }); var creds = AWS.config.credentials; if (creds.needsRefresh()) { creds.refresh(function (error) { if (error) { _this.setState({ error: error.message }); console.error(error); } else { console.log('Successfully logged!'); } }); } }; AWSCognitoWrapper.prototype.componentDidMount = function () { this.getSessionData(); }; AWSCognitoWrapper.prototype.getSessionData = function () { var _this = this; var cognitoUser = this .userPool .getCurrentUser(); var self = this; if (cognitoUser !== null) { cognitoUser.getSession(function (err, session) { if (err !== null) { _this.setState({ error: err }); return; } if (session.isValid()) { self.setAwsCredentials(session.getIdToken().getJwtToken()); if (self.props.returnUser) { self .props .returnUser(cognitoUser); } if (self.props.returnAccessToken) { self .props .returnAccessToken(session.getIdToken().getJwtToken()); } if (self.props.returnUserSession) { self .props .returnUserSession(session); } if (cognitoUser !== null) { cognitoUser.getUserAttributes(function (err, attributes) { if (err || typeof attributes === "undefined") { console.log(err); self.setState({ error: "Fetching user attributes failed." }); } else { if (self.props.returnAttributes) { self .props .returnAttributes(attributes); } self.setState({ awsUserAttributes: attributes }); } }); } } }); } }; AWSCognitoWrapper.prototype.loginHandler = function (username, password) { var authenticationData = { Username: username, Password: password }; var authenticationDetails = new amazon_cognito_identity_js_1.AuthenticationDetails(authenticationData); var userData = { Username: username, Pool: this.userPool }; var self = this; var cognitoUser = new amazon_cognito_identity_js_1.CognitoUser(userData); cognitoUser.authenticateUser(authenticationDetails, { onSuccess: function (result) { self.setAwsCredentials(result.getIdToken().getJwtToken()); if (self.props.returnAccessToken) { self .props .returnAccessToken(result.getIdToken().getJwtToken()); } self.getSessionData(); self.setState({ requiredPasswordChange: false }); }, onFailure: function (err) { self.setState({ error: err.message }); }, newPasswordRequired: function (userAttributes, requiredAttributes) { // the api doesn't accept these fields back delete userAttributes.email_verified; delete userAttributes.phone_number_verified; self.setState({ requiredPasswordChange: true, cognitoUser: cognitoUser, userAttributes: userAttributes, self: this }); } }); }; AWSCognitoWrapper.prototype.submitForcedNewPassword = function (newPassword) { if (this.state.cognitoUser) { this.state.cognitoUser. completeNewPasswordChallenge(newPassword, this.state.userAttributes, this.state.self); } else { this.setState({ error: "System error when submitting new password." }); console.log("cognitoUser is not valid in component state."); } }; AWSCognitoWrapper.prototype.render = function () { if (this.state.requiredPasswordChange) { var ResetPasswordType = this.props.overrideResetPassword ? this.props.overrideResetPassword : DefaultResetPasswordForm_1.DefaultResetPasswordForm; return (React.createElement(ResetPasswordType, { returnNewPassword: this.submitForcedNewPassword, error: this.state.error })); } if (AWS.config.credentials === null) { var LoginFormType = this.props.overrideLoginForm ? this.props.overrideLoginForm : DefaultLoginForm_1.DefaultLoginForm; return (React.createElement(LoginFormType, { loginHandler: this.loginHandler, error: this.state.error })); } var self = this; var childrenWithProps = React.Children.map(this.props.children, function (child, index) { if (React.isValidElement(child)) { return React.cloneElement(child, { awsUserAttributes: self.state.awsUserAttributes, AWS: AWS }); } return; }); return (React.createElement("div", null, childrenWithProps)); }; return AWSCognitoWrapper; }(React.Component)); exports.AWSCognitoWrapper = AWSCognitoWrapper; //# sourceMappingURL=AWSCognitoWrapper.js.map