React authenticate login

React Login Authentication Example | React Login Template

React is very popular front-end framework from facebook for creating single page apps. Let's start by creating the basic login form; users can login with their credentials in our react application. Before accessing the app you asked to sign-in with their email and password in the apps.

You should check out the React Docs.

Some noted points for the below example -
1.      The useState gives you the current value of the variable you want to store in the state and a function to set the new value.
2.      The setEmail and setPassword functions to store what the user types in — e.target.value.
3.      The autoFocus sets focus to email field.
4.      The validateForm function is used to validate the form after click on submit form.
5.      Finally when the form is submitted, we trigger our handleSubmit callback.

First install create-react-app globally -
npm install -g create-react-app

Create new project for in your Documents -
create-react-app logindemo
cd logindemo
npm start

Open localhost:3000 in your browser to see basic react app in action. Let’s see the below example.

As an Example,
Create a new file src/Login.js and add the following :-

import React, { useState } from "react";
import { Button } from "react-bootstrap";

export default function Login(props) {
  const [emailsetEmail] = useState('');
  const [passwordsetPassword] = useState('');

  function validateForm() {
    return email.length > 0 && password.length > 0;
  }

  function handleSubmit(event) {
    event.preventDefault();

    //Validate Your email and Password from API call
    //If everything id ok, redirect to user dashboard otherwise redirect to login page.
    if (authenticateUser(emailpassword){
      window.localStorage.setItem('token', true);
      props.history.push("/user/dashboard");
    }
    else {
      props.history.push("/login");
    }
  }

  return (
    <div className="container">
      <div className="row justify-content-md-center">
        <div className="card">
          <div className="card-header">Login</div>
          <div className="card-body">
            <form>
              <div className="form-group">
                <label for="exampleInputEmail1">Email address</label>
                <input type="email" className="form-control" aria-describedby="emailHelp" placeholder="Enter email" autoFocus
                  value={email} onChange={e => setEmail(e.target.value)} />
              </div>
              <div className="form-group">
                <label for="exampleInputPassword1">Password</label>
                <input type="password" className="form-control" placeholder="Password" value={password}
                  onChange={e => setPassword(e.target.value)} />
              </div>
              <Button block bsSize="large" disabled={!validateForm()} onClick={handleSubmit} type="submit">Login</Button>
            </form>
          </div>
        </div>
      </div>
    </div>
  )
}

The login result page looks like -
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^