From 50ce70435a6af6ca3aae3a9cf1d41a80657553ba Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 19 Jan 2021 18:11:25 +0900 Subject: [PATCH] fix bug and logout guest --- src/client/page/login.tsx | 13 ++++----- src/client/state.tsx | 60 +++++++++++++++++++++++++++++++-------- src/login.ts | 5 +++- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/client/page/login.tsx b/src/client/page/login.tsx index 08458b4..66d8ca0 100644 --- a/src/client/page/login.tsx +++ b/src/client/page/login.tsx @@ -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); } diff --git a/src/client/state.tsx b/src/client/state.tsx index 2e20d78..b16d4e8 100644 --- a/src/client/state.tsx +++ b/src/client/state.tsx @@ -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=>{ + 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; } \ No newline at end of file diff --git a/src/login.ts b/src/login.ts index 40ceb87..cebdd65 100644 --- a/src/login.ts +++ b/src/login.ts @@ -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) =>