fix: jump to the previous url after logging in
This commit is contained in:
parent
45d4a9f242
commit
be235786d0
packages/core/client/src/user
@ -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>;
|
||||
};
|
||||
|
@ -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');
|
||||
}
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user