From 2cf48a0457b4aa20e5a5ee723742dc1bc835be26 Mon Sep 17 00:00:00 2001 From: Paul Cross Date: Tue, 11 Apr 2023 17:52:58 +0200 Subject: [PATCH] chore(release): v0.4.3 --- CHANGELOG.md | 2 ++ package.json | 2 +- src/runtime/plugin.ts | 33 ++++++++++++++++++--------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b96f3..999dff0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## v0.4.3 + ## v0.4.2 ### Features diff --git a/package.json b/package.json index e3a6c16..9d5f6fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt-sanctum-auth", - "version": "0.4.2", + "version": "0.4.3", "license": "MIT", "type": "module", "exports": { diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 5664754..7547ef1 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -20,41 +20,45 @@ export default defineNuxtPlugin(async () => { const config: ModuleOptions = useRuntimeConfig().nuxtSanctumAuth - addRouteMiddleware('fetch-user', async () => { - getToken() - + addRouteMiddleware('auth', async () => { + if (config.token) { + getToken() + } await getUser() - }, { global: true }) - - addRouteMiddleware('auth', async () => { if (auth.value.loggedIn === false) { return config.redirects.login } }) addRouteMiddleware('guest', async () => { + if (config.token) { + getToken() + } + await getUser() + if (auth.value.loggedIn) { return config.redirects.home } }) const apiFetch = (endpoint: FetchRequest, options?: FetchOptions) => { - const fetch = ofetch.create({ baseURL: config.baseUrl, credentials: 'include', headers: { Accept: 'application/json', - [config.csrf.headerKey]: !config.token ? useCookie(config.csrf.cookieKey).value : null, - 'Authorization': config.token ? 'Bearer ' + auth.value.token : null + [config.csrf.headerKey]: !config.token + ? useCookie(config.csrf.cookieKey).value + : null, + Authorization: config.token ? 'Bearer ' + auth.value.token : null } as HeadersInit }) return fetch(endpoint, options) } - async function csrf (): Csrf { + async function csrf(): Csrf { await ofetch(config.endpoints.csrf, { baseURL: config.baseUrl, credentials: 'include', @@ -66,7 +70,7 @@ export default defineNuxtPlugin(async () => { } const getToken = () => { - auth.value.token = useCookie('nuxt-sanctum-auth-token').value + auth.value.token = useCookie('nuxt-sanctum-auth-token')?.value || null } const setToken = (token: string) => { @@ -77,7 +81,7 @@ export default defineNuxtPlugin(async () => { useCookie('nuxt-sanctum-auth-token').value = null } - async function getUser (): Promise { + async function getUser(): Promise { if (auth.value.loggedIn && auth.value.user) { return auth.value.user as T } @@ -94,11 +98,10 @@ export default defineNuxtPlugin(async () => { } } - async function login ( + async function login( data: any, callback?: Callback | undefined ): Promise { - if (!config.token) { await csrf() } @@ -110,7 +113,7 @@ export default defineNuxtPlugin(async () => { headers: { Accept: 'application/json', 'X-XSRF-TOKEN': !config.token ? useCookie('XSRF-TOKEN').value : null, - 'Authorization': config.token ? 'Bearer ' + auth.value.token : null + Authorization: config.token ? 'Bearer ' + auth.value.token : null } as HeadersInit })