Skip to content

Commit

Permalink
perf: pass header token to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulter committed Sep 16, 2024
1 parent 7a843e6 commit 9435ab3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import App from './App.vue'
// Composables
import { createApp } from 'vue'

import axios from 'axios'
import axios from './utils/axios-wrapper.js'
import store from './store/index.js'
import VueCookies from 'vue-cookies'

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export default {
if (res.data.code === 0) {
this.toast('登录成功', 'success')
this.$store.commit('tokenCheck', this.$bus)
// save token to local storage
localStorage.setItem('access-token', res.data.data.token)
} else {
this.toast('登录失败:' + res.data.msg)
}
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/utils/axios-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import axios from 'axios';

axios.interceptors.request.use(function (config) {
// add Authorization header before request is sent
const token = localStorage.getItem('access-token');
if (token) {
config.headers
.Authorization = `Bearer ${token}`;
}
return config;
}, function (error) {
return Promise.reject(error);
});

export default axios;

0 comments on commit 9435ab3

Please sign in to comment.