Skip to content

Commit a94077e

Browse files
committed
feat(env): improve env parameters
1 parent 3892e08 commit a94077e

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/runtime/server/composables/useEdgeDbEnv.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export function useEdgeDbEnv() {
66
NUXT_EDGEDB_USER: user,
77
NUXT_EDGEDB_PASS: pass,
88
NUXT_EDGEDB_DATABASE: database,
9+
NUXT_EDGEDB_TLS_CA: tlsCA,
10+
NUXT_EDGEDB_TLS_SECURITY: tlsSecurity,
911
NUXT_EDGEDB_AUTH_BASE_URL: authBaseUrl = 'http://localhost:10702/db/edgedb/ext/auth/',
1012
NUXT_EDGEDB_OAUTH_CALLBACK: oAuthCallbackUrl = 'http://localhost:10702/db/edgedb/ext/auth/callback',
1113
NUXT_EDGEDB_AUTH_VERIFY_REDIRECT_URL: verifyRedirectUrl = 'http://localhost:3000/auth/verify',
@@ -36,5 +38,7 @@ export function useEdgeDbEnv() {
3638
pass,
3739
host,
3840
port,
41+
tlsCA,
42+
tlsSecurity: tlsSecurity as 'insecure' | 'no_host_verification' | 'strict' | 'default',
3943
}
4044
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { EventHandlerRequest, H3Event } from 'h3'
2+
import { getCookie, sendRedirect, setCookie } from 'h3'
3+
import { useEdgeDb } from './useEdgeDb'
4+
5+
interface UseEdgeDbIdentityData<T = any> {
6+
identity: T
7+
cookie: string
8+
update: (event?: H3Event) => Promise<void>
9+
logout: (redirectTo?: string) => Promise<void>
10+
isLoggedIn: boolean
11+
}
12+
13+
export async function useEdgeDbIdentity<T>(
14+
req: H3Event<EventHandlerRequest> | undefined = undefined,
15+
): Promise<UseEdgeDbIdentityData<T>> {
16+
const client = useEdgeDb(req)
17+
18+
let token: string | undefined
19+
20+
let user: T | undefined
21+
22+
const update = async () => {
23+
if (req)
24+
token = getCookie(req, 'edgedb-auth-token')
25+
26+
user = client.querySingle(`select global current_user;`) as T
27+
}
28+
29+
const logout = async (redirectTo: string | undefined) => {
30+
if (!req)
31+
return
32+
33+
setCookie(req, 'edgedb-auth-token', '')
34+
35+
if (redirectTo)
36+
return sendRedirect(req, '/')
37+
}
38+
39+
await update()
40+
41+
const identityData = {
42+
isLoggedIn: !!user,
43+
identity: user,
44+
cookie: token,
45+
update,
46+
logout,
47+
} as UseEdgeDbIdentityData
48+
49+
return identityData
50+
}

src/runtime/server/plugins/edgedb-client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
33
import { useEdgeDbEnv } from '../'
44

55
export default defineNitroPlugin(() => {
6-
const { dsn } = useEdgeDbEnv()
6+
const { dsn, tlsSecurity, tlsCA } = useEdgeDbEnv()
77

8-
const client = createClient({ dsn })
8+
const client = createClient({
9+
dsn,
10+
tlsSecurity,
11+
tlsCA,
12+
})
913

1014
// @ts-expect-error - untyped global
1115
globalThis.__nuxt_edgedb_client__ = client

0 commit comments

Comments
 (0)