Skip to content

Commit

Permalink
feat: add auto-refresh failure function
Browse files Browse the repository at this point in the history
Also moved the check into a composable, so it can be used elsewhere.
  • Loading branch information
codiam committed May 8, 2023
1 parent 2b0ec0c commit 0e709d8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
8 changes: 8 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface ModuleOptions {
* @type boolean
*/
autoRefresh?: boolean;
/**
* Auto refesh tokens
* @default true
* @type boolean
*/
onAutoRefreshFailure?: () => Promise<void>;
/**
* fetch user params
* @type boolean
Expand Down Expand Up @@ -88,6 +94,7 @@ export default defineNuxtModule<ModuleOptions>({
url: options.url,
autoFetch: options.autoFetch,
autoRefresh: options.autoRefresh,
onAutoRefreshFailure: options.onAutoRefreshFailure,
fetchUserParams: options.fetchUserParams,
token: options.token,
devtools: options.devtools,
Expand All @@ -104,6 +111,7 @@ export default defineNuxtModule<ModuleOptions>({
url: options.url,
autoFetch: options.autoFetch,
autoRefresh: options.autoRefresh,
onAutoRefreshFailure: options.onAutoRefreshFailure,
fetchUserParams: options.fetchUserParams,
token: options.token,
devtools: options.devtools,
Expand Down
12 changes: 2 additions & 10 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, refreshToken, refreshTokens } = useDirectusToken()
const { token, token_expired, refreshToken, refreshTokens, checkAutoRefresh } = useDirectusToken()

return async <T>(
url: string,
Expand All @@ -15,15 +15,7 @@ export const useDirectus = () => {
): Promise<T> => {
const headers: HeadersInit = {}

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

if (token?.value && !token_expired.value) {
headers.Authorization = `Bearer ${token.value}`
Expand Down
19 changes: 18 additions & 1 deletion src/runtime/composables/useDirectusToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ export const useDirectusToken = () => {
}
}


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

const token_expires_in = computed(() => Math.max(0, (expires().value ?? 0) - new Date().getTime()));

const token_expired = computed(() => !token().value || token_expires_in.value == 0);
Expand All @@ -69,6 +85,7 @@ export const useDirectusToken = () => {
expires: expires(),
token_expires_in,
token_expired,
refreshTokens
refreshTokens,
checkAutoRefresh
}
}
18 changes: 3 additions & 15 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@ export default defineNuxtPlugin(async (nuxtApp) => {

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

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

async function checkIfUserExists() {
if (config.public.directus.autoFetch) {
if (!user.value && token.value) {
Expand All @@ -33,14 +21,14 @@ export default defineNuxtPlugin(async (nuxtApp) => {

nuxtApp.hook('app:created', async () => {
if (process.server) {
await checkRefreshToken();
await checkAutoRefresh();
await checkIfUserExists();
}
})

nuxtApp.hook('page:start', async () => {
if (process.client) {
await checkRefreshToken();
await checkAutoRefresh();
await checkIfUserExists();
}
})
Expand Down

0 comments on commit 0e709d8

Please sign in to comment.