fix: try to log out even http authentication

Massimo Melina committed Aug 2, 2022 at 19:11 UTC e65105db39230ed2ef700de52d080a092e20d052
5 files changed +11 -8
admin/src/LogoutPage.ts
+2 -3
@@ -18,9 +18,8 @@ export default function LogoutPage() {
18 size: 'large',
19 variant: 'contained',
20 onClick() {
21 - apiCall('logout').then(() =>
22 - apiCall('get_status').catch(()=>0), // second call is supposed to trigger a 401 if login is required
23 - alertDialog) // show errors
21 + apiCall('logout').catch(err => // we expect 401
22 + err.code !== 401 && alertDialog(err))
23 }
24 }, "Yes, I want to logout")
25 )
frontend/src/UserPanel.ts
+1 -1
@@ -45,7 +45,7 @@ function Content() {
45 icon: 'logout',
46 label: "Logout",
47 onClick() {
48 - logout().then(closeDialog)
48 + logout().then(closeDialog, alertDialog)
49 }
50 })
51 )
frontend/src/login.ts
+5 -2
@@ -41,7 +41,10 @@ function sessionRefresher(response: any) {
41 }
42
43 export function logout(){
44 - return apiCall('logout').then(()=> {
45 - state.username = ''
44 + return apiCall('logout').catch(res => {
45 + if (res.code === 401) // we expect 401
46 + state.username = ''
47 + else
48 + throw res
49 })
50 }
server/src/api.auth.ts
+2 -1
@@ -110,7 +110,8 @@ export const logout: ApiHandler = async ({}, ctx) => {
110 if (!ctx.session)
111 return new ApiError(500)
112 loggedIn(ctx, false)
113 - return {}
113 + // 401 is a convenient code for OK: the browser clears a possible http authentication (hopefully), and Admin automatically triggers login dialog
114 + return new ApiError(401)
115 }
116
117 export const refresh_session: ApiHandler = async ({}, ctx) => {
server/src/serveGuiFiles.ts
+1 -1
@@ -55,7 +55,7 @@ function serveProxied(port: string | undefined, uri: string) { // used for devel
55 return
56 console.debug('proxied on port', port)
57 let proxy: Koa.Middleware
58 - import('koa-better-http-proxy').then(lib =>
58 + import('koa-better-http-proxy').then(lib => // dynamic import to avoid having this in final distribution
59 proxy = lib.default('127.0.0.1:'+port, {
60 proxyReqPathResolver: (ctx) => ctx.path.endsWith('/') ? '/' : ctx.path,
61 userResDecorator(res, data, ctx) {