Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 27, 2020 11:44 pm GMT

How to add Authentication to your Reactapp using AuthUI

When starting a new project, it takes some effort to implement Login / Sign-up screens. We often have to repeat the same implementation again and again.

  • Authentication should be simple to remove friction to build an MVP (Most Viable Product) or get users started.
  • Authentication logic should be reused to save development time.

There are a few online services that let you integrate your application with their Authentication APIs. But it doesn't really save much time when we have to go through long documentation to understand it, then spend more time learning and integrating.

An Authentication Service should be simple to integrate with. Below is an example:

Step 1-Import and use Login Component

$ npm install authui-uiimport LoginBox from "authui-ui/dist/js/components/LoginBox/LoginBox";<LoginBox accountId="MyProductName" afterSubmit={afterSubmitHandler} />

LoginBox source code can also be customized and used directly.

Step 2-Handle response

After user submitted the form (Sign up or Log in), the "afterSubmit" callback function will be called. Here you can handle the response (full User object) by redirecting user to the home page, dashboard, etc.

const afterSubmitHandler = (jwtData) => {  if (jwtData && jwtData.email) {    alert('jwtData: ' + JSON.stringify(jwtData))  }}

jwtData is the user object which has properties like: user uuid, accessToken, email, name, picUrl, etc. which can be used in the app (or store in localStorage).

That's it! There is no Step 3. In the next article, I will write more about how it works. In the meantime, there is more information in the links below:


Original Link: https://dev.to/ngduc/how-to-add-authentication-to-your-react-app-3c9c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To