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
packages/core/client/src/user

View File

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

View File

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