From b5ce65fc3ec221856c4fe1734f1fb9592f379345 Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Sat, 23 Dec 2017 17:47:13 +0100 Subject: [PATCH] Now use vuex to manage state for authentication --- front/src/auth/index.js | 99 ---------------------- front/src/components/Sidebar.vue | 6 +- front/src/components/audio/SearchBar.vue | 3 +- front/src/components/audio/Track.vue | 5 +- front/src/components/audio/track/Table.vue | 6 +- front/src/components/auth/Login.vue | 20 +++-- front/src/components/auth/Logout.vue | 21 +---- front/src/components/auth/Profile.vue | 28 ++---- front/src/components/library/Library.vue | 13 +-- front/src/components/library/Track.vue | 5 +- front/src/components/metadata/Search.vue | 3 +- front/src/main.js | 7 +- front/src/store/auth.js | 98 +++++++++++++++++++++ front/src/store/index.js | 2 + 14 files changed, 137 insertions(+), 179 deletions(-) delete mode 100644 front/src/auth/index.js create mode 100644 front/src/store/auth.js diff --git a/front/src/auth/index.js b/front/src/auth/index.js deleted file mode 100644 index 802369428..000000000 --- a/front/src/auth/index.js +++ /dev/null @@ -1,99 +0,0 @@ -import logger from '@/logging' -import config from '@/config' -import cache from '@/cache' -import Vue from 'vue' - -import favoriteTracks from '@/favorites/tracks' - -// URL and endpoint constants -const LOGIN_URL = config.API_URL + 'token/' -const USER_PROFILE_URL = config.API_URL + 'users/users/me/' -// const SIGNUP_URL = API_URL + 'users/' - -let userData = { - authenticated: false, - username: '', - availablePermissions: {}, - profile: {} -} -let auth = { - - // Send a request to the login URL and save the returned JWT - login (context, creds, redirect, onError) { - return context.$http.post(LOGIN_URL, creds).then(response => { - logger.default.info('Successfully logged in as', creds.username) - cache.set('token', response.data.token) - cache.set('username', creds.username) - - this.user.authenticated = true - this.user.username = creds.username - this.connect() - // Redirect to a specified route - if (redirect) { - context.$router.push(redirect) - } - }, response => { - logger.default.error('Error while logging in', response.data) - if (onError) { - onError(response) - } - }) - }, - - // To log out, we just need to remove the token - logout () { - cache.clear() - this.user.authenticated = false - logger.default.info('Log out, goodbye!') - }, - - checkAuth () { - logger.default.info('Checking authentication...') - var jwt = this.getAuthToken() - var username = cache.get('username') - if (jwt) { - this.user.authenticated = true - this.user.username = username - logger.default.info('Logged back in as ' + username) - this.connect() - } else { - logger.default.info('Anonymous user') - this.user.authenticated = false - } - }, - - getAuthToken () { - return cache.get('token') - }, - - // The object to be passed as a header for authenticated requests - getAuthHeader () { - return 'JWT ' + this.getAuthToken() - }, - - fetchProfile () { - let resource = Vue.resource(USER_PROFILE_URL) - return resource.get({}).then((response) => { - logger.default.info('Successfully fetched user profile') - return response.data - }, (response) => { - logger.default.info('Error while fetching user profile') - }) - }, - connect () { - // called once user has logged in successfully / reauthenticated - // e.g. after a page refresh - let self = this - this.fetchProfile().then(data => { - Vue.set(self.user, 'profile', data) - Object.keys(data.permissions).forEach(function (key) { - // this makes it easier to check for permissions in templates - Vue.set(self.user.availablePermissions, key, data.permissions[String(key)].status) - }) - }) - favoriteTracks.fetch() - } -} - -Vue.set(auth, 'user', userData) -export default auth diff --git a/front/src/components/Sidebar.vue b/front/src/components/Sidebar.vue index 9112c2588..d6a253922 100644 --- a/front/src/components/Sidebar.vue +++ b/front/src/components/Sidebar.vue @@ -28,8 +28,8 @@