Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚸 Check api token expire before restore #489

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading