diff --git a/komga-webui/src/components/BrowseLibraries.vue b/komga-webui/src/components/BrowseLibraries.vue index 382917399..f1881a97b 100644 --- a/komga-webui/src/components/BrowseLibraries.vue +++ b/komga-webui/src/components/BrowseLibraries.vue @@ -112,7 +112,7 @@ export default Vue.extend({ } }, async created () { - this.libraryName = await this.getLibraryName() + this.libraryName = await this.getLibraryNameLazy(this.libraryId) }, mounted () { // fill series skeletons if an index is provided, so scroll position can be restored @@ -226,13 +226,6 @@ export default Vue.extend({ this.series.splice(page.number * page.size, page.size, ...page.content) this.pagesState[page.number] = LoadState.Loaded }, - async getLibraryName (): Promise { - if (this.libraryId !== 0) { - return (await this.$komgaLibraries.getLibrary(this.libraryId)).name - } else { - return 'All libraries' - } - }, getLibraryNameLazy (libraryId: any): string { if (libraryId !== 0) { return (this.$store.getters.getLibraryById(libraryId)).name diff --git a/komga-webui/src/components/UserSharedLibrariesEditDialog.vue b/komga-webui/src/components/UserSharedLibrariesEditDialog.vue index f7707ba02..e5828012e 100644 --- a/komga-webui/src/components/UserSharedLibrariesEditDialog.vue +++ b/komga-webui/src/components/UserSharedLibrariesEditDialog.vue @@ -98,13 +98,6 @@ export default Vue.extend({ this.dialogReset(val) } }, - async mounted () { - try { - await this.$store.dispatch('getLibraries') - } catch (e) { - this.showSnack(e.message) - } - }, computed: { libraries (): LibraryDto[] { return this.$store.state.komgaLibraries.libraries diff --git a/komga-webui/src/plugins/http.plugin.ts b/komga-webui/src/plugins/http.plugin.ts index 8bb3e4f9a..75f2bc839 100644 --- a/komga-webui/src/plugins/http.plugin.ts +++ b/komga-webui/src/plugins/http.plugin.ts @@ -5,7 +5,8 @@ export default { install (Vue: typeof _Vue) { Vue.prototype.$http = axios.create({ baseURL: process.env.VUE_APP_KOMGA_API_URL ? process.env.VUE_APP_KOMGA_API_URL : window.location.origin, - withCredentials: true + withCredentials: true, + headers: { 'X-Requested-With': 'XMLHttpRequest' } } as AxiosRequestConfig) } } diff --git a/komga-webui/src/plugins/komga-users.plugin.ts b/komga-webui/src/plugins/komga-users.plugin.ts index a9016f97e..5a537eb89 100644 --- a/komga-webui/src/plugins/komga-users.plugin.ts +++ b/komga-webui/src/plugins/komga-users.plugin.ts @@ -11,7 +11,8 @@ const vuexModule: Module = { users: [] as UserWithSharedLibrariesDto[] }, getters: { - meAdmin: state => state.me.hasOwnProperty('roles') && state.me.roles.includes('ADMIN') + meAdmin: state => state.me.hasOwnProperty('roles') && state.me.roles.includes('ADMIN'), + authenticated: state => state.me.hasOwnProperty('id') }, mutations: { setMe (state, user: UserDto) { @@ -22,6 +23,16 @@ const vuexModule: Module = { } }, actions: { + async getMeWithAuth ({ commit }, { login, password }: { login: string, password: string }) { + commit('setMe', await service.getMeWithAuth(login, password)) + }, + async logout ({ commit }) { + try { + await service.getMeWithAuth('', '') + } catch (e) { + } + commit('setMe', {}) + }, async getMe ({ commit }) { commit('setMe', await service.getMe()) }, diff --git a/komga-webui/src/router.ts b/komga-webui/src/router.ts index cc422a9bb..2dc886a04 100644 --- a/komga-webui/src/router.ts +++ b/komga-webui/src/router.ts @@ -11,7 +11,7 @@ const adminGuard = (to: any, from: any, next: any) => { else next() } -export default new Router({ +const router = new Router({ mode: 'history', base: process.env.BASE_URL, routes: [ @@ -75,12 +75,20 @@ export default new Router({ } ] }, + { + path: '/startup', + name: 'startup', + component: () => import(/* webpackChunkName: "startup" */ './views/Startup.vue') + }, + { + path: '/login', + name: 'login', + component: () => import(/* webpackChunkName: "login" */ './views/Login.vue') + }, { path: '*', - name: - 'notfound', - component: - () => import(/* webpackChunkName: "notfound" */ './views/PageNotFound.vue') + name: 'notfound', + component: () => import(/* webpackChunkName: "notfound" */ './views/PageNotFound.vue') } ], scrollBehavior (to, from, savedPosition) { @@ -93,3 +101,10 @@ export default new Router({ } } }) + +router.beforeEach((to, from, next) => { + if (to.name !== 'startup' && to.name !== 'login' && !lStore.getters.authenticated) next({ name: 'startup' }) + else next() +}) + +export default router diff --git a/komga-webui/src/services/komga-users.service.ts b/komga-webui/src/services/komga-users.service.ts index 03eb8099c..af55582ad 100644 --- a/komga-webui/src/services/komga-users.service.ts +++ b/komga-webui/src/services/komga-users.service.ts @@ -9,6 +9,31 @@ export default class KomgaUsersService { this.http = http } + async getMeWithAuth (login: string, password: string): Promise { + try { + return (await this.http.get( + `${API_USERS}/me`, + { + auth: { + username: login, + password: password + } + } + )).data + } catch (e) { + let msg = 'An error occurred while trying to login' + if (e.response) { + if (e.response.status === 401) { + msg = 'Invalid authentication' + } + } + if (e.response.data.message) { + msg += `: ${e.response.data.message}` + } + throw new Error(msg) + } + } + async getMe (): Promise { try { return (await this.http.get(`${API_USERS}/me`)).data diff --git a/komga-webui/src/views/Home.vue b/komga-webui/src/views/Home.vue index c21a18f8f..8ce6839d2 100644 --- a/komga-webui/src/views/Home.vue +++ b/komga-webui/src/views/Home.vue @@ -96,14 +96,14 @@ - - - - - - - - + + + mdi-power + + + Log out + + @@ -114,20 +114,6 @@ - - - {{ snackText }} - - Close - - @@ -142,9 +128,7 @@ export default Vue.extend({ drawerVisible: true, modalAddLibrary: false, modalDeleteLibrary: false, - libraryToDelete: {} as LibraryDto, - snackbar: false, - snackText: '' + libraryToDelete: {} as LibraryDto }), computed: { libraries (): LibraryDto[] { @@ -165,27 +149,17 @@ export default Vue.extend({ return [] } }, - async mounted () { - try { - await this.$store.dispatch('getMe') - await this.$store.dispatch('getLibraries') - } catch (e) { - this.showSnack(e.message) - } - }, methods: { toggleDrawer () { this.drawerVisible = !this.drawerVisible }, - showSnack (message: string) { - this.snackText = message - this.snackbar = true - }, promptDeleteLibrary (library: LibraryDto) { this.libraryToDelete = library this.modalDeleteLibrary = true }, logout () { + this.$store.dispatch('logout') + this.$router.push({ name: 'login' }) } } }) diff --git a/komga-webui/src/views/Login.vue b/komga-webui/src/views/Login.vue new file mode 100644 index 000000000..ea2f6a78d --- /dev/null +++ b/komga-webui/src/views/Login.vue @@ -0,0 +1,95 @@ + + + + + diff --git a/komga-webui/src/views/Startup.vue b/komga-webui/src/views/Startup.vue new file mode 100644 index 000000000..9d328591f --- /dev/null +++ b/komga-webui/src/views/Startup.vue @@ -0,0 +1,30 @@ + + + + +