fix bug and logout guest

This commit is contained in:
monoid 2021-01-19 18:11:25 +09:00
parent df10984957
commit 50ce70435a
3 changed files with 57 additions and 21 deletions

View File

@ -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);
}

View File

@ -13,14 +13,14 @@ type LoginLocalStorage = {
refreshExpired: number
};
let localObj: LoginLocalStorage|null = null;
let localObj: LoginLocalStorage | null = null;
export const getInitialValue = async () => {
if(localObj === null){
if (localObj === null) {
const storagestr = window.localStorage.getItem("UserLoginContext") as string | null;
const storage = storagestr !== null ? JSON.parse(storagestr) as LoginLocalStorage | null : null;
localObj = storage;
}
}
if (localObj !== null && localObj.refreshExpired > Math.floor(Date.now() / 1000)) {
return {
username: localObj.username,
@ -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',
export const doLogout = async () => {
const req = await fetch('/user/logout', {
method: 'POST'
});
await res.json();
localObj = {
refreshExpired: 0,
username:"",
permission:[]
try {
const res = await req.json();
localObj = {
refreshExpired: 0,
username: "",
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;
}

View File

@ -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) =>