From 42e1b0216a73debce356c4e76e734e648b3932b8 Mon Sep 17 00:00:00 2001 From: hunterlong Date: Mon, 4 May 2020 13:45:43 -0700 Subject: [PATCH] fixed authentication via cookies, oauth login, --- frontend/package.json | 1 + frontend/src/API.js | 30 +++---- frontend/src/App.vue | 9 +- frontend/src/components/Dashboard/TopNav.vue | 4 +- frontend/src/components/Index/Footer.vue | 2 +- frontend/src/forms/Login.vue | 21 ++--- frontend/src/forms/OAuth.vue | 23 +++-- frontend/src/main.js | 12 ++- frontend/src/routes.js | 18 +++- frontend/src/store.js | 6 +- frontend/vue.config.js | 2 +- frontend/yarn.lock | 5 ++ go.mod | 1 + handlers/api.go | 21 +++-- handlers/dashboard.go | 13 +-- handlers/oauth.go | 94 ++++++++++++++------ handlers/routes.go | 10 ++- types/core/struct.go | 2 - types/users/struct.go | 1 + 19 files changed, 180 insertions(+), 95 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index f6bd23ee..fd16c2d5 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -33,6 +33,7 @@ "vue-apexcharts": "^1.5.2", "vue-clipboard2": "^0.3.1", "vue-codemirror": "^4.0.6", + "vue-cookies": "^1.7.0", "vue-flatpickr-component": "^8.1.5", "vue-github-button": "^1.1.2", "vue-moment": "^4.1.0", diff --git a/frontend/src/API.js b/frontend/src/API.js index 0ae562e2..e062a728 100644 --- a/frontend/src/API.js +++ b/frontend/src/API.js @@ -4,7 +4,7 @@ import * as Sentry from "@sentry/browser"; import * as Integrations from "@sentry/integrations"; const qs = require('querystring'); -const tokenKey = "statping_user"; +const tokenKey = "statping_auth"; const errorReporter = "https://bed4d75404924cb3a799e370733a1b64@sentry.statping.com/3" class Api { @@ -29,6 +29,10 @@ class Api { return axios.post('api/core', obj).then(response => (response.data)) } + async oauth_save(obj) { + return axios.post('api/oauth', obj).then(response => (response.data)) + } + async setup_save(data) { return axios.post('api/setup', qs.stringify(data)).then(response => (response.data)) } @@ -228,19 +232,11 @@ class Api { async login(username, password) { const f = {username: username, password: password} - return axios.post('api/login', qs.stringify(f)) - .then(response => (response.data)) + return axios.post('api/login', qs.stringify(f)).then(response => (response.data)) } async logout() { - await axios.get('api/logout').then(response => (response.data)) - return localStorage.removeItem(tokenKey) - } - - saveToken(username, token, admin) { - const user = {username: username, token: token, admin: admin} - localStorage.setItem(tokenKey, JSON.stringify(user)); - return user + return axios.get('api/logout').then(response => (response.data)) } async scss_base() { @@ -255,17 +251,17 @@ class Api { } token() { - const tk = localStorage.getItem(tokenKey) + const tk = $cookies.get(tokenKey) if (!tk) { - return {}; + return {admin: false}; } - return JSON.parse(tk); + return tk; } authToken() { - let user = JSON.parse(localStorage.getItem(tokenKey)); - if (user && user.token) { - return {'Authorization': 'Bearer ' + user.token}; + const tk = $cookies.get(tokenKey) + if (tk.token) { + return {'Authorization': 'Bearer ' + tk.token}; } else { return {}; } diff --git a/frontend/src/App.vue b/frontend/src/App.vue index c605d153..323a7651 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -32,22 +32,19 @@ this.$router.push('/setup') } if (this.$route.path !== '/setup') { - if (this.core.logged_in) { + if (this.$store.state.admin) { await this.$store.dispatch('loadAdmin') } else { await this.$store.dispatch('loadRequired') } this.loaded = true } - - }, async mounted() { if (this.$route.path !== '/setup') { - const tk = localStorage.getItem("statping_user") - if (this.core.logged_in) { + if (this.$store.state.admin) { this.logged_in = true - await this.$store.dispatch('loadAdmin') + // await this.$store.dispatch('loadAdmin') } } } diff --git a/frontend/src/components/Dashboard/TopNav.vue b/frontend/src/components/Dashboard/TopNav.vue index b87c360c..3bfa458a 100644 --- a/frontend/src/components/Dashboard/TopNav.vue +++ b/frontend/src/components/Dashboard/TopNav.vue @@ -28,7 +28,7 @@ - Logout + Logout @@ -37,6 +37,7 @@