Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
Handle login with JWT auth in reactjs

im trying to handle login with JWT auth and axios in my reactjs app; but everytime i send my data into the backend i get "'data' is not defined no-undef" error...

this is my handle submit code in Login.js:

class Login extends Component{
constructor(props) {
    super(props);
    this.state = {username: "", password: ""};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
    this.setState({[event.target.name]: event.target.value});
}

handleSubmit(event) {
    event.preventDefault();
    try {
        const response = axiosInstance.post('/token/obtain/', {
            username: this.state.username,
            password: this.state.password
        });
        axiosInstance.defaults.headers['Authorization'] = "Bearer " + response.data.access;
        localStorage.setItem('access_token', response.data.access);
        localStorage.setItem('refresh_token', response.data.refresh);
        return data;
    } catch (error) {
        throw error;
    }
}

render() {
    return (...

and this in my axiosInstance file :

const axiosInstance = axios.create({
baseURL: 'http://127.0.0.1:8000/',
timeout: 5000,
headers: {
    'Authorization': "Bearer " + localStorage.getItem('access_token'),
    'Content-Type': 'application/json',
    'accept': 'application/json'
}
});

how can in handle this error "'data' is not defined no-undef" ????

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you are missing await

... await axios.post(...

Or you can handle like this

    axios.post(...).then(response => { //response.data.access can be found here } 

But of course you better check documentation of axios and check what a Promise is

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda