Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
chore(release): v0.4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kreejzak committed Apr 11, 2023
1 parent 3d5f474 commit 2cf48a0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## v0.4.3

## v0.4.2

### Features
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-sanctum-auth",
"version": "0.4.2",
"version": "0.4.3",
"license": "MIT",
"type": "module",
"exports": {
Expand Down
33 changes: 18 additions & 15 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) => {
Expand All @@ -77,7 +81,7 @@ export default defineNuxtPlugin(async () => {
useCookie('nuxt-sanctum-auth-token').value = null
}

async function getUser<T> (): Promise<T | undefined> {
async function getUser<T>(): Promise<T | undefined> {
if (auth.value.loggedIn && auth.value.user) {
return auth.value.user as T
}
Expand All @@ -94,11 +98,10 @@ export default defineNuxtPlugin(async () => {
}
}

async function login (
async function login(
data: any,
callback?: Callback | undefined
): Promise<void> {

if (!config.token) {
await csrf()
}
Expand All @@ -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
})

Expand Down

0 comments on commit 2cf48a0

Please sign in to comment.