Skip to content
Snippets Groups Projects
Verified Commit be46fb0e authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Fixed broken login redirection when 401 (again)

parent 2b91cc37
Branches
Tags
No related merge requests found
...@@ -52,7 +52,7 @@ import PasswordInput from "@/components/forms/PasswordInput" ...@@ -52,7 +52,7 @@ import PasswordInput from "@/components/forms/PasswordInput"
export default { export default {
props: { props: {
next: { type: String, default: "/" } next: { type: String, default: "/library" }
}, },
components: { components: {
PasswordInput PasswordInput
...@@ -69,6 +69,11 @@ export default { ...@@ -69,6 +69,11 @@ export default {
isLoading: false isLoading: false
} }
}, },
created () {
if (this.$store.state.auth.authenticated) {
this.$router.push(this.next)
}
},
mounted() { mounted() {
this.$refs.username.focus() this.$refs.username.focus()
}, },
...@@ -91,10 +96,11 @@ export default { ...@@ -91,10 +96,11 @@ export default {
username: this.credentials.username, username: this.credentials.username,
password: this.credentials.password password: this.credentials.password
} }
console.log('NEXT', this.next)
this.$store this.$store
.dispatch("auth/login", { .dispatch("auth/login", {
credentials, credentials,
next: "/library", next: this.next,
onError: error => { onError: error => {
if (error.response.status === 400) { if (error.response.status === 400) {
self.error = "invalid_credentials" self.error = "invalid_credentials"
......
...@@ -101,6 +101,9 @@ export default { ...@@ -101,6 +101,9 @@ export default {
} }
}, },
created() { created() {
if (!this.$store.state.auth.authenticated) {
this.$router.push({name: 'login', query: {next: this.$router.currentRoute.fullPath}})
}
this.fetchFavorites(FAVORITES_URL) this.fetchFavorites(FAVORITES_URL)
}, },
mounted() { mounted() {
......
...@@ -97,7 +97,7 @@ axios.interceptors.response.use(function (response) { ...@@ -97,7 +97,7 @@ axios.interceptors.response.use(function (response) {
error.backendErrors = [] error.backendErrors = []
if (error.response.status === 401) { if (error.response.status === 401) {
store.commit('auth/authenticated', false) store.commit('auth/authenticated', false)
logger.default.warn('Received 401 response from API, redirecting to login form') logger.default.warn('Received 401 response from API, redirecting to login form', router.currentRoute.fullPath)
router.push({name: 'login', query: {next: router.currentRoute.fullPath}}) router.push({name: 'login', query: {next: router.currentRoute.fullPath}})
} }
if (error.response.status === 404) { if (error.response.status === 404) {
......
...@@ -75,9 +75,10 @@ export default { ...@@ -75,9 +75,10 @@ export default {
return axios.post('token/', credentials).then(response => { return axios.post('token/', credentials).then(response => {
logger.default.info('Successfully logged in as', credentials.username) logger.default.info('Successfully logged in as', credentials.username)
commit('token', response.data.token) commit('token', response.data.token)
dispatch('fetchProfile') dispatch('fetchProfile').then(() => {
// Redirect to a specified route // Redirect to a specified route
router.push(next) router.push(next)
})
}, response => { }, response => {
logger.default.error('Error while logging in', response.data) logger.default.error('Error while logging in', response.data)
onError(response) onError(response)
...@@ -116,27 +117,34 @@ export default { ...@@ -116,27 +117,34 @@ export default {
document.cookie = 'sessionid=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;' document.cookie = 'sessionid=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'
} }
return axios.get('users/users/me/').then((response) => { return new Promise((resolve, reject) => {
logger.default.info('Successfully fetched user profile') axios.get('users/users/me/').then((response) => {
dispatch('updateProfile', response.data) logger.default.info('Successfully fetched user profile')
dispatch('ui/fetchUnreadNotifications', null, { root: true }) dispatch('updateProfile', response.data).then(() => {
dispatch('favorites/fetch', null, { root: true }) resolve(response.data)
dispatch('playlists/fetchOwn', null, { root: true }) })
return response.data dispatch('ui/fetchUnreadNotifications', null, { root: true })
}, (response) => { dispatch('favorites/fetch', null, { root: true })
logger.default.info('Error while fetching user profile') dispatch('playlists/fetchOwn', null, { root: true })
}, (response) => {
logger.default.info('Error while fetching user profile')
reject()
})
}) })
}, },
updateProfile({ commit }, data) { updateProfile({ commit }, data) {
commit("authenticated", true) return new Promise((resolve, reject) => {
commit("profile", data) commit("authenticated", true)
commit("username", data.username) commit("profile", data)
Object.keys(data.permissions).forEach(function(key) { commit("username", data.username)
// this makes it easier to check for permissions in templates Object.keys(data.permissions).forEach(function(key) {
commit("permission", { // this makes it easier to check for permissions in templates
key, commit("permission", {
status: data.permissions[String(key)] key,
status: data.permissions[String(key)]
})
}) })
resolve()
}) })
}, },
refreshToken ({commit, dispatch, state}) { refreshToken ({commit, dispatch, state}) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment