Skip to content

Commit 2400d7a

Browse files
authored
Merge pull request #126 from web-tech-tw/jwt-decode
perf: replace sara with jwt decoder
2 parents 23a6adf + 209358e commit 2400d7a

File tree

9 files changed

+61
-94
lines changed

9 files changed

+61
-94
lines changed

.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ VUE_APP_SELF_HOST=https://web-tech.tw/openchat
22
VUE_APP_OCJI_HOST=https://web-tech.tw/recv/openchat
33

44
VUE_APP_SARA_INTE_HOST=https://web-tech.tw/sara
5-
VUE_APP_SARA_RECV_HOST=https://web-tech.tw/recv/sara
65
VUE_APP_SARA_TOKEN_NAME=unified_token
76

87
VUE_APP_TURNSTILE_SITE_KEY="0x4AAAAAAAr6LY1hBUbTBkVA"

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"autoprefixer": "^10",
1313
"core-js": "^3.8.3",
1414
"dayjs": "^1.11.12",
15+
"jwt-decode": "^4.0.0",
1516
"nanoid": "^5.0.7",
1617
"postcss": "^8",
1718
"tailwindcss": "^3",

src/components/AppHeader.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export default {
237237
},
238238
async created() {
239239
document.addEventListener('click', this.handleDocumentClick);
240-
this.profile = await this.$profile();
240+
this.profile = this.$profile();
241241
},
242242
methods: {
243243
openMenu(item) {

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue'
2-
import './plugins/sara'
32
import './plugins/axios'
3+
import './plugins/profile'
44
import App from './App.vue'
55
import router from './router'
66
import './assets/tailwind.css'

src/plugins/profile.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
import Vue from "vue";
4+
import { jwtDecode } from "jwt-decode";
5+
6+
const {
7+
VUE_APP_SARA_TOKEN_NAME: saraTokenName,
8+
} = process.env;
9+
10+
export const useProfile = () => {
11+
const saraToken = localStorage.getItem(saraTokenName);
12+
if (!saraToken) {
13+
return null;
14+
}
15+
16+
try {
17+
const data = jwtDecode(saraToken);
18+
if (Date.now() >= data.exp * 1000) {
19+
throw new Error("sara token expired");
20+
}
21+
return data?.user;
22+
} catch (e) {
23+
console.warn(e);
24+
localStorage.removeItem(saraTokenName);
25+
location.reload();
26+
return null;
27+
}
28+
};
29+
30+
const extension = {
31+
install: (Vue) => {
32+
window.profile = useProfile;
33+
Vue.profile = useProfile;
34+
Vue.prototype.profile = useProfile;
35+
Vue.prototype.$profile = useProfile;
36+
},
37+
};
38+
39+
Vue.use(extension);
40+
41+
export default extension;

src/plugins/sara.js

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

src/views/AdminJoinView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export default {
176176
},
177177
},
178178
async created() {
179-
this.profile = await this.$profile();
179+
this.profile = this.$profile();
180180
if (!this.profile) {
181181
const refer = `${process.env.VUE_APP_SELF_HOST}/#/admin/join`;
182182
const url = `${process.env.VUE_APP_SARA_INTE_HOST}/?refer=${encodeURIComponent(refer)}`;

src/views/AdminRoomView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default {
6262
},
6363
},
6464
async created() {
65-
this.profile = await this.$profile();
65+
this.profile = this.$profile();
6666
if (!this.profile) {
6767
const refer = `${process.env.VUE_APP_SELF_HOST}/#/admin/room`;
6868
const url = `${process.env.VUE_APP_SARA_INTE_HOST}/?refer=${encodeURIComponent(refer)}`;

0 commit comments

Comments
 (0)