Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda