1- import type { H3Event } from 'h3'
21import { Agent } from '@atproto/api'
32import { NodeOAuthClient } from '@atproto/oauth-client-node'
4- import type {
5- NodeSavedSession ,
6- NodeSavedSessionStore ,
7- NodeSavedState ,
8- NodeSavedStateStore ,
9- } from '@atproto/oauth-client-node'
10- import { scope , getOauthClientMetadata } from '~~/server/utils/atproto'
11- import { createError , getQuery , sendRedirect , getCookie , setCookie , deleteCookie } from 'h3'
3+ import { createError , getQuery , sendRedirect } from 'h3'
4+ import { OAuthSessionStore , OAuthStateStore } from '#server/utils/atproto/storage'
5+ import { SLINGSHOT_ENDPOINT } from '#shared/utils/constants'
126
137export default defineEventHandler ( async event => {
148 const query = getQuery ( event )
159 const clientMetadata = getOauthClientMetadata ( )
16- const stateStore = new StateStore ( event )
17- const sessionStore = new SessionStore ( event )
10+ const stateStore = new OAuthStateStore ( event )
11+ const sessionStore = new OAuthSessionStore ( event )
1812 const atclient = new NodeOAuthClient ( {
1913 stateStore,
2014 sessionStore,
@@ -41,13 +35,12 @@ export default defineEventHandler(async event => {
4135 const agent = new Agent ( authSession )
4236 event . context . agent = agent
4337
44- //TODO prob do server side kv store here too?
4538 const session = await useSession ( event , {
4639 password : process . env . NUXT_SESSION_PASSWORD as string ,
4740 } )
4841
4942 const response = await fetch (
50- `https://slingshot.microcosm.blue /xrpc/com.bad-example.identity.resolveMiniDoc?identifier=${ agent . did } ` ,
43+ `${ SLINGSHOT_ENDPOINT } /xrpc/com.bad-example.identity.resolveMiniDoc?identifier=${ agent . did } ` ,
5144 { headers : { 'User-Agent' : 'npmx' } } ,
5245 )
5346 const miniDoc = ( await response . json ( ) ) as { did : string ; handle : string ; pds : string }
@@ -56,74 +49,5 @@ export default defineEventHandler(async event => {
5649 miniDoc,
5750 } )
5851
59- // await sessionStore.del()
60-
6152 return sendRedirect ( event , '/' )
6253} )
63-
64- /**
65- * Storage key prefix for oauth state storage.
66- */
67- export const OAUTH_STATE_CACHE_STORAGE_BASE = 'oauth-atproto-state'
68-
69- export class StateStore implements NodeSavedStateStore {
70- private readonly cookieKey = 'oauth:atproto:state'
71- private readonly storage = useStorage ( OAUTH_STATE_CACHE_STORAGE_BASE )
72-
73- constructor ( private event : H3Event ) { }
74-
75- async get ( ) : Promise < NodeSavedState | undefined > {
76- const stateKey = getCookie ( this . event , this . cookieKey )
77- if ( ! stateKey ) return
78- const result = await this . storage . getItem < NodeSavedState > ( stateKey )
79- if ( ! result ) return
80- return result
81- }
82-
83- async set ( key : string , val : NodeSavedState ) {
84- setCookie ( this . event , this . cookieKey , key )
85- await this . storage . setItem < NodeSavedState > ( key , val )
86- }
87-
88- async del ( ) {
89- let stateKey = getCookie ( this . event , this . cookieKey )
90- deleteCookie ( this . event , this . cookieKey )
91- if ( stateKey ) {
92- await this . storage . del ( stateKey )
93- }
94- }
95- }
96-
97- /**
98- * Storage key prefix for oauth session storage.
99- */
100- export const OAUTH_SESSION_CACHE_STORAGE_BASE = 'oauth-atproto-session'
101-
102- export class SessionStore implements NodeSavedSessionStore {
103- //TODO not sure if we will support multi accounts, but if we do in the future will need to change this around
104- private readonly cookieKey = 'oauth:atproto:session'
105- private readonly storage = useStorage ( OAUTH_SESSION_CACHE_STORAGE_BASE )
106-
107- constructor ( private event : H3Event ) { }
108-
109- async get ( ) : Promise < NodeSavedSession | undefined > {
110- const sessionKey = getCookie ( this . event , this . cookieKey )
111- if ( ! sessionKey ) return
112- let result = await this . storage . getItem < NodeSavedSession > ( sessionKey )
113- if ( ! result ) return
114- return result
115- }
116-
117- async set ( key : string , val : NodeSavedSession ) {
118- setCookie ( this . event , this . cookieKey , key )
119- await this . storage . setItem < NodeSavedSession > ( key , val )
120- }
121-
122- async del ( ) {
123- let sessionKey = getCookie ( this . event , this . cookieKey )
124- if ( sessionKey ) {
125- await this . storage . del ( sessionKey )
126- }
127- deleteCookie ( this . event , this . cookieKey )
128- }
129- }
0 commit comments