fix bug and logout guest
This commit is contained in:
parent
df10984957
commit
50ce70435a
@ -4,6 +4,7 @@ import { Button, Dialog, DialogActions, DialogContent, DialogContentText,
|
||||
DialogTitle, MenuList, Paper, TextField, Typography, useTheme } from '@material-ui/core';
|
||||
import { UserContext } from '../state';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import {doLogin as doSessionLogin} from '../state';
|
||||
|
||||
export const LoginPage = ()=>{
|
||||
const theme = useTheme();
|
||||
@ -15,17 +16,13 @@ export const LoginPage = ()=>{
|
||||
setOpenDialog({...openDialog,open:false});
|
||||
}
|
||||
const doLogin = async ()=>{
|
||||
const res = await fetch('/user/login',{
|
||||
method:'POST',
|
||||
body:JSON.stringify(userLoginInfo),
|
||||
headers:{"content-type":"application/json"}
|
||||
});
|
||||
try{
|
||||
const b = await res.json();
|
||||
if(res.status !== 200){
|
||||
setOpenDialog({open:true,message: b.detail});
|
||||
const b = await doSessionLogin(userLoginInfo);
|
||||
if(typeof b === "string"){
|
||||
setOpenDialog({open:true,message: b});
|
||||
return;
|
||||
}
|
||||
console.log(`login as ${b.username}`);
|
||||
setUsername(b.username);
|
||||
setPermission(b.permission);
|
||||
}
|
||||
|
@ -38,22 +38,58 @@ export const getInitialValue = async () => {
|
||||
permission: r.permission,
|
||||
refreshExpired: r.refreshExpired
|
||||
}
|
||||
window.localStorage.setItem("UserLoginContext", JSON.stringify(localObj));
|
||||
}
|
||||
else {
|
||||
localObj = {
|
||||
refreshExpired: 0,
|
||||
username: "",
|
||||
permission: r.permission
|
||||
}
|
||||
}
|
||||
window.localStorage.setItem("UserLoginContext", JSON.stringify(localObj));
|
||||
return {
|
||||
username: r.username,
|
||||
permission: r.permission
|
||||
}
|
||||
}
|
||||
export const doLogout = async () => {
|
||||
const res = await fetch('/user/logout', {
|
||||
method: 'POST',
|
||||
const req = await fetch('/user/logout', {
|
||||
method: 'POST'
|
||||
});
|
||||
await res.json();
|
||||
try {
|
||||
const res = await req.json();
|
||||
localObj = {
|
||||
refreshExpired: 0,
|
||||
username: "",
|
||||
permission:[]
|
||||
permission: res["permission"]
|
||||
}
|
||||
window.localStorage.setItem("UserLoginContext", JSON.stringify(localObj));
|
||||
return {
|
||||
username: localObj.username,
|
||||
permission: localObj.permission,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Server Error ${error}`);
|
||||
return {
|
||||
username: "",
|
||||
permission: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
export const doLogin = async (userLoginInfo:{
|
||||
username:string,
|
||||
password:string,
|
||||
}): Promise<string|LoginLocalStorage>=>{
|
||||
const res = await fetch('/user/login',{
|
||||
method:'POST',
|
||||
body:JSON.stringify(userLoginInfo),
|
||||
headers:{"content-type":"application/json"}
|
||||
});
|
||||
const b = await res.json();
|
||||
if(res.status !== 200){
|
||||
return b.detail as string;
|
||||
}
|
||||
localObj = b;
|
||||
window.localStorage.setItem("UserLoginContext", JSON.stringify(localObj));
|
||||
return b;
|
||||
}
|
@ -144,9 +144,12 @@ export const createLoginMiddleware = (userController: UserAccessor) =>
|
||||
};
|
||||
|
||||
export const LogoutMiddleware = (ctx: Koa.Context, next: Koa.Next) => {
|
||||
const setting = get_setting()
|
||||
ctx.cookies.set(accessTokenName, null);
|
||||
ctx.cookies.set(refreshTokenName, null);
|
||||
ctx.body = { ok: true };
|
||||
ctx.body = { ok: true,
|
||||
username: "",
|
||||
permission: setting.guest };
|
||||
return;
|
||||
};
|
||||
export const createUserMiddleWare = (userController: UserAccessor) =>
|
||||
|
Loading…
Reference in New Issue
Block a user