0

I am beginner with token authorization and I am trying to create login system with react. The endpoints seem to require the token to be accessed, so I am redirected to /home, which is currently not happening. I am using this api https://app.swaggerhub.com/apis/warp/etn-device-checker-test/1.0#/default/post_login

Login.js

import React from 'react'
import axios from 'axios';
import { useState } from 'react';
import { Redirect } from "react-router-dom";
function Login() {
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    
   
  
    const onSubmit = (e) => {
      e.preventDefault();
      const getIn = {
      
        "login":email,
        "password":password,
        
    
      };
  
      axios
        .post('https://js-test-api.etnetera.cz/api/v1/login', getIn,
        {
            headers: {
                authorization: 'Auth-Token',
                 'content-type': 'application/json', 
           }
        })
        .then((res) => {
          console.log(res.data); 
          return <Redirect to="/home" />
         
        })
        .catch((error) => console.log(error));
       
    };
    return (
        <div>
           <form >
         <label>email</label> <input value={email}
          onChange={(e) => setEmail(e.target.value)} type="text"/>
        <label>password</label>  <input type="text" value={password}
          onChange={(e) => setPassword(e.target.value)}/>
        <button onClick={onSubmit}>login</button>
           </form>
        </div>
    )
}

export default Login

App.js

import './App.css';
import Login from './Login';
import MobileList from './mobileList';
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
function App() {
  return (
    <div className="App">
 <Router>
 <Switch>
 
<Route>
<Login path='/' exact/>
</Route>
<Route>
 <MobileList path='/home' />
 </Route>
</Switch>
 </Router>
 
    </div>
  );
}

export default App;
jenlee123
  • 133
  • 1
  • 10

1 Answers1

0

headers: {"Authorization" : `Bearer ${token}`}

Maybe this answer will help you. how-to-pass-header-jwt-token-with-axios-react

But in login I don't think that you need a token, so you can remove the authorization part there.

Josef Wittmann
  • 1,259
  • 9
  • 16
Betzalel
  • 21
  • 5