Skip to content

Commit 89161fe

Browse files
authored
Merge pull request #64 from SourceCodeOER/workaround_401_token
feat(Plugin): add interceptor for 401 error (workaround)
2 parents 1c207c9 + a0cca9b commit 89161fe

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

plugins/axios.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

plugins/axios.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import qs from 'qs'
2+
import {Plugin} from '@nuxt/types'
3+
4+
const axios: Plugin = ({$axios, redirect, $auth, app}) => {
5+
6+
// Set array format to repeat
7+
$axios.onRequest(config => {
8+
config.paramsSerializer = (params) => {
9+
return qs.stringify(params, {arrayFormat: 'repeat'})
10+
}
11+
});
12+
13+
// Disconnect user (if connected) when 401 error and redirect to login page
14+
$axios.interceptors.response.use(
15+
function (response) {
16+
return response
17+
},
18+
async (error) => {
19+
if (error.response.status === 401 && app.router && app.router.currentRoute.path !== '/login') {
20+
21+
if($auth.loggedIn) {
22+
await $auth.logout();
23+
}
24+
25+
redirect(401, "/login");
26+
} else {
27+
return Promise.reject(error);
28+
}
29+
});
30+
31+
};
32+
33+
export default axios

0 commit comments

Comments
 (0)