Skip to content

Commit

Permalink
🐛 Handle auth errors with axios interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed Nov 23, 2021
1 parent a772b0c commit 7f53d71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions client/plugins/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class API {
baseURL: publicMode ? '/api/public' : '/api'
})

// Not in use right now since Space doesn't return auth errors yet
const loginUrl = process.client ? `https://deta.space/auth?redirect_uri=${ window.location.toString() }` : `https://deta.space/library?open=webcrate`
this.http.onError((error) => {
if (error && error.response && error.response.status === 403) {
Expand All @@ -16,7 +15,9 @@ class API {

// Workaround to catch Space auth errors by checking if the request got redirected and then follow the redirect
this.http.onResponse((res) => {
if (res.request.responseURL.startsWith('https://deta.space/login')) {
if (res.status === 403) {
return redirect(loginUrl)
} else if (res.request.responseURL.startsWith('https://deta.space/login')) {
return redirect(res.request.responseURL)
}
})
Expand Down
9 changes: 9 additions & 0 deletions client/plugins/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function({ $axios, redirect }) {
$axios.onError((error) => {
const loginUrl = process.client ? `https://deta.space/auth?redirect_uri=${ window.location.toString() }` : `https://deta.space/library?open=webcrate`
const code = parseInt(error.response && error.response.status)
if (code === 403) {
return redirect(loginUrl)
}
})
}
3 changes: 2 additions & 1 deletion nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default {
'~/plugins/api.js',
'~/plugins/vue-librarys.js',
'~/plugins/helpers.js',
'~/plugins/directives.js'
'~/plugins/directives.js',
'~/plugins/axios.js'
],

modules: [
Expand Down

0 comments on commit 7f53d71

Please sign in to comment.