Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!