From c7ea6c0b47e6fe8e509a2093296e773b19dbd4fd Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Thu, 9 Apr 2020 10:38:27 +0200 Subject: [PATCH] configuration for Axios and vue auth api --- zero.Web/App/Resources/applications.js | 2 +- zero.Web/App/Resources/page-tree.js | 2 +- zero.Web/App/Resources/sections.js | 2 +- zero.Web/App/Resources/settings.js | 2 +- zero.Web/App/Services/auth.js | 39 ++++++++++++++++++++++++++ zero.Web/App/app.vue | 1 + zero.Web/App/axios.config.js | 26 +++++++++++++++++ 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 zero.Web/App/Services/auth.js create mode 100644 zero.Web/App/axios.config.js diff --git a/zero.Web/App/Resources/applications.js b/zero.Web/App/Resources/applications.js index bb0c0b0f..022310ac 100644 --- a/zero.Web/App/Resources/applications.js +++ b/zero.Web/App/Resources/applications.js @@ -5,6 +5,6 @@ export default { // get all applications getAll() { - return Axios.get(zero.path + 'api/applications/getAll').then(res => Promise.resolve(res.data)); + return Axios.get('applications/getAll').then(res => Promise.resolve(res.data)); } }; \ No newline at end of file diff --git a/zero.Web/App/Resources/page-tree.js b/zero.Web/App/Resources/page-tree.js index 31416d59..c50ee462 100644 --- a/zero.Web/App/Resources/page-tree.js +++ b/zero.Web/App/Resources/page-tree.js @@ -5,7 +5,7 @@ export default { // get all pages with a certain parent (can be empty) getChildren(parent) { - return Axios.get(zero.path + 'api/pageTree/getChildren', { + return Axios.get('pageTree/getChildren', { params: { parent: parent } diff --git a/zero.Web/App/Resources/sections.js b/zero.Web/App/Resources/sections.js index dec8ecf9..7cb57583 100644 --- a/zero.Web/App/Resources/sections.js +++ b/zero.Web/App/Resources/sections.js @@ -5,6 +5,6 @@ export default { // get all sections getAll() { - return Axios.get(zero.path + 'api/sections/getAll').then(res => Promise.resolve(res.data)); + return Axios.get('sections/getAll').then(res => Promise.resolve(res.data)); } }; \ No newline at end of file diff --git a/zero.Web/App/Resources/settings.js b/zero.Web/App/Resources/settings.js index b7180783..2cf5b401 100644 --- a/zero.Web/App/Resources/settings.js +++ b/zero.Web/App/Resources/settings.js @@ -5,6 +5,6 @@ export default { // get all settings areas getAreas() { - return Axios.get(zero.path + 'api/settings/getAreas').then(res => Promise.resolve(res.data)); + return Axios.get('settings/getAreas').then(res => Promise.resolve(res.data)); } }; \ No newline at end of file diff --git a/zero.Web/App/Services/auth.js b/zero.Web/App/Services/auth.js new file mode 100644 index 00000000..e4c3d689 --- /dev/null +++ b/zero.Web/App/Services/auth.js @@ -0,0 +1,39 @@ +import Vue from 'vue'; +import Strings from 'zeroservices/strings'; +import { find as _find, extend as _extend } from 'underscore'; + +export default new Vue({ + + data: () => ({ + isAuthenticated: false, + user: null + }), + + methods: { + + // loads the current user into the cache + loadUser() + { + + }, + + // the cached user has been rejected by the server so we clear credentials here + rejectUser() + { + this.isAuthenticated = false; + this.user = null; + }, + + // logs the user in with the passed credentials + login() + { + + }, + + // logs the current user out + logout() + { + + } + } +}); \ No newline at end of file diff --git a/zero.Web/App/app.vue b/zero.Web/App/app.vue index b795e567..5ea6ecbb 100644 --- a/zero.Web/App/app.vue +++ b/zero.Web/App/app.vue @@ -14,6 +14,7 @@ import AppNavigation from 'zero/navigation.vue' import AppOverlays from 'zerocomponents/Overlays/overlay-holder.vue' import Router from '../router.js' + import 'zero/axios.config.js' export default { name: 'app', diff --git a/zero.Web/App/axios.config.js b/zero.Web/App/axios.config.js new file mode 100644 index 00000000..d9841089 --- /dev/null +++ b/zero.Web/App/axios.config.js @@ -0,0 +1,26 @@ +import Axios from 'axios'; +import Auth from 'zeroservices/auth'; + +if (!zero || !zero.path) +{ + throw Exception('window.zero and zero.path (= base path) have to be configured'); +} + +Axios.defaults.baseURL = zero.path + 'api/'; +Axios.defaults.withCredentials = true; + +Axios.interceptors.response.use(response => response, error => +{ + if (error.response) + { + if (error.response.status === 401) + { + Auth.rejectUser(); + //Notification.error('Authentication failed. Please login again.', 3); + } + } + + return Promise.reject(error); +}); + +export default Axios; \ No newline at end of file