From d6cd0ee983a5f80e9f44bd8da324ef88d25749bb Mon Sep 17 00:00:00 2001 From: slavas490 Date: Sun, 10 Sep 2023 15:23:17 +0300 Subject: [PATCH] fix unnecessary requests for user data on frontend --- main/handlers/api/handlers.go | 2 +- ui/core/src/baseApp.js | 36 ++++++++++++------- .../src/components/user/TopRightProfile.vue | 1 + ui/core/src/views/AdminLogin.vue | 2 ++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/main/handlers/api/handlers.go b/main/handlers/api/handlers.go index ec4e82e5a..189bb6a12 100644 --- a/main/handlers/api/handlers.go +++ b/main/handlers/api/handlers.go @@ -1133,7 +1133,7 @@ func GetProfilePhotoHandler(e echo.Context) error { err := userService.GetProfilePhoto(sess, id, c.Response().Writer) if err != nil { - return c.NoContent(http.StatusNotFound) + return c.NoContent(http.StatusNoContent) } c.Response().Committed = true c.Response().Header().Set("Content-Type", "image/jpeg") diff --git a/ui/core/src/baseApp.js b/ui/core/src/baseApp.js index 0d7e1b687..b355d72f5 100644 --- a/ui/core/src/baseApp.js +++ b/ui/core/src/baseApp.js @@ -73,15 +73,13 @@ export default { if (this.me.role >= 100) { return true } - } catch (e) { - } + } catch (e) {} try { // check public if (item.publicByID[0] === 2) { return true } - } catch (e) { - } + } catch (e) {} try { // check owner @@ -92,8 +90,7 @@ export default { item.grant[this.me.id][0] === 2) { return true } - } catch (e) { - } + } catch (e) {} try { // check group if (this.me.role !== 0) { @@ -102,8 +99,7 @@ export default { return true } } - } catch (e) { - } + } catch (e) {} // check others try { @@ -111,8 +107,7 @@ export default { // others have write rights return true } - } catch (e) { - } + } catch (e) {} return false }, handleError (o) { @@ -152,14 +147,29 @@ export default { }, setSelectedLang (lang) { if (lang) { - this.$cookie.set('lang', lang, { expires: '1Y' }) + this.$cookie.set('lang', lang, { + expires: '1Y' + }) this.reloadI18n() } else { this.$cookie.delete('lang') this.$i18n.set(this.fallbackLang()) } }, + checkUserHasSession () { + return !!localStorage.getItem('userhassession') + }, + initUserHasSession () { + localStorage.setItem('userhassession', true) + }, + deleteUserHasSession () { + localStorage.removeItem('userhassession') + }, loadMe (clb) { + if (!this.checkUserHasSession()) { + return + } + axios.get('/api/me').then((response) => { this.me = response.data this.$root.$emit('me', this.me) @@ -171,6 +181,7 @@ export default { } } }, (err) => { + this.deleteUserHasSession() this.handleError(err) }) }, @@ -396,8 +407,7 @@ export default { get () { return this.$root.$children[0] }, - set (a) { - } + set (a) {} } }, created () { diff --git a/ui/core/src/components/user/TopRightProfile.vue b/ui/core/src/components/user/TopRightProfile.vue index 6a62dc1d9..7a9da6224 100644 --- a/ui/core/src/components/user/TopRightProfile.vue +++ b/ui/core/src/components/user/TopRightProfile.vue @@ -172,6 +172,7 @@ export default { }, logout () { axios.post('/api/logout', null).then(response => { + this.app.deleteUserHasSession() window.location.replace('/') }, (err) => { this.app.handleError(err) diff --git a/ui/core/src/views/AdminLogin.vue b/ui/core/src/views/AdminLogin.vue index e0545232b..8f05ad2be 100644 --- a/ui/core/src/views/AdminLogin.vue +++ b/ui/core/src/views/AdminLogin.vue @@ -94,9 +94,11 @@ export default { this.pwlogin = true if (this.checkTermsAndConditions()) { axios.post('/api/login', { email: this.email, password: this.password }).then(res => { + this.app.initUserHasSession() this.hasError = false window.location = res.data.location || '/admin/workflow' }, (err) => { + this.app.deleteUserHasSession() this.app.handleError(err) this.loginErrorMessage = this.$t('You have entered an invalid username or password') this.hasError = true