添加退出登录功能

This commit is contained in:
whyour 2021-03-23 14:32:17 +08:00
parent d41eb582e4
commit 51b0418fa9
5 changed files with 21 additions and 6 deletions

View File

@ -10,7 +10,7 @@ const route = Router();
export default (app: Router) => { export default (app: Router) => {
app.use('/', route); app.use('/', route);
route.post( route.post(
'/auth', '/login',
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {

View File

@ -13,7 +13,7 @@ export default ({ app }: { app: Application }) => {
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use( app.use(
jwt({ secret: config.secret as string, algorithms: ['HS384'] }).unless({ jwt({ secret: config.secret as string, algorithms: ['HS384'] }).unless({
path: ['/api/auth'], path: ['/api/login'],
}), }),
); );
app.use(config.api.prefix, routes()); app.use(config.api.prefix, routes());

View File

@ -8,11 +8,16 @@ import {
} from 'darkreader'; } from 'darkreader';
import defaultProps from './defaultProps'; import defaultProps from './defaultProps';
import { Link, history } from 'umi'; import { Link, history } from 'umi';
import { LogoutOutlined } from '@ant-design/icons';
import config from '@/utils/config'; import config from '@/utils/config';
import 'codemirror/mode/shell/shell.js'; import 'codemirror/mode/shell/shell.js';
import './index.less'; import './index.less';
export default function (props: any) { export default function (props: any) {
const logout = () => {
localStorage.removeItem(config.authKey);
};
useEffect(() => { useEffect(() => {
const isAuth = localStorage.getItem(config.authKey); const isAuth = localStorage.getItem(config.authKey);
if (!isAuth) { if (!isAuth) {
@ -55,6 +60,16 @@ export default function (props: any) {
} }
return <Link to={menuItemProps.path}>{defaultDom}</Link>; return <Link to={menuItemProps.path}>{defaultDom}</Link>;
}} }}
postMenuData={(menuData) => {
return [
...(menuData || []),
{
icon: <LogoutOutlined />,
name: '退出登录',
onTitleClick: () => logout(),
},
];
}}
{...defaultProps} {...defaultProps}
> >
{props.children} {props.children}

View File

@ -11,7 +11,7 @@ const FormItem = Form.Item;
const Login = () => { const Login = () => {
const handleOk = (values: any) => { const handleOk = (values: any) => {
request request
.post(`${config.apiPrefix}auth`, { .post(`${config.apiPrefix}login`, {
data: { data: {
username: values.username, username: values.username,
password: values.password, password: values.password,

View File

@ -9,6 +9,7 @@ import {
auto as followSystemColorScheme, auto as followSystemColorScheme,
setFetchMethod, setFetchMethod,
} from 'darkreader'; } from 'darkreader';
import { history } from 'umi';
const optionsWithDisabled = [ const optionsWithDisabled = [
{ label: '亮色', value: 'light' }, { label: '亮色', value: 'light' },
@ -34,9 +35,8 @@ const Password = () => {
}, },
}) })
.then((data: any) => { .then((data: any) => {
notification.success({ localStorage.removeItem(config.authKey);
message: data.msg, history.push('/login');
});
}) })
.catch((error: any) => { .catch((error: any) => {
console.log(error); console.log(error);