feat(frontend): Get project running again after move to Mac Mini
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
// Copied from https://github.com/atinux/nuxthub-better-auth
|
||||
|
||||
import { defu } from 'defu'
|
||||
import { createAuthClient } from 'better-auth/client'
|
||||
import { createAuthClient } from 'better-auth/vue'
|
||||
import type { InferSessionFromClient, InferUserFromClient, ClientOptions } from 'better-auth/client'
|
||||
import { organizationClient, jwtClient } from 'better-auth/client/plugins'
|
||||
import type { RouteLocationRaw } from 'vue-router'
|
||||
import type { UserDto } from '~/.api-client'
|
||||
import type { RouteLocationNormalizedLoaded } from '#vue-router'
|
||||
|
||||
interface RuntimeAuthConfig {
|
||||
redirectUserTo: RouteLocationRaw | string
|
||||
@@ -30,6 +31,7 @@ const selectedOrganization = ref<{
|
||||
|
||||
export function useAuth() {
|
||||
const url = useRequestURL()
|
||||
const route = useRoute()
|
||||
const headers = import.meta.server ? useRequestHeaders() : undefined
|
||||
|
||||
const client = createAuthClient({
|
||||
@@ -60,18 +62,32 @@ export function useAuth() {
|
||||
user.value = data?.user || null
|
||||
sessionFetching.value = false
|
||||
|
||||
// Fetch JWT - workaround for not working extraction of JWT out of session (https://github.com/better-auth/better-auth/issues/1835)
|
||||
jwt.value = (await client.token()).data?.token ?? null
|
||||
|
||||
// Fetch organization
|
||||
organizations.value = (await client.organization.list()).data ?? []
|
||||
if (!selectedOrganization.value && organizations.value.length > 0) {
|
||||
selectedOrganization.value = organizations.value[0]
|
||||
// Only fetch JWT and organizations if we have a session and not on public routes
|
||||
if (session.value && !isPublicRoute()) {
|
||||
await fetchJwtAndOrganizations()
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
async function fetchJwtAndOrganizations() {
|
||||
// Fetch JWT
|
||||
const tokenResult = await client.token()
|
||||
jwt.value = tokenResult.data?.token ?? null
|
||||
|
||||
// Fetch organization
|
||||
const orgResult = await client.organization.list({
|
||||
fetchOptions: {
|
||||
headers
|
||||
}
|
||||
})
|
||||
organizations.value = orgResult.data ?? []
|
||||
|
||||
if (!selectedOrganization.value && organizations.value.length > 0) {
|
||||
selectedOrganization.value = organizations.value[0]
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => selectedOrganization.value,
|
||||
async (newValue) => {
|
||||
@@ -90,6 +106,12 @@ export function useAuth() {
|
||||
})
|
||||
}
|
||||
|
||||
function isPublicRoute(routeToCheck?: RouteLocationNormalizedLoaded) {
|
||||
const finalRoute = routeToCheck ?? route
|
||||
const publicRoutes = ['/login', '/signup', '/accept-invitation']
|
||||
return publicRoutes.some((path) => finalRoute.path.startsWith(path))
|
||||
}
|
||||
|
||||
async function signOut({ redirectTo }: { redirectTo?: RouteLocationRaw } = {}) {
|
||||
const res = await client.signOut()
|
||||
if (res.error) {
|
||||
@@ -99,7 +121,7 @@ export function useAuth() {
|
||||
session.value = null
|
||||
user.value = null
|
||||
if (redirectTo) {
|
||||
await navigateTo(redirectTo)
|
||||
await navigateTo(redirectTo, { external: true })
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -122,7 +144,9 @@ export function useAuth() {
|
||||
selectedOrganization,
|
||||
options,
|
||||
fetchSession,
|
||||
fetchJwtAndOrganizations,
|
||||
client,
|
||||
jwt
|
||||
jwt,
|
||||
isPublicRoute
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user