@@ -10,10 +10,11 @@ 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 { NitroRuntimeConfig } from 'nitropack/types'
13+ import { type NitroRuntimeConfig } from 'nitropack/types'
1414
1515// @ts -expect-error virtual file from oauth module
1616import { clientUri } from '#oauth/config'
17+ import type { UserServerSession } from '~~/shared/types/userSession'
1718// TODO: If you add writing a new record you will need to add a scope for it
1819export const scope = `atproto ${ LIKES_SCOPE } `
1920
@@ -42,7 +43,7 @@ export function getOauthClientMetadata(pkAlg: string | undefined = undefined): O
4243 ? `http://localhost?redirect_uri=${ encodeURIComponent ( redirect_uri ) } &scope=${ encodeURIComponent ( scope ) } `
4344 : `${ client_uri } /oauth-client-metadata.json`
4445
45- // If anything changes here, please make sure to also update /shared/schemas/oauth.ts to match
46+ // If anything changes here, please make zsure to also update /shared/schemas/oauth.ts to match
4647 return {
4748 client_name : 'npmx.dev' ,
4849 client_id,
@@ -78,7 +79,6 @@ export async function getNodeOAuthClient(
7879 const keyset = await loadJWKs ( config )
7980 // @ts -expect-error Taken from statusphere-example-app. Throws a ts error
8081 const pk = keyset ?. findPrivateKey ( { use : 'sig' } )
81- console . log ( pk )
8282 const clientMetadata = getOauthClientMetadata ( pk ?. alg )
8383
8484 return new NodeOAuthClient ( {
@@ -104,9 +104,10 @@ export async function loadJWKs(config: NitroRuntimeConfig): Promise<Keyset | und
104104 return new Keyset ( [ keys ] )
105105}
106106
107- async function getOAuthSession (
108- event : H3Event ,
109- ) : Promise < { oauthSession : OAuthSession | undefined ; serverSession : SessionManager } > {
107+ async function getOAuthSession ( event : H3Event ) : Promise < {
108+ oauthSession : OAuthSession | undefined
109+ serverSession : SessionManager < UserServerSession >
110+ } > {
110111 const serverSession = await useServerSession ( event )
111112 const config = useRuntimeConfig ( event )
112113
@@ -119,10 +120,6 @@ async function getOAuthSession(
119120 return { oauthSession : undefined , serverSession }
120121 }
121122
122- if ( currentSession . oauthSession && currentSession . public . did ) {
123- //TODO clear and redirect to login to clean up old sessions
124- }
125-
126123 const oauthSession = await client . restore ( currentSession . public . did )
127124 return { oauthSession, serverSession }
128125 } catch ( error ) {
@@ -159,6 +156,12 @@ export function eventHandlerWithOAuthSession<T extends EventHandlerRequest, D>(
159156) {
160157 return defineEventHandler ( async event => {
161158 const { oauthSession, serverSession } = await getOAuthSession ( event )
159+
160+ //A one time redirect to upgrade the previous sessions. Can remove in 2 weeks from merge if we'd like
161+ if ( serverSession . data . oauthSession && serverSession . data ?. public ?. did ) {
162+ return sendRedirect ( event , `/api/auth/atproto?hanlde=${ serverSession . data ?. public ?. did } ` )
163+ }
164+
162165 return await handler ( event , oauthSession , serverSession )
163166 } )
164167}
0 commit comments