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'; DialogTitle, MenuList, Paper, TextField, Typography, useTheme } from '@material-ui/core';
import { UserContext } from '../state'; import { UserContext } from '../state';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import {doLogin as doSessionLogin} from '../state';
export const LoginPage = ()=>{ export const LoginPage = ()=>{
const theme = useTheme(); const theme = useTheme();
@ -15,17 +16,13 @@ export const LoginPage = ()=>{
setOpenDialog({...openDialog,open:false}); setOpenDialog({...openDialog,open:false});
} }
const doLogin = async ()=>{ const doLogin = async ()=>{
const res = await fetch('/user/login',{
method:'POST',
body:JSON.stringify(userLoginInfo),
headers:{"content-type":"application/json"}
});
try{ try{
const b = await res.json(); const b = await doSessionLogin(userLoginInfo);
if(res.status !== 200){ if(typeof b === "string"){
setOpenDialog({open:true,message: b.detail}); setOpenDialog({open:true,message: b});
return; return;
} }
console.log(`login as ${b.username}`);
setUsername(b.username); setUsername(b.username);
setPermission(b.permission); setPermission(b.permission);
} }

View File

@ -13,10 +13,10 @@ type LoginLocalStorage = {
refreshExpired: number refreshExpired: number
}; };
let localObj: LoginLocalStorage|null = null; let localObj: LoginLocalStorage | null = null;
export const getInitialValue = async () => { export const getInitialValue = async () => {
if(localObj === null){ if (localObj === null) {
const storagestr = window.localStorage.getItem("UserLoginContext") as string | null; const storagestr = window.localStorage.getItem("UserLoginContext") as string | null;
const storage = storagestr !== null ? JSON.parse(storagestr) as LoginLocalStorage | null : null; const storage = storagestr !== null ? JSON.parse(storagestr) as LoginLocalStorage | null : null;
localObj = storage; localObj = storage;
@ -38,22 +38,58 @@ export const getInitialValue = async () => {
permission: r.permission, permission: r.permission,
refreshExpired: r.refreshExpired 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 { return {
username: r.username, username: r.username,
permission: r.permission permission: r.permission
} }
} }
export const doLogout = async ()=>{ export const doLogout = async () => {
const res = await fetch('/user/logout', { const req = await fetch('/user/logout', {
method: 'POST', method: 'POST'
}); });
await res.json(); try {
const res = await req.json();
localObj = { localObj = {
refreshExpired: 0, refreshExpired: 0,
username:"", username: "",
permission:[] permission: res["permission"]
} }
window.localStorage.setItem("UserLoginContext", JSON.stringify(localObj)); 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) => { export const LogoutMiddleware = (ctx: Koa.Context, next: Koa.Next) => {
const setting = get_setting()
ctx.cookies.set(accessTokenName, null); ctx.cookies.set(accessTokenName, null);
ctx.cookies.set(refreshTokenName, null); ctx.cookies.set(refreshTokenName, null);
ctx.body = { ok: true }; ctx.body = { ok: true,
username: "",
permission: setting.guest };
return; return;
}; };
export const createUserMiddleWare = (userController: UserAccessor) => export const createUserMiddleWare = (userController: UserAccessor) =>