I'm calling a HTTP API to login in the AUTH_LOGIN using axios which returns a promise. Before the API call is complete the AUTH_CHECK gets called and fails. Is it possible for AUTH_CHECK to be triggered only after the http call completes? Please find below my authprovider
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_CHECK } from 'react-admin'
import { login, logout, isLoggedIn, isLoginComplete, isLoginStarted } from '../services/auth'
export default (type, params) => {
// called when the user attempts to log in
if (type === AUTH_LOGIN) {
const { username, password } = params
login(username, password)
return Promise.resolve()
}
// called when the user clicks on the logout button
if (type === AUTH_LOGOUT) {
logout()
return Promise.resolve()
}
// called when the API returns an error
if (type === AUTH_ERROR) {
const { status } = params
if (status === 401 || status === 403) {
logout()
return Promise.reject()
}
return Promise.resolve()
}
// called when the user navigates to a new location
if (type === AUTH_CHECK) {
// return isLoggedIn() ? Promise.resolve() : Promise.reject()
return isLoggedIn() ? Promise.resolve() : Promise.reject()
}
return Promise.reject('Unknown method')
}
The login method:
import axios from 'axios'
export function login(username, password) {
logout()
startLogin()
axios({
method: 'post',
url: '/auth/login',
auth: {
username: username,
password: password
},
})
.then(function (response) {
let token = response.headers['jwt-token']
setToken(token)
})
.catch(function (error) {
logout()
});
}
export function logout() {
localStorage.removeItem('token')
}
export function isLoggedIn() {
return localStorage.getItem('token') ? true:false
}
export function getToken() {
localStorage.getItem('token')
}
export function setToken(token) {
localStorage.setItem('token', token)
}
I was able to fix it by using async / await.
auth provider:
Login method: import axios from 'axios'