feat(frontend): Send JWT with every request
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { defu } from 'defu'
|
||||
import { createAuthClient } from 'better-auth/client'
|
||||
import type { InferSessionFromClient, InferUserFromClient, ClientOptions } from 'better-auth/client'
|
||||
import { organizationClient } from 'better-auth/client/plugins'
|
||||
import { organizationClient, jwtClient } from 'better-auth/client/plugins'
|
||||
import type { RouteLocationRaw } from 'vue-router'
|
||||
|
||||
interface RuntimeAuthConfig {
|
||||
@@ -20,7 +20,7 @@ export function useAuth() {
|
||||
fetchOptions: {
|
||||
headers
|
||||
},
|
||||
plugins: [organizationClient()]
|
||||
plugins: [organizationClient(), jwtClient()]
|
||||
})
|
||||
|
||||
const options = defu(useRuntimeConfig().public.auth as Partial<RuntimeAuthConfig>, {
|
||||
@@ -30,8 +30,9 @@ export function useAuth() {
|
||||
const session = useState<InferSessionFromClient<ClientOptions> | null>('auth:session', () => null)
|
||||
const user = useState<InferUserFromClient<ClientOptions> | null>('auth:user', () => null)
|
||||
const sessionFetching = import.meta.server ? ref(false) : useState('auth:sessionFetching', () => false)
|
||||
const jwt = useState<string | null>('auth:jwt', () => null)
|
||||
|
||||
const fetchSession = async () => {
|
||||
async function fetchSession() {
|
||||
if (sessionFetching.value) {
|
||||
console.log('already fetching session')
|
||||
return
|
||||
@@ -42,6 +43,7 @@ export function useAuth() {
|
||||
headers
|
||||
}
|
||||
})
|
||||
jwt.value = (await client.token()).data?.token ?? null
|
||||
session.value = data?.session || null
|
||||
user.value = data?.user || null
|
||||
sessionFetching.value = false
|
||||
@@ -79,6 +81,7 @@ export function useAuth() {
|
||||
organization: client.organization,
|
||||
options,
|
||||
fetchSession,
|
||||
client
|
||||
client,
|
||||
jwt
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user