Intenté muchas cosas pero no puedo continuar... Me atasqué... Si inicio sesión en mi cuenta, funciona, también obtengo mi información de usuario, pero solo mi ID de usuario y mi contraseña... pero también tendría mi token. .
Escribí un estado if donde atrapo mi token, pero quiero configurarlo también en el almacenamiento local y no sé cómo hacerlo.
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, }); } };prueba esto
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, }); } };