Skip to content

Commit

Permalink
🚸 Check api token expire before restore
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Oct 19, 2024
1 parent 5d34156 commit 99e8502
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"image-type": "^4.1.0",
"ipfs-only-hash": "^4.0.0",
"jszip": "^3.10.1",
"jwt-decode": "^4.0.0",
"lodash.chunk": "^4.2.0",
"lodash.debounce": "^4.0.8",
"mime-types": "^2.1.34",
Expand Down
4 changes: 2 additions & 2 deletions store/book-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Module, VuexModule,Mutation, Action } from 'vuex-module-decorators'

import axios from 'axios'

import { saveAuthSession, clearAuthSession, loadAuthSession} from '~/utils/auth'
import { saveAuthSession, clearAuthSession, loadAuthSession, checkJwtTokenValidity} from '~/utils/auth'
import { API_POST_AUTHORIZE } from '~/constant/api'

@Module({
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class BookAPI extends VuexModule {
try {
this.context.commit('setIsRestoringSession', true)
const session = loadAuthSession()
if (session) {
if (session && checkJwtTokenValidity(session.token)) {
this.context.commit('setToken', session.token)
this.context.commit('setSessionWallet', session.wallet)
if (session.wallet) {
Expand Down
12 changes: 12 additions & 0 deletions utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { jwtDecode } from "jwt-decode";

const AUTH_SESSION_KEY = 'likecoin_nft_book_press_token'

export function checkJwtTokenValidity (token: string) {
try {
const decoded = jwtDecode(token);
return decoded?.exp && decoded.exp * 1000 > Date.now();
} catch (error) {
console.error(error);

Check warning on line 10 in utils/auth.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 18)

Unexpected console statement
return false;
}
}

export function loadAuthSession () {
try {
if (window.localStorage) {
Expand Down

0 comments on commit 99e8502

Please sign in to comment.