11import type { OAuthClientMetadataInput , OAuthSession } from '@atproto/oauth-client-node'
22import type { EventHandlerRequest , H3Event , SessionManager } from 'h3'
3- import { NodeOAuthClient } from '@atproto/oauth-client-node'
3+ import { NodeOAuthClient , AtprotoDohHandleResolver } from '@atproto/oauth-client-node'
44import { parse } from 'valibot'
55import { getOAuthLock } from '#server/utils/atproto/lock'
66import { useOAuthStorage } from '#server/utils/atproto/storage'
@@ -11,6 +11,13 @@ import { clientUri } from '#oauth/config'
1111// TODO: If you add writing a new record you will need to add a scope for it
1212export const scope = `atproto ${ LIKES_SCOPE } `
1313
14+ /**
15+ * Resolves a did to a handle via DoH or via the http website calls
16+ */
17+ export const handleResolver = new AtprotoDohHandleResolver ( {
18+ dohEndpoint : 'https://cloudflare-dns.com/dns-query' ,
19+ } )
20+
1421export function getOauthClientMetadata ( ) {
1522 const dev = import . meta. dev
1623
@@ -42,24 +49,32 @@ type EventHandlerWithOAuthSession<T extends EventHandlerRequest, D> = (
4249 serverSession : SessionManager ,
4350) => Promise < D >
4451
45- async function getOAuthSession ( event : H3Event ) : Promise < OAuthSession | undefined > {
52+ async function getOAuthSession (
53+ event : H3Event ,
54+ ) : Promise < { oauthSession : OAuthSession | undefined ; serverSession : SessionManager } > {
55+ const serverSession = await useServerSession ( event )
56+
4657 try {
4758 const clientMetadata = getOauthClientMetadata ( )
48- const serverSession = await useServerSession ( event )
4959 const { stateStore, sessionStore } = useOAuthStorage ( serverSession )
5060
5161 const client = new NodeOAuthClient ( {
5262 stateStore,
5363 sessionStore,
5464 clientMetadata,
5565 requestLock : getOAuthLock ( ) ,
66+ handleResolver,
5667 } )
5768
58- const currentSession = await sessionStore . get ( )
59- if ( ! currentSession ) return undefined
69+ const currentSession = serverSession . data
70+ if ( ! currentSession ) {
71+ console . log ( 'oauth session not found' )
72+ return { oauthSession : undefined , serverSession }
73+ }
6074
6175 // restore using the subject
62- return await client . restore ( currentSession . tokenSet . sub )
76+ const oauthSession = await client . restore ( currentSession . public . did )
77+ return { oauthSession, serverSession }
6378 } catch ( error ) {
6479 // Log error safely without using util.inspect on potentially problematic objects
6580 // The @atproto library creates error objects with getters that crash Node's util.inspect
@@ -68,7 +83,7 @@ async function getOAuthSession(event: H3Event): Promise<OAuthSession | undefined
6883 '[oauth] Failed to get session:' ,
6984 error instanceof Error ? error . message : 'Unknown error' ,
7085 )
71- return undefined
86+ return { oauthSession : undefined , serverSession }
7287 }
7388}
7489
@@ -93,9 +108,7 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
93108 handler : EventHandlerWithOAuthSession < T , D > ,
94109) {
95110 return defineEventHandler ( async event => {
96- const serverSession = await useServerSession ( event )
97-
98- const oAuthSession = await getOAuthSession ( event )
99- return await handler ( event , oAuthSession , serverSession )
111+ const { oauthSession, serverSession } = await getOAuthSession ( event )
112+ return await handler ( event , oauthSession , serverSession )
100113 } )
101114}
0 commit comments