This commit is contained in:
felinae98
2021-09-24 15:48:53 +08:00
parent 31c5e283ba
commit dce58580f2
16 changed files with 370 additions and 20 deletions
+27
View File
@@ -0,0 +1,27 @@
import React, {useContext, useEffect, useState} from "react";
import { useParams } from "react-router";
import { auth } from '../api/config';
import { LoginContext } from '../utils/context';
import { Redirect } from 'react-router-dom'
interface AuthParam {
code: string
}
export function Auth() {
const { code } = useParams<AuthParam>();
const [ content, contentUpdate ] = useState(<div>Logining...</div>);
const { save } = useContext(LoginContext);
useEffect(() => {
const loginFun = async () => {
const resp = await auth(code);
if (resp.status === 200) {
save({login: true, type: resp.type, name: resp.name, id: resp.id, token: resp.token});
contentUpdate(_ => <Redirect to={{pathname: '/admin'}} />);
sessionStorage.setItem('token', resp.token);
} else {
contentUpdate(_ => <div></div>);
}
}
loginFun();
}, [])
return content;
}