fix: jump to the previous url after logging in

This commit is contained in:
chenos 2022-04-21 11:56:53 +08:00
parent 45d4a9f242
commit be235786d0
2 changed files with 9 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import { Spin } from 'antd'; import { Spin } from 'antd';
import React, { createContext, useContext } from 'react'; import React, { createContext, useContext } from 'react';
import { Redirect } from 'react-router-dom'; import { Redirect, useLocation } from 'react-router-dom';
import { useRequest } from '../api-client'; import { useRequest } from '../api-client';
export const CurrentUserContext = createContext(null); export const CurrentUserContext = createContext(null);
@ -10,14 +10,17 @@ export const useCurrentUserContext = () => {
} }
export const CurrentUserProvider = (props) => { export const CurrentUserProvider = (props) => {
const location = useLocation();
const result = useRequest({ const result = useRequest({
url: 'users:check', url: 'users:check',
}); });
if (result.loading) { if (result.loading) {
return <Spin />; return <Spin />;
} }
const { pathname, search } = location;
let redirect = `?redirect=${pathname}${search}`;
if (!result?.data?.data?.id) { if (!result?.data?.data?.id) {
return <Redirect to={'/signin'} />; return <Redirect to={`/signin${redirect}`} />;
} }
return <CurrentUserContext.Provider value={result}>{props.children}</CurrentUserContext.Provider>; return <CurrentUserContext.Provider value={result}>{props.children}</CurrentUserContext.Provider>;
}; };

View File

@ -1,7 +1,7 @@
import { ISchema, useForm } from '@formily/react'; import { ISchema, useForm } from '@formily/react';
import { uid } from '@formily/shared'; import { uid } from '@formily/shared';
import React from 'react'; import React from 'react';
import { useHistory } from 'react-router-dom'; import { useHistory, useLocation } from 'react-router-dom';
import { SchemaComponent, useAPIClient, useCurrentDocumentTitle, useSystemSettings } from '..'; import { SchemaComponent, useAPIClient, useCurrentDocumentTitle, useSystemSettings } from '..';
const schema: ISchema = { const schema: ISchema = {
@ -60,9 +60,11 @@ const schema: ISchema = {
}; };
const useSignin = () => { const useSignin = () => {
const location = useLocation<any>();
const history = useHistory(); const history = useHistory();
const form = useForm(); const form = useForm();
const api = useAPIClient(); const api = useAPIClient();
const redirect = location?.['query']?.redirect;
return { return {
async run() { async run() {
await form.submit(); await form.submit();
@ -71,7 +73,7 @@ const useSignin = () => {
}); });
if (response?.data?.data?.token) { if (response?.data?.data?.token) {
api.setBearerToken(response?.data?.data?.token); api.setBearerToken(response?.data?.data?.token);
history.push('/admin'); history.push(redirect || '/admin');
} }
}, },
}; };