Skip to content

Commit

Permalink
feat: remove the refresh token, if it fails to refresh.
Browse files Browse the repository at this point in the history
  • Loading branch information
codiam committed May 1, 2023
1 parent 25efffd commit 336e3a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/runtime/composables/useDirectus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useDirectusToken } from './useDirectusToken'
export const useDirectus = () => {
const baseURL = useDirectusUrl()
const config = useRuntimeConfig()
const { token, token_expired, refreshTokens } = useDirectusToken()
const { token, token_expired, refreshToken, refreshTokens } = useDirectusToken()

return async <T>(
url: string,
Expand All @@ -17,7 +17,11 @@ export const useDirectus = () => {

if (config.public.directus.autoRefresh) {
if (token_expired.value) {
await refreshTokens();
try {
await refreshTokens();
} catch (e) {
refreshToken.value = null;
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ export default defineNuxtPlugin(async (nuxtApp) => {

const config = useRuntimeConfig();
const { fetchUser } = useDirectusAuth();
const { token, token_expired, refreshTokens } = useDirectusToken();
const { token, token_expired, refreshToken, refreshTokens } = useDirectusToken();
const user = useDirectusUser();

async function checkRefreshToken() {
if (config.public.directus.autoRefresh) {
if (token_expired.value) await refreshTokens();
if (token_expired.value) {
try {
await refreshTokens();
} catch (e) {
refreshToken.value = null;
}
}
}
}

Expand Down

0 comments on commit 336e3a9

Please sign in to comment.