@@ -2,8 +2,11 @@ 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 { parse } from 'valibot'
6+ import { useOAuthStorage } from '#server/utils/atproto/storage'
7+ import { UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
8+ import { OAuthMetadataSchema } from '#shared/schemas/oauth'
9+ import type { SessionManager } from 'h3'
710// TODO: limit scope as features gets added. atproto just allows login so no scary login screen till we have scopes
811export const scope = 'atproto'
912
@@ -18,7 +21,8 @@ export function getOauthClientMetadata() {
1821 ? `http://localhost?redirect_uri=${ encodeURIComponent ( redirect_uri ) } &scope=${ encodeURIComponent ( scope ) } `
1922 : `${ client_uri } /oauth-client-metadata.json`
2023
21- return {
24+ // If anything changes here, please make sure to also update /shared/schemas/oauth.ts to match
25+ return parse ( OAuthMetadataSchema , {
2226 client_name : 'npmx.dev' ,
2327 client_id,
2428 client_uri,
@@ -28,18 +32,18 @@ export function getOauthClientMetadata() {
2832 application_type : 'web' ,
2933 token_endpoint_auth_method : 'none' ,
3034 dpop_bound_access_tokens : true ,
31- } as OAuthClientMetadataInput
35+ } ) as OAuthClientMetadataInput
3236}
3337
3438type EventHandlerWithOAuthSession < T extends EventHandlerRequest , D > = (
3539 event : H3Event < T > ,
3640 session : OAuthSession | undefined ,
41+ serverSession : SessionManager ,
3742) => Promise < D >
3843
3944async function getOAuthSession ( event : H3Event ) : Promise < OAuthSession | undefined > {
4045 const clientMetadata = getOauthClientMetadata ( )
41- const stateStore = new OAuthStateStore ( event )
42- const sessionStore = new OAuthSessionStore ( event )
46+ const { stateStore, sessionStore } = useOAuthStorage ( event )
4347
4448 const client = new NodeOAuthClient ( {
4549 stateStore,
@@ -59,7 +63,20 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
5963 handler : EventHandlerWithOAuthSession < T , D > ,
6064) {
6165 return defineEventHandler ( async event => {
66+ const config = useRuntimeConfig ( event )
67+
68+ if ( ! config . sessionPassword ) {
69+ throw createError ( {
70+ status : 500 ,
71+ message : UNSET_NUXT_SESSION_PASSWORD ,
72+ } )
73+ }
74+
75+ const serverSession = await useSession ( event , {
76+ password : config . sessionPassword ,
77+ } )
78+
6279 const oAuthSession = await getOAuthSession ( event )
63- return await handler ( event , oAuthSession )
80+ return await handler ( event , oAuthSession , serverSession )
6481 } )
6582}
0 commit comments