avatarmim Armand

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3258

Abstract

changes (added lines are <b>highlighted</b>) or you can simply replace the content of this file with the provided code, here we are using a <a href="https://reactjs.org/docs/higher-order-components.html">HOC</a> to easily add all the necessary dialogues (there are other ways to do this too); you also see that we are using <b>AmplifySignOut</b> component to let the users sign-out once they are logged in, you can change the layout and styling of all the component as you wish:</li></ul><div id="26aa"><pre><span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.svg'</span>; <span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>; <span class="hljs-keyword">import</span> <span class="hljs-title class_">React</span> <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>; <span class="hljs-keyword">import</span> { withAuthenticator, <span class="hljs-title class_">AmplifySignOut</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'@aws-amplify/ui-react'</span>; <span class="hljs-keyword">import</span> <span class="hljs-title class_">Amplify</span> <span class="hljs-keyword">from</span> <span class="hljs-string">"aws-amplify"</span>;

<span class="hljs-title class_">Amplify</span>.<span class="hljs-title function_">configure</span>({ <span class="hljs-title class_">Auth</span>: { <span class="hljs-attr">mandatorySignIn</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">'us-east-1'</span>, <span class="hljs-attr">userPoolId</span>: <span class="hljs-string">'CHANGE ME 2 THE VALUE FROM STEP 1!'</span>, <span class="hljs-attr">userPoolWebClientId</span>: <span class="hljs-string">'CHANGE ME 2 THE VALUE FROM STEP 1!'</span> } })

<span class="hljs-keyword">function</span> <span class="hljs-title function_">App</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> ( <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>></span> <span class="hljs-tag"><<span class="hljs-name">AmplifySignOut</span>/></span> <span class="hljs-tag"><<span class="hljs-name">header</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-header"</span>></span> <span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App-logo"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"logo"</span> /></span> <span class="hljs-tag"><<span class="hljs-name">p</span>></span> Edit <span class="hljs-tag"><<span class="hljs-name">code</span>></span>src/App.js<span class="hljs-tag"></<span class="hljs-name">code</span>></span> and save to reload. <span class="hljs-tag"></<span class="hljs-name">p</span>></span> <span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">cla

Options

ssName</span>=<span class="hljs-string">"App-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://reactjs.org"</span> <span class="hljs-attr">target</span>=<span class="hljs-string">"_blank"</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"noopener noreferrer"</span> ></span> Learn React <span class="hljs-tag"></<span class="hljs-name">a</span>></span> <span class="hljs-tag"></<span class="hljs-name">header</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span> ); }

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title function_">withAuthenticator</span>(<span class="hljs-title class_">App</span>);</pre></div><h1 id="9ff2">And that’s it!</h1><p id="42a7">For real! run the project ( <code>npm start</code> ) and create a user and login and test everything! You have all the needed user-management components and mechanisms added to your app, as easy as that! Plus, from now on, all your API calls can easily carry the authentication tokens and cookies that you can use to authenticate the user and protect your APIs ( we can cover those in a separate article, let me know if you are interested! ). Of course, you can continue to <a href="https://docs.amplify.aws/ui/auth/authenticator/q/framework/react">add more</a>; for instance, if you want to have access to the user and authentication states ( like user-name, email, etc.) in your Front-End app, you can easily do that too ( Interested to learn more? lmk! )</p><figure id="09a9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*lRFl6oCpz6j8qZFlWhZ1mA.png"><figcaption></figcaption></figure><h1 id="e3c4">Get in touch!</h1><p id="4aa9">I’m always happy to connect with others interested in new technologies like myself, so please don’t hesitate to get in touch and start a conversation! <b>Connect</b> with me on <a href="https://www.linkedin.com/in/3dots/"><b>LinkedIn</b>.</a> <b>Follow</b> me on <a href="https://twitter.com/mim_Armand"><b>Twitter</b></a><b>.</b></p><p id="a6f5">Take care,

  • mim</p><figure id="af48"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*t_MOgMTr_f1N4qHN_R7KVA.png"><figcaption></figcaption></figure><p id="3652">Join FAUN: <a href="https://faun.to/i9Pt9"><b>Website</b></a><b> </b>💻<b>|<a href="https://faun.dev/podcast">Podcast</a> </b>🎙️<b>|<a href="https://twitter.com/joinfaun">Twitter</a> </b>🐦<b>|<a href="https://www.facebook.com/faun.dev/">Facebook</a> </b>👥<b>|<a href="https://instagram.com/fauncommunity/">Instagram</a> </b>📷|<a href="https://www.facebook.com/groups/364904580892967/"><b>Facebook Group</b></a><b> </b>🗣️<b>|<a href="https://www.linkedin.com/company/faundev">Linkedin Group</a> </b>💬<b>|</b> <a href="https://faun.dev/chat"><b>Slack</b></a> 📱<b>|<a href="https://thechief.io">Cloud Native</a></b><a href="https://thechief.io"> <b>News</b></a><b> </b>📰<b>|<a href="https://linktr.ee/faun.dev/">More</a>.</b></p><p id="3062"><b>If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇</b></p></article></body>

Serverless User management system for React.

Complete React User Mgmt System with AWS Cognito

Dead simple, ready out of the box, User management system, including UI pages and components for React!

Trust me! It’s effortless! Let’s just get to it then!

Pre-requisite:

Let’s get to it!

  • If you haven’t done yet, go to your Cognito User Pool and create a new App Client by selecting your user pool and then select the App Clients on the menu on the left panel. just enter a name and make sure you un-check the checkbox that reads Generate client secret ( you’d need this if you want to authenticate the calls yourself, server-side ), click on create! in the next window, please take note of your App Client ID as you’ll need it soon. You also need the userPoolId which you can find by clicking on the General settings tab on the left panel as well. Take a note of that value too.
  • Create a React App to start ( in your terminal run:npx create-react-app best-app-eva )
  • CD to your newly created project ( cd best-app-eva ) and install these dependencies: npm i -P aws-amplify @aws-amplify/ui-react, don’t be intimidated by the amplify in the dependency name! We are not going to use any fancy features of it ( I’m not a huge fan of them myself! ), we are just going to take advantage of its User-Management UI components, which are awesomely available and ready to be used out of the box, saving you a lot of time and adding a lot of great features for free! Amplify in its entirety can also be pretty awesome for certain projects, but here we will not use most of its core functionalities.
  • Open the file ./src/App.js in your IDE and make the following changes (added lines are highlighted) or you can simply replace the content of this file with the provided code, here we are using a HOC to easily add all the necessary dialogues (there are other ways to do this too); you also see that we are using AmplifySignOut component to let the users sign-out once they are logged in, you can change the layout and styling of all the component as you wish:
import logo from './logo.svg';
import './App.css';
import React from 'react';
import { withAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react';
import Amplify from "aws-amplify";

Amplify.configure({
  Auth: {
    mandatorySignIn: true,
    region: 'us-east-1',
    userPoolId: 'CHANGE ME 2 THE VALUE FROM STEP 1!',
    userPoolWebClientId: 'CHANGE ME 2 THE VALUE FROM STEP 1!'
  }
})

function App() {
  return (
    <div className="App">
      <AmplifySignOut/>
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default withAuthenticator(App);

And that’s it!

For real! run the project ( npm start ) and create a user and login and test everything! You have all the needed user-management components and mechanisms added to your app, as easy as that! Plus, from now on, all your API calls can easily carry the authentication tokens and cookies that you can use to authenticate the user and protect your APIs ( we can cover those in a separate article, let me know if you are interested! ). Of course, you can continue to add more; for instance, if you want to have access to the user and authentication states ( like user-name, email, etc.) in your Front-End app, you can easily do that too ( Interested to learn more? lmk! )

Get in touch!

I’m always happy to connect with others interested in new technologies like myself, so please don’t hesitate to get in touch and start a conversation! Connect with me on LinkedIn. Follow me on Twitter.

Take care, - mim

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

React
AWS
Cloud Computing
Front End Development
JavaScript
Recommended from ReadMedium