Skip to content

Commit

Permalink
feat: add auto-refreshing tokens to plugin and directus handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
codiam committed Apr 27, 2023
1 parent 764d02e commit 570fbdf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface ModuleOptions {
* @type boolean
*/
autoFetch?: boolean;
/**
* Auto refesh tokens
* @default true
* @type boolean
*/
autoRefresh?: boolean;
/**
* fetch user params
* @type boolean
Expand Down Expand Up @@ -60,6 +66,7 @@ export default defineNuxtModule<ModuleOptions>({
defaults: {
url: process.env.NUXT_DIRECTUS_URL,
autoFetch: true,
autoRefresh: false,
devtools: false,
cookieNameToken: 'directus_token',
cookieNameRefreshToken: 'directus_refresh_token'
Expand All @@ -72,6 +79,7 @@ export default defineNuxtModule<ModuleOptions>({
{
url: options.url,
autoFetch: options.autoFetch,
autoRefresh: options.autoRefresh,
fetchUserParams: options.fetchUserParams,
token: options.token,
devtools: options.devtools,
Expand All @@ -86,6 +94,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.runtimeConfig.public.directus = defu(nuxt.options.runtimeConfig.public.directus, {
url: options.url,
autoFetch: options.autoFetch,
autoRefresh: options.autoRefresh,
fetchUserParams: options.fetchUserParams,
token: options.token,
devtools: options.devtools,
Expand Down
10 changes: 8 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 } = useDirectusToken()
const { token, token_expired, refreshTokens } = useDirectusToken()

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

if (token && token.value) {
if (config.public.directus.autoRefresh) {
if (token_expired.value) {
await refreshTokens();
}
}

if (token?.value && !token_expired.value) {
headers.Authorization = `Bearer ${token.value}`
} else if (config.public.directus.token && useStaticToken) {
headers.Authorization = `Bearer ${config.public.directus.token}`
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ export default defineNuxtPlugin(async (nuxtApp) => {

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

async function checkRefreshToken() {
if (config.public.directus.autoRefresh) {
if (token_expired.value) await refreshTokens();
}
}

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

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

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

0 comments on commit 570fbdf

Please sign in to comment.