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

187
Views
How can I store my JWT token in userInfo?

I try a lot of things but I can´t go on .. I stucked ... If I login in to my account it works I get also my userInfo but only userID and my password.. but I would have my token also ..

I wrote an if state where I catch my token but I want to set it also in the localStorage and I don´t know how to do it..

export const login = (userID, password) => async (dispatch) => {
    try {
        dispatch({ type: USER_LOGIN_REQUEST });
       
        const url = "http://localhost:8080/authenticate/";
        const config = {
          auth: {
            username: userID,
            password,
          },
        };
    
        const data = {};
        const response = await axios.post(
            url, 
            data, 
            config,
        )

        dispatch({ type: USER_LOGIN_SUCCESS, payload: config});
        //localStorage.setItem("userInfo", JSON.stringify(config) );
 
        if (response.status === 200) {
          // Login succeeded
          const token = response.data.token;
          console.log("TOKEN\n" + token);
          localStorage.setItem("userInfo", JSON.stringify(config) );
        }
      } catch (error) {
        //alert("Sorry, login failed");
        dispatch({
            type: USER_LOGIN_FAIL,
            payload:
                error.response && error.response.data.ErrorMessage
                    ? error.response.data.ErrorMessage
                    : error.message,
        });
      }
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try this

export const login = (userID, password) => async (dispatch) => {
    try {
        dispatch({ type: USER_LOGIN_REQUEST });
       
        const url = "http://localhost:8080/authenticate/";
        const config = {
          auth: {
            username: userID,
            password, //this should not be saved in your local storage delete this from here
          },
        };
    
        const data = {};
        const response = await axios.post(
            url, 
            data, 
            config,
        )

        dispatch({ type: USER_LOGIN_SUCCESS, payload: config});
 
        if (response.status === 200) {
          // Login succeeded
          config.token = response.data.token;
          
        }
        localStorage.setItem("userInfo", JSON.stringify(config) );
      } catch (error) {
        //alert("Sorry, login failed");
        dispatch({
            type: USER_LOGIN_FAIL,
            payload:
                error.response && error.response.data.ErrorMessage
                    ? error.response.data.ErrorMessage
                    : error.message,
        });
      }
};
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!