feat(frontend): Send JWT with every request
This commit is contained in:
@@ -5,12 +5,15 @@ import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo'
|
|||||||
export function useApplicationFormApi() {
|
export function useApplicationFormApi() {
|
||||||
const appBaseUrl = useRuntimeConfig().app.baseURL
|
const appBaseUrl = useRuntimeConfig().app.baseURL
|
||||||
const { serverApiBaseUrl, serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
|
const { serverApiBaseUrl, serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
|
||||||
|
const { jwt } = useAuth()
|
||||||
|
|
||||||
const basePath = withoutTrailingSlash(
|
const basePath = withoutTrailingSlash(
|
||||||
cleanDoubleSlashes(import.meta.client ? appBaseUrl + clientProxyBasePath : serverApiBaseUrl + serverApiBasePath)
|
cleanDoubleSlashes(import.meta.client ? appBaseUrl + clientProxyBasePath : serverApiBaseUrl + serverApiBasePath)
|
||||||
)
|
)
|
||||||
|
|
||||||
const applicationFormApiClient = new ApplicationFormApi(new Configuration({ basePath }))
|
const applicationFormApiClient = new ApplicationFormApi(
|
||||||
|
new Configuration({ basePath, headers: { Authorization: jwt.value ?? '' } })
|
||||||
|
)
|
||||||
|
|
||||||
async function createApplicationForm(
|
async function createApplicationForm(
|
||||||
createApplicationFormDto: CreateApplicationFormDto
|
createApplicationFormDto: CreateApplicationFormDto
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
import { createAuthClient } from 'better-auth/client'
|
import { createAuthClient } from 'better-auth/client'
|
||||||
import type { InferSessionFromClient, InferUserFromClient, ClientOptions } 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'
|
import type { RouteLocationRaw } from 'vue-router'
|
||||||
|
|
||||||
interface RuntimeAuthConfig {
|
interface RuntimeAuthConfig {
|
||||||
@@ -20,7 +20,7 @@ export function useAuth() {
|
|||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
headers
|
headers
|
||||||
},
|
},
|
||||||
plugins: [organizationClient()]
|
plugins: [organizationClient(), jwtClient()]
|
||||||
})
|
})
|
||||||
|
|
||||||
const options = defu(useRuntimeConfig().public.auth as Partial<RuntimeAuthConfig>, {
|
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 session = useState<InferSessionFromClient<ClientOptions> | null>('auth:session', () => null)
|
||||||
const user = useState<InferUserFromClient<ClientOptions> | null>('auth:user', () => null)
|
const user = useState<InferUserFromClient<ClientOptions> | null>('auth:user', () => null)
|
||||||
const sessionFetching = import.meta.server ? ref(false) : useState('auth:sessionFetching', () => false)
|
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) {
|
if (sessionFetching.value) {
|
||||||
console.log('already fetching session')
|
console.log('already fetching session')
|
||||||
return
|
return
|
||||||
@@ -42,6 +43,7 @@ export function useAuth() {
|
|||||||
headers
|
headers
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
jwt.value = (await client.token()).data?.token ?? null
|
||||||
session.value = data?.session || null
|
session.value = data?.session || null
|
||||||
user.value = data?.user || null
|
user.value = data?.user || null
|
||||||
sessionFetching.value = false
|
sessionFetching.value = false
|
||||||
@@ -79,6 +81,7 @@ export function useAuth() {
|
|||||||
organization: client.organization,
|
organization: client.organization,
|
||||||
options,
|
options,
|
||||||
fetchSession,
|
fetchSession,
|
||||||
client
|
client,
|
||||||
|
jwt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { betterAuth } from 'better-auth'
|
import { betterAuth } from 'better-auth'
|
||||||
import Database from 'better-sqlite3'
|
import Database from 'better-sqlite3'
|
||||||
import { organization } from 'better-auth/plugins'
|
import { organization, jwt } from 'better-auth/plugins'
|
||||||
import { resend } from './mail'
|
import { resend } from './mail'
|
||||||
|
|
||||||
export const auth = betterAuth({
|
export const auth = betterAuth({
|
||||||
database: new Database('./sqlite.db'),
|
database: new Database('./sqlite.db'),
|
||||||
emailAndPassword: { enabled: true, autoSignIn: false },
|
emailAndPassword: { enabled: true, autoSignIn: false },
|
||||||
plugins: [
|
plugins: [
|
||||||
|
jwt(),
|
||||||
organization({
|
organization({
|
||||||
async sendInvitationEmail(data) {
|
async sendInvitationEmail(data) {
|
||||||
console.log('Sending invitation email', data)
|
console.log('Sending invitation email', data)
|
||||||
|
|||||||
Reference in New Issue
Block a user