@@ -2,8 +2,9 @@ import type { OAuthClientMetadataInput } from '@atproto/oauth-client-node'
22import type { EventHandlerRequest , H3Event } from 'h3'
33import type { OAuthSession } from '@atproto/oauth-client-node'
44import { NodeOAuthClient } from '@atproto/oauth-client-node'
5- import { OAuthSessionStore , OAuthStateStore } from '#server/utils/atproto/storage'
6-
5+ import { useOAuthStorage } from '#server/utils/atproto/storage'
6+ import { UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
7+ import type { SessionManager } from 'h3'
78// TODO: limit scope as features gets added. atproto just allows login so no scary login screen till we have scopes
89export const scope = 'atproto'
910
@@ -34,12 +35,12 @@ export function getOauthClientMetadata() {
3435type EventHandlerWithOAuthSession < T extends EventHandlerRequest , D > = (
3536 event : H3Event < T > ,
3637 session : OAuthSession | undefined ,
38+ serverSession : SessionManager ,
3739) => Promise < D >
3840
3941async function getOAuthSession ( event : H3Event ) : Promise < OAuthSession | undefined > {
4042 const clientMetadata = getOauthClientMetadata ( )
41- const stateStore = new OAuthStateStore ( event )
42- const sessionStore = new OAuthSessionStore ( event )
43+ const { stateStore, sessionStore } = useOAuthStorage ( event )
4344
4445 const client = new NodeOAuthClient ( {
4546 stateStore,
@@ -59,7 +60,20 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
5960 handler : EventHandlerWithOAuthSession < T , D > ,
6061) {
6162 return defineEventHandler ( async event => {
63+ const config = useRuntimeConfig ( event )
64+
65+ if ( ! config . sessionPassword ) {
66+ throw createError ( {
67+ status : 500 ,
68+ message : UNSET_NUXT_SESSION_PASSWORD ,
69+ } )
70+ }
71+
72+ const serverSession = await useSession ( event , {
73+ password : config . sessionPassword ,
74+ } )
75+
6276 const oAuthSession = await getOAuthSession ( event )
63- return await handler ( event , oAuthSession )
77+ return await handler ( event , oAuthSession , serverSession )
6478 } )
6579}
0 commit comments