Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 handleSubmitcallback.

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

If you have yarn package manager installed then run npm start.

Open localhost:3000 in your browser to see basic react app in action.

Now that we are done with setup process let’s see the below example code-

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 (
    
"container">
      
"row justify-content-md-center">
        
"card">
          
"card-header">Login
          
"card-body">
            
              
"form-group">
                
                "email" className="form-control" aria-describedby="emailHelp" placeholder="Enter email" autoFocus
                  value={email} onChange={e => setEmail(e.target.value)} />
              
              
"form-group">
                
                "password" className="form-control" placeholder="Password" value={password}
                  onChange={e => setPassword(e.target.value)} />
              
              
            
          
        
      
    
  )
}

The login result page looks like -


This post first appeared on Programming, please read the originial post: here

Share the post

React Login Authentication Example | React Login Template

×

Subscribe to Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×