login is a async function function. During development the code works fine but in production promise never leaves pending state.
axios.js
import axios from "axios"
// during development
// const BASE_URL = 'http://localhost:8000/api'
const BASE_URL = 'https://hotel-website-1-9mlw.onrender.com/api'
export default axios.create({
baseURL: BASE_URL
})
login.js
This is the login function which is called on onClick event.
import axios from "../config/axios.js"
export const login = async (userDetails , setAuth,navigate , from ) => {
try {
const response = await axios.post('/login',
JSON.stringify(userDetails),
{
headers: { 'Content-Type ': "application/json" },
withCredentials: true
}
)
if (response?.status === 200) {
const { accessToken, username , email } = response.data
setAuth({ accessToken, username, email })
alert("Login successful!! ")
navigate(from , {replace: true})
}
} catch (error) {
if (!error.response) {
alert("No server response");
console.log(`error message : ${error.message} `);
}
else if (error.response.status === 404) {
alert("Credentials missing.")
}
else if (error.response.status === 401) {
alert(`Unauthorized`)
}
else {
alert("login failed");
console.log('error message: ', error.message)
}
}
}