Skip to main content

Login API with Token in reactjs

Login API with Token in reactjs 



import React, { useState } from 'react'

import { Link, useNavigate } from 'react-router-dom'
import axios from 'axios'
import {
  CButton,
  CCard,
  CCardBody,
  CCardGroup,
  CCol,
  CContainer,
  CForm,
  CFormInput,
  CInputGroup,
  CInputGroupText,
  CRow,
} from '@coreui/react'
import CIcon from '@coreui/icons-react'
import { cilLockLocked, cilUser } from '@coreui/icons'
import { setUserSession } from 'src/Utils/Common'
// import { response } from 'express'

const Login = (props) => {
  const navigate = useNavigate()
  const [MailID, setMailID] = useState('')
  const [Password, setPassword] = useState('')
  const [error, setError] = useState(null)
  const [loading, setLoading] = useState(false)

  const handleLogin = () => {
    setError(null)
    setLoading(true)
    axios
      .post('http://164.92.124.108/api/login', {
        MailID: MailID,
        Password: Password,
      })
      .then((response) => {
        setLoading(false)
        console.log('response', response)
        // setUserSession(response.data)
        if (response.data.status === true) {
          localStorage.setItem('@token', response.data.token)
          navigate('/client')
        } else {
          alert(response.data.message)
        }
      })
      .catch((error) => {
        setLoading(false)
        console.error(error)
      })
    // console.log(MailID, Password)
    // const item = { MailID, Password }
    // let result = await fetch('http://164.92.124.108/api/login', {
    //   method: 'POST',
    //   headers: {
    //     'Content-Type': 'application/json',
    //     Accept: 'application/json',
    //   },
    //   body: JSON.stringify(item),
    // })
    // result = await result.json()
    // console.log(result)
    // localStorage.setItem('name', JSON.stringify(result))
    //
  }
  return (
    <div className="bg-light min-vh-100 d-flex flex-row align-items-center">
      <CContainer>
        <CRow className="justify-content-center">
          <CCol md={8}>
            <CCardGroup>
              <CCard className="p-4">
                <CCardBody>
                  <CForm>
                    <h1>Login</h1>
                    <p className="text-medium-emphasis">Sign In to your account</p>
                    <CInputGroup className="mb-3">
                      <CInputGroupText>
                        <CIcon icon={cilUser} />
                      </CInputGroupText>
                      <CFormInput
                        placeholder="Username"
                        value={MailID}
                        onChange={(e) => setMailID(e.target.value)}
                        type="text"
                      />
                    </CInputGroup>
                    <CInputGroup className="mb-4">
                      <CInputGroupText>
                        <CIcon icon={cilLockLocked} />
                      </CInputGroupText>
                      <CFormInput
                        type="password"
                        placeholder="Password"
                        value={Password}
                        onChange={(e) => setPassword(e.target.value)}
                      />
                    </CInputGroup>
                    {error && <p>{error}</p>}
                    <CRow>
                      <CCol xs={6}>
                        {/* <CButton color="primary" className="px-4" onClick={log}>
                          Login
                        </CButton> */}
                        <input type="button" value={'login'} onClick={handleLogin} />
                      </CCol>
                      <CCol xs={6} className="text-right">
                        <CButton color="link" className="px-0">
                          Forgot password?
                        </CButton>
                      </CCol>
                    </CRow>
                  </CForm>
                </CCardBody>
              </CCard>
              <CCard className="text-white bg-primary py-5" style={{ width: '44%' }}>
                <CCardBody className="text-center">
                  <div>
                    <h2>Sign up</h2>
                    <p>
                      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                      tempor incididunt ut labore et dolore magna aliqua.
                    </p>
                    <Link to="/register">
                      <CButton color="primary" className="mt-3" active tabIndex={-1}>
                        Register Now!
                      </CButton>
                    </Link>
                  </div>
                </CCardBody>
              </CCard>
            </CCardGroup>
          </CCol>
        </CRow>
      </CContainer>
    </div>
  )
}

export default Login

Comments

Popular posts from this blog

Santa Claus Village : Finland

Santa Claus Village is a year-round enchanting destination that embodies the spirit of Christmas and is situated in Rovaniemi, Finland. This charming community, which is tucked away in the Arctic Circle and is recognized as Santa Claus' official hometown, is a must-visit location for both families and Christmas lovers. Experience the magic and delight of the holidays in a charming winter wonderland setting at Santa Claus Village. The town has a quaint feel right out of a Christmas novel thanks to its festive décor, sparkling lights, and quaint houses. Meeting Santa Claus himself is one of the pleasures of Santa Claus Village. Guests can see Santa in his jovial Santa Claus Office, where he extends a warm welcome to everyone worldwide. Kids can snap pictures, tell Santa what they want for Christmas, and get a unique memento to remember their visit. There are many of other things to do in the hamlet besides visiting Santa Claus. In addition to sending letters and postcards stamped wit...

Pakistan: Entire History, Culture, and Gorgeous Beauty

  Pakistan, which is tucked away in the center of South Asia, is a culturally and historically diverse nation with a wealth of natural beauty. Pakistan is an enthralling fusion of heritage and modernity, encompassing the ancient ruins of Mohenjo-Daro and the beautiful peaks of the Himalayas. Come with me as we set off to discover the splendors of this captivating country. Cultural Heritage Pakistan's geography is as varied as its cultural legacy. The Indus Valley Civilization, the Mughals, and the British Raj are just a few of the civilizations that have shaped the region over its 5,000-year history. Immerse yourself in the lively Lahore bazaars, where the sounds of centuries-old customs continue to resonate. Admire the exquisite workmanship of bygone centuries as you explore the elaborate architecture of Lahore Fort and Badshahi Mosque. Visit the shrines of well-known saints, such as Data Ganj Bakhsh and Lal Shahbaz Qalandar, where followers congregate to find spiritual comfort, t...

React JS Interview Q & A

1. What is DOCTYPE in HTML? DOCTYPE is an instruction that is used in the beginning of an HTML or XHTML document to inform a web browser about the version and type of HTML or XHTML being used. It is not an HTML tag; it is an "information" to the browser about what document type to expect. It is important to include the DOCTYPE in your HTML documents in order for the browser to correctly render the content of your web page. 2.What are the new HTML5 features? Semantic Elements: HTML5 introduces new semantic elements like <header>, <nav>, <article>, <section>, <aside>, <figure> and <figcaption>, which provide additional meaning to the content of a web page. Audio and Video Support: HTML5 includes built-in support for audio and video, eliminating the need for third-party plugins like Flash. Canvas: HTML5 introduces the <canvas> element, which allows for dynamic, scriptable rendering of 2D shapes and bitmap images. Web Storage: HTML5...