React-admin登录后未重定向

问题描述 投票:1回答:1

成功登录后,我无法在登录页面上将我重定向到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,从而阻止了首次加载?

reactjs react-admin admin-on-rest
1个回答
0
投票

您能否提供更多代码?像您的路由器和主要组件一样?

祝您有美好的一天:)

© www.soinside.com 2019 - 2024. All rights reserved.