成功登录后,我无法进入登录页面将我重定向到React Admin dahsboard
我的authProvider.js看起来像这样:
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_CHECK, AUTH_ERROR } from 'react-admin';
import axios from 'axios';
import { print } from 'graphql';
import gql from 'graphql-tag';
import { httpUri } from './ServerUri';
import {
setSessionCookie,
getSessionCookie,
flushCookie,
} from './SessionCookie';
const ADMIN_LOGIN = gql`
mutation adminLogin($email: String!, $password: String!) {
adminLogin(email: $email, password: $password) {
administrator {
id
}
token
}
}
`;
export default async (type, params) => {
if (type === AUTH_LOGIN) {
const { username, password } = params;
axios
.post(httpUri, {
query: print(ADMIN_LOGIN),
variables: {
email: String(username),
password: String(password),
},
})
.then(res => {
const { token } = res.data.data.adminLogin;
if (token) {
setSessionCookie(token);
}
})
.catch(e => Promise.reject(e))
.then(() => Promise.resolve())
}
if (type === AUTH_CHECK) {
return getSessionCookie() ? Promise.resolve() : Promise.reject();
}
....
return Promise.resolve();
};
和我的sessionCookie.js看起来像这样:
const SESSION_COOKIE_NAME = 'ADMIN-SESSION';
const DAYS_LIMIT = 1;
const getSessionCookie = () => {
const match = `; ${document.cookie}`.match(
`;\\s*${SESSION_COOKIE_NAME}=([^;]+)`
);
return match && match[1];
};
const setSessionCookie = value => {
if (getSessionCookie()) {
document.cookie = `${SESSION_COOKIE_NAME}=; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
}
const date = new Date();
date.setTime(date.getTime() + DAYS_LIMIT * 24 * 60 * 60 * 1000);
const expires = `; expires=${date.toUTCString()}`;
document.cookie = `${SESSION_COOKIE_NAME}=${value || ''}${expires}; path=/`;
return document.cookie
};
const flushCookie = () => {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i];
const eqPos = cookie.indexOf('=');
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
}
};
export { getSessionCookie, setSessionCookie, flushCookie };
这种行为对我来说很奇怪,因为当我单击登录时,我可以看到Cookie设置了。但是React-Admin仍然停留在登录页面上。 不过,如果我第二次单击,我会进入仪表板:/
有人知道吗?我是否有可能无法以同步方式获取cookie,从而阻止了首次加载?