Answer the question
In order to leave comments, you need to log in
How to combine 2 requests to React server?
Hello! there is a request to the /auth server that should receive a token from it, but I need this request to be in /login, I don’t quite understand how to combine 2 requests, I will be glad for any help, I’m new to react, thanks, here is the code :
export const registration = async (email, password) => {
try {
const response = await axios.post(`http://localhost:5000/api/auth/registration`, {
email,
password
})
alert(response.data.message)
} catch (e) {
alert(e.response.data.message)
}
}
export const login = (email, password) => {
return async dispatch => {
try {
const response = await axios.post(`http://localhost:5000/api/auth/login`, {
email,
password
})
dispatch(setUser(response.data.user))
localStorage.setItem('token', response.data.token)
} catch (e) {
alert(e.response.data.message)
}
}
}
export const auth = () => {
return async dispatch => {
try {
const response = await axios.get(`http://localhost:5000/api/auth/auth`,
{headers:{Authorization:`Bearer ${localStorage.getItem('token')}`}}
)
dispatch(setUser(response.data.user))
localStorage.setItem('token', response.data.token)
} catch (e) {
alert(e.response.data.message)
localStorage.removeItem('token')
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question