@@ -10,7 +10,6 @@ import { NodeOAuthClient, AtprotoDohHandleResolver } from '@atproto/oauth-client
1010import { getOAuthLock } from '#server/utils/atproto/lock'
1111import { useOAuthStorage } from '#server/utils/atproto/storage'
1212import { LIKES_SCOPE } from '#shared/utils/constants'
13- import type { RuntimeConfig } from 'nuxt/schema'
1413import type { UserServerSession } from '#shared/types/userSession'
1514// @ts -expect-error virtual file from oauth module
1615import { clientUri } from '#oauth/config'
@@ -69,16 +68,12 @@ type EventHandlerWithOAuthSession<T extends EventHandlerRequest, D> = (
6968 serverSession : SessionManager ,
7069) => Promise < D >
7170
72- export async function getNodeOAuthClient (
73- serverSession : SessionManager ,
74- config : RuntimeConfig ,
75- ) : Promise < NodeOAuthClient > {
76- const { stateStore, sessionStore } = useOAuthStorage ( serverSession )
71+ export async function getNodeOAuthClient ( ) : Promise < NodeOAuthClient > {
72+ const { stateStore, sessionStore } = useOAuthStorage ( )
7773
7874 // These are optional and not expected or can be used easily in local development, only in production
79- const keyset = await loadJWKs ( config )
80- // @ts -expect-error Taken from statusphere-example-app. Throws a ts error
81- const pk = keyset ?. findPrivateKey ( { use : 'sig' } )
75+ const keyset = await loadJWKs ( )
76+ const pk = keyset ?. findPrivateKey ( { usage : 'sign' } )
8277 const clientMetadata = getOauthClientMetadata ( pk ?. alg )
8378
8479 return new NodeOAuthClient ( {
@@ -91,10 +86,11 @@ export async function getNodeOAuthClient(
9186 } )
9287}
9388
94- export async function loadJWKs ( config : RuntimeConfig ) : Promise < Keyset | undefined > {
89+ export async function loadJWKs ( ) : Promise < Keyset | undefined > {
9590 // If we ever need to add multiple JWKs to rotate keys we will need to add a new one
9691 // under a new variable and update here
97- const jwkOne = config . oauthJwkOne
92+ // @ts -expect-error Not sure how to strongly type these or if there's a better way to get this env
93+ const jwkOne = import . meta. env . NUXT_OAUTH_JWK_ONE
9894 if ( ! jwkOne ) return undefined
9995
10096 // For multiple keys if we need to rotate
@@ -109,18 +105,15 @@ async function getOAuthSession(event: H3Event): Promise<{
109105 serverSession : SessionManager < UserServerSession >
110106} > {
111107 const serverSession = await useServerSession ( event )
112- const config = useRuntimeConfig ( event )
113108
114109 try {
115- const client = await getNodeOAuthClient ( serverSession , config )
116-
117110 const currentSession = serverSession . data
118111 // TODO (jg): why can a session be `{}`?
119112 if ( ! currentSession || ! currentSession . public ?. did ) {
120113 return { oauthSession : undefined , serverSession }
121114 }
122115
123- const oauthSession = await client . restore ( currentSession . public . did )
116+ const oauthSession = await event . context . oauthClient . restore ( currentSession . public . did )
124117 return { oauthSession, serverSession }
125118 } catch ( error ) {
126119 // Log error safely without using util.inspect on potentially problematic objects
@@ -156,11 +149,10 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
156149) {
157150 return defineEventHandler ( async event => {
158151 const { oauthSession, serverSession } = await getOAuthSession ( event )
159- let oauthSessionId = serverSession . data . oauthSessionId
160-
152+ const publicData = serverSession . data . public
161153 // User was authenticated at one point, but was not able to restore
162154 // the session to the PDS
163- if ( ! oauthSession && oauthSessionId ) {
155+ if ( ! oauthSession && publicData ) {
164156 // cleans up our server side session store
165157 await serverSession . clear ( )
166158 throw createError ( {
0 commit comments