{"id":147,"date":"2019-07-11T22:00:50","date_gmt":"2019-07-11T13:00:50","guid":{"rendered":"https:\/\/blog.hacya.com\/?p=147"},"modified":"2019-09-01T13:44:58","modified_gmt":"2019-09-01T04:44:58","slug":"cognitoamplifyreact3","status":"publish","type":"post","link":"https:\/\/blog.hacya.com\/index.php\/2019\/07\/11\/cognitoamplifyreact3\/","title":{"rendered":"Cognito\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3092Amplify\u3068React\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3059\u308b\u3002(\u305d\u306e3)"},"content":{"rendered":"\n<p>Client\u5074\u306eAmplify\u3092Install\u3057\u307e\u3059\u3002\u74b0\u5883\u306f\u524d\u56de\u300c<a href=\"https:\/\/blog.hacya.com\/index.php\/2019\/06\/29\/reactstaticwebhosting1\/\">React\u3092\u4f7f\u3063\u3066Static\u306aWeb Hosting\u3092\u3059\u308b\u3002<\/a>\u300d\u3067\u4f7f\u7528\u3057\u305f\u74b0\u5883\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002<br><code>npm install --save aws-amplify aws-amplify-react<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"632\" src=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify1_edit-1024x632.png\" alt=\"\" class=\"wp-image-149\" srcset=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify1_edit-1024x632.png 1024w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify1_edit-300x185.png 300w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify1_edit-768x474.png 768w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify1_edit-1200x741.png 1200w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify1_edit.png 1223w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>Client\u5074\u306eApplication\u3092\u4f5c\u3063\u3066\u884c\u304d\u307e\u3059\u3002React\u306eWelcome\u753b\u9762\u306bCognito\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u521d\u3081\u306b\u8aad\u307f\u8fbc\u307e\u308c\u308b\u3001index.js\u306bAWS Amplify\u306e\u8a2d\u5b9a\u3092\u5165\u308c\u3066\u7f6e\u304f&#8221;services&#8221;\u3068\u3044\u3046file\u3092import\u3059\u308b\u884c\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<br><code>import { configureAmplify } from '.\/services'<\/code><br>\u305d\u306e\u3042\u3068\u3067\u3001Amlify\u3092configure\u3059\u308b\u305f\u3081\u306b\u3001<br><code>configureAmplify();<\/code><br>\u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002ReactDom.render\u306b\u6e21\u3059element\u3092&lt;App \/&gt;\u304b\u3089&lt;AppWithAuth \/&gt;\u306b\u5909\u66f4\u3057\u3001\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3092\u8ffd\u52a0\u3057\u305f\u63cf\u753b\u51e6\u7406\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<br><code>ReactDOM.render(&lt;AppWithAuth \/&gt;, document.getElementById('root'));<\/code><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"index.js\"><code>import React from &#39;react&#39;;\nimport ReactDOM from &#39;react-dom&#39;;\nimport &#39;.\/index.css&#39;;\nimport AppWithAuth from &#39;.\/AppWithAuth&#39;;\nimport * as serviceWorker from &#39;.\/serviceWorker&#39;;\n\n\/\/Import Amplify Configure function\nimport { configureAmplify } from &#39;.\/services&#39;\n\nconfigureAmplify();\n\nReactDOM.render(&lt;AppWithAuth \/&gt;, document.getElementById(&#39;root&#39;));\n\n\/\/ If you want your app to work offline and load faster, you can change\n\/\/ unregister() to register() below. Note this comes with some pitfalls.\n\/\/ Learn more about service workers: http:\/\/bit.ly\/CRA-PWA\nserviceWorker.unregister();<\/code><\/pre><\/div>\n\n\n\n<p>index.js\u304b\u3089import\u3055\u308c\u308b&#8221;services&#8221;\u3092\u6e96\u5099\u3057\u307e\u3059\u3002&#8221;services&#8221;\u3067\u306f\u3001Amplify\u306e\u8a2d\u5b9a\u3092\u3057\u307e\u3059\u304c\u3001\u5b9f\u969b\u306e\u60c5\u5831\u306f\u74b0\u5883\u5909\u6570\u306b\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u3001\u8d77\u52d5\u6642\u306b&#8221;.env&#8221;\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u74b0\u5883\u304c\u5909\u308f\u3063\u3066\u3082\u518dbuild\u3059\u308b\u3053\u3068\u306a\u304f\u3001\u74b0\u5883\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"services.js\"><code>import Amplify from &#39;@aws-amplify\/core&#39;;\n\nexport function configureAmplify() {\n  Amplify.configure(\n  {\n    Auth: {\n      region: process.env.REACT_APP_region,\n      userPoolId: process.env.REACT_APP_userPoolId,\n      userPoolWebClientId: process.env.REACT_APP_userPoolWebClientId,\n      identityPoolId: process.env.REACT_APP_identityPoolId,\n   }\n } );\n}<\/code><\/pre><\/div>\n\n\n\n<p>&#8220;.env&#8221;\u30674\u3064\u306e\u60c5\u5831\u3092\u8a2d\u5b9a\u3057\u3001project\u306eroot directory\u306b\u7f6e\u304d\u307e\u3059\u3002<br><code>REACT_APP_region=ap-northeast-1<\/code><br><code>REACT_APP_userPoolId=ap-northeast-1<em>yourPoolID<\/em><\/code><br><code>REACT_APP_userPoolWebClientId=<em>yourWebClientId<\/em><\/code><br><code>REACT_APP_identityPoolId=ap-northeast-1:<em>yourIdentityPoolId<\/em><\/code><br>\u3053\u308c\u3089\u306e\u60c5\u5831\u306f\u3001Cognito User Pool\u3092\u4f5c\u6210\u3057\u305f\u6642\u306b\u751f\u6210\u3055\u308c\u305fUser ID\u3001App Client ID\u3068\u3001Cognito Identity Pool\u3092\u4f5c\u6210\u3057\u305f\u6642\u306b\u751f\u6210\u3055\u308c\u305fIdentity Pool ID\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u3053\u308c\u3089\u306f\u3001Cognito\u306b\u63a5\u7d9a\u3059\u308b\u305f\u3081\u306e\u60c5\u5831\u3067\u3059\u304c\u3001URL\u306e\u3088\u3046\u306a\u7269\u3067\u96a0\u3059\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30dc\u30c3\u30c8\u306b\u3088\u308b\u653b\u6483\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u306f\u3001Cognito User Pool\u306e\u4f5c\u6210\u306e\u969b\u306b\u3001Client Device\u306e\u8a8d\u8a3c\u3092\u8ffd\u52a0\u3059\u308b\u306a\u3069\u691c\u8a0e\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6b21\u306b\u3001 ReactDom.render\u304b\u3089\u6700\u521d\u306b\u63cf\u753b\u3055\u308c\u308belement\u3001&#8221;AppWithAuth.js&#8221;\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\u4eca\u56de\u306fE\u30e1\u30fc\u30eb\u306b\u3088\u308b\u8a8d\u8a3c\u3092\u884c\u308f\u306a\u3044\u305f\u3081\u3001Login\u753b\u9762\u3092\u7c21\u7d20\u5316\u3057\u307e\u3057\u305f\u3002\u5358\u7d14\u306busername\u3068password\u3060\u3051\u3092\u5165\u529b\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"AppWithAuth.js\"><code>import React from &#39;react&#39;;\nimport { Authenticator, ConfirmSignIn, RequireNewPassword, ConfirmSignUp, VerifyContact, ForgotPassword, TOTPSetup } from &#39;aws-amplify-react&#39;;\nimport App from &quot;.\/App&quot;;\nimport MyVerifyContact from &quot;.\/MyVerifyContact&quot;\nimport MyConfirmSignUp from &quot;.\/MyConfirmSignUp&quot;\n\nconst signUpConfig = {\n  hideAllDefaults: true,\n  signUpFields: [\n    {\n      label: &#39;Username&#39;,\n      key: &#39;username&#39;,\n      required: true,\n      placeholder: &#39;Username&#39;,\n      displayOrder: 1\n    },\n    {\n      label: &#39;Password&#39;,\n      key: &#39;password&#39;,\n      required: true,\n      placeholder: &#39;Password&#39;,\n      type: &#39;password&#39;,\n      displayOrder: 2\n    }\n  ]\n}\n\nfunction AppWithAuth() {\n\n  function handleAuthStateChange(state, data) {\n    if (state === &#39;confirmSignIn&#39;) {\n        \/* Do something when the user has signed-in *\/\n    }\n  }\n\n  return (\n    &lt;Authenticator signUpConfig={signUpConfig} hide={[ConfirmSignIn, RequireNewPassword, VerifyContact, ConfirmSignUp, ForgotPassword, TOTPSetup]} onStateChange={handleAuthStateChange}&gt;\n      &lt;MyVerifyContact override={VerifyContact} \/&gt;\n      &lt;MyConfirmSignUp override={ConfirmSignUp} \/&gt;\n      &lt;App \/&gt;\n    &lt;\/Authenticator&gt;\n  )\n}\n\nexport default AppWithAuth;\n<\/code><\/pre><\/div>\n\n\n\n<p>Login\u753b\u9762\u3092\u7c21\u7d20\u5316\u3059\u308b\u305f\u3081\u306b\u3001Amplify\u304c\u63d0\u4f9b\u3057\u3066\u3044\u308b&#8221;VerifyContact&#8221;\u3001&#8221;ConfirmSignUp&#8221;\u3092override\u3057\u3066Customize\u3057\u3066\u3044\u307e\u3059\u3002Amplify\u306f\u624b\u3063\u53d6\u308a\u65e9\u304fApplication\u3092\u4f5c\u6210\u3067\u304d\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u3046\u3044\u3063\u305fCustomize\u3092\u3059\u308b\u306e\u304c\u7c21\u5358\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"MyVerifyContact.js\"><code>import { AuthPiece } from &#39;aws-amplify-react&#39;;\n\nclass MyVerifyContact extends AuthPiece {\n  constructor(props) {\n    super(props);\n    this._validAuthStates = [&quot;verifyContact&quot;];\n    this.state = {}\n  }\n\n  static getDerivedStateFromProps(props, state) {\n    if(props.authState === &quot;verifyContact&quot;) {\n      props.onStateChange(&#39;signedIn&#39;);\n      return props;\n    } else {\n      return null;\n    }\n  }\n\n  showComponent(theme) {\n    return null;\n  }\n\n}\n\nexport default MyVerifyContact;<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"MyConfirmSignUp.js\"><code>import React from &#39;react&#39;;\n\nimport { I18n } from &#39;aws-amplify&#39;;\nimport { AuthPiece } from &#39;aws-amplify-react&#39;;\n\nimport {\n  FormSection,\n  SectionHeader,\n  SectionBody,\n  SectionFooter,\n  Button\n} from &#39;aws-amplify-react&#39;;\n\nclass MyConfirmSignUp extends AuthPiece {\n  constructor(props) {\n    super(props);\n    this._validAuthStates = [&quot;confirmSignUp&quot;];\n    this.gotoSignIn = this.gotoSignIn.bind(this);\n  }\n\n  gotoSignIn() {\n    super.changeState(&#39;signIn&#39;);\n  }\n\n  showComponent(theme) {\n    return (\n      &lt;FormSection theme={theme}&gt;\n        &lt;SectionHeader theme={theme}&gt;{I18n.get(&#39;Please contact your administrator to grant your account.&#39;)}&lt;\/SectionHeader&gt;\n        &lt;SectionBody theme={theme}&gt;\n        &lt;\/SectionBody&gt;\n        &lt;SectionFooter theme={theme}&gt;\n          &lt;Button onClick={this.gotoSignIn} theme={theme}&gt;\n            {I18n.get(&#39;Goto SignIn&#39;)}\n          &lt;\/Button&gt;\n        &lt;\/SectionFooter&gt;\n      &lt;\/FormSection&gt;\n    );\n\n  }\n\n}\n\nexport default MyConfirmSignUp;<\/code><\/pre><\/div>\n\n\n\n<p>\u6700\u5f8c\u306b\u3001&#8221;App.js&#8221;\u3092\u7de8\u96c6\u3057\u3066\u3001Login\u306e\u72b6\u614b\u3092\u8868\u793a\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-js\" data-lang=\"JavaScript\" data-file=\"App.js\"><code>import React from &#39;react&#39;;\nimport logo from &#39;.\/logo.svg&#39;;\nimport &#39;.\/App.css&#39;;\n\nfunction App(props) {\n\n  if(props.authState === &#39;signedIn&#39;) {\n    return (\n      &lt;div className=&quot;App&quot;&gt;\n        &lt;header className=&quot;App-header&quot;&gt;\n          &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; \/&gt;\n          &lt;p&gt;\n            Edit &lt;code&gt;src\/App.js&lt;\/code&gt; and save to reload.\n          &lt;\/p&gt;\n          &lt;a\n            className=&quot;App-link&quot;\n            href=&quot;https:\/\/reactjs.org&quot;\n            target=&quot;_blank&quot;\n            rel=&quot;noopener noreferrer&quot;\n          &gt;\n            Learn React\n          &lt;\/a&gt;\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  } else if(props.authState === &#39;confirmSignUp&#39;) {\n    return (\n      &lt;div className=&quot;Your account is not granted&quot;&gt;\n        &lt;header className=&quot;App-header&quot;&gt;\n          Your account is not granted.\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  } else if(props.authState === &#39;signIn&#39;) {\n    return (\n      &lt;div className=&quot;Welcome to the world&quot;&gt;\n        &lt;header className=&quot;App-header&quot;&gt;\n          Welcome to the world.\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  } else {\n    return (\n      &lt;div className=&quot;Login failed&quot;&gt;\n        &lt;header className=&quot;App-header&quot;&gt;\n          Login failed.\n        &lt;\/header&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n\nexport default App;<\/code><\/pre><\/div>\n\n\n\n<p>\u4eca\u56de\u306fLocal\u74b0\u5883\u304b\u3089npm start\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"535\" src=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify2-1024x535.png\" alt=\"\" class=\"wp-image-153\" srcset=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify2-1024x535.png 1024w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify2-300x157.png 300w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify2-768x401.png 768w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify2-1200x627.png 1200w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify2.png 1223w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify3-1024x576.png\" alt=\"\" class=\"wp-image-154\" srcset=\"https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify3-1024x576.png 1024w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify3-300x169.png 300w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify3-768x432.png 768w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify3-1200x675.png 1200w, https:\/\/blog.hacya.com\/wp-content\/uploads\/2019\/07\/Amplify3.png 1920w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>Amplify\u306eLogin\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6b21\u56de\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Client\u5074\u306eAmplify\u3092Install\u3057\u307e\u3059\u3002\u74b0\u5883\u306f\u524d\u56de\u300cReact\u3092\u4f7f\u3063\u3066Static\u306aWeb Hosting\u3092\u3059\u308b\u3002\u300d\u3067\u4f7f\u7528\u3057\u305f\u74b0\u5883\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002npm install &#8211;save aws-amplify  &hellip; <a href=\"https:\/\/blog.hacya.com\/index.php\/2019\/07\/11\/cognitoamplifyreact3\/\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Cognito\u30e6\u30fc\u30b6\u30fc\u8a8d\u8a3c\u3092Amplify\u3068React\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3059\u308b\u3002(\u305d\u306e3)&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[14,4,13,7],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-aws","tag-amplify","tag-aws","tag-cognito","tag-react"],"_links":{"self":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":10,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":260,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/posts\/147\/revisions\/260"}],"wp:attachment":[{"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hacya.com\/index.php\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}