Skip to content

Commit c4b6e06

Browse files
committed
feat(Plugin): add interceptor for 401 error (workaround)
- Transform file to typescript extension - Use interceptor to force the user to login w(refresh token workaround)
1 parent a9a1f68 commit c4b6e06

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

plugins/axios.js

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

plugins/axios.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import qs from 'qs'
2+
import {Plugin} from '@nuxt/types'
3+
4+
const axios: Plugin = ({$axios, redirect, $auth}) => {
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) {
20+
if($auth.loggedIn) {
21+
await $auth.logout();
22+
}
23+
redirect(401, "/login");
24+
} else {
25+
return Promise.reject(error);
26+
}
27+
});
28+
29+
};
30+
31+
export default axios

0 commit comments

Comments
 (0)