diff --git a/package-lock.json b/package-lock.json index 63d7c46..7c9e8d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "autoprefixer": "^9.8.8", "axios": "^0.24.0", + "axios-auth-refresh": "^3.3.4", "lorem-ipsum": "^2.0.8", "postcss": "^7.0.39", "tailwind-children": "^0.5.0", @@ -859,6 +860,14 @@ "follow-redirects": "^1.14.4" } }, + "node_modules/axios-auth-refresh": { + "version": "3.3.4", + "resolved": "https://registry.npmjs.org/axios-auth-refresh/-/axios-auth-refresh-3.3.4.tgz", + "integrity": "sha512-cGq3bZu+lip5j+byaQRZaZ3wpCUxs93jGV0614VYP5k2H1vbdoaw6HGazaUJxcRsFMctR3DItCAx1Dn7KerlcA==", + "peerDependencies": { + "axios": ">= 0.18 < 0.19.0 || >= 0.19.1" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -4758,6 +4767,12 @@ "follow-redirects": "^1.14.4" } }, + "axios-auth-refresh": { + "version": "3.3.4", + "resolved": "https://registry.npmjs.org/axios-auth-refresh/-/axios-auth-refresh-3.3.4.tgz", + "integrity": "sha512-cGq3bZu+lip5j+byaQRZaZ3wpCUxs93jGV0614VYP5k2H1vbdoaw6HGazaUJxcRsFMctR3DItCAx1Dn7KerlcA==", + "requires": {} + }, "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", diff --git a/package.json b/package.json index 5adefdc..5465120 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "autoprefixer": "^9.8.8", "axios": "^0.24.0", + "axios-auth-refresh": "^3.3.4", "lorem-ipsum": "^2.0.8", "postcss": "^7.0.39", "tailwind-children": "^0.5.0", diff --git a/src/App.vue b/src/App.vue index 7295241..9b26ef1 100644 --- a/src/App.vue +++ b/src/App.vue @@ -45,8 +45,10 @@ export default defineComponent({ } }, created() { + store.dispatch('loadAuth') + console.log("Starting connection to WebSocket Server") - this.connection = new WebSocket("wss://zierle-training-staging.riezel.com/ws/notifications/") + this.connection = new WebSocket("wss://zierle-training.riezel.com/ws/notifications/") this.connection.onmessage = function(event) { console.log(event); diff --git a/src/api/index.js b/src/api/index.js index 413f76f..927966b 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,4 +1,7 @@ import axios from 'axios' +import createAuthRefreshInterceptor from 'axios-auth-refresh'; + +import store from '../store' const apiClient = axios.create({ baseURL: 'https://zierle-training.riezel.com/api/v1', @@ -10,6 +13,35 @@ const apiClient = axios.create({ } }) +const refreshAuthLLogic = (failedRequest) => { + apiClient.post('/token/refresh/', {'refresh': store.getters.auth.tokens.refresh}).then(response => { + store.dispatch('setTokens', response.data) + failedRequest.response.config.headers['Authorization'] = 'Bearer ' + store.getters.auth.tokens.access + return Promise.resolve() + }).catch(error => { + store.dispatch('logout') + return Promise.reject() + }); +} + +export function setupApiClient() { + createAuthRefreshInterceptor(apiClient, refreshAuthLLogic, { + statusCodes: [401, 403] + }); +} + +apiClient.interceptors.request.use(function(config) { + let auth = store.getters.auth + if (auth.authenticated) { + config.headers['Authorization'] = 'Bearer ' + auth.tokens.access + } + return config +}) + +export function login(username, password) { + return apiClient.post('/token/', {email: username, password}) +} + export function getPhotoLogList() { return apiClient.get('/photologs/') } diff --git a/src/components/NavBar.vue b/src/components/NavBar.vue index f454f23..eff1998 100644 --- a/src/components/NavBar.vue +++ b/src/components/NavBar.vue @@ -12,10 +12,14 @@ + New Log All Templates + New Template + Login +
Logged in as {{authUser}}
+ + \ No newline at end of file