@@ -15,6 +15,8 @@ import { UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
1515// @ts -expect-error virtual file from oauth module
1616import { clientUri } from '#oauth/config'
1717
18+ const OAUTH_REQUEST_COOKIE_PREFIX = 'atproto_oauth_req'
19+
1820export default defineEventHandler ( async event => {
1921 const config = useRuntimeConfig ( event )
2022 if ( ! config . sessionPassword ) {
@@ -81,8 +83,12 @@ export default defineEventHandler(async event => {
8183 try {
8284 const state = decodeOAuthState ( event , result . state )
8385 const profile = await getMiniProfile ( result . session )
86+ const npmxProfile = await getNpmxProfile ( query . handle as string , result . session )
8487
85- await session . update ( { public : profile } )
88+ await session . update ( {
89+ public : profile ,
90+ profile : npmxProfile ,
91+ } )
8692 return sendRedirect ( event , state . redirectPath )
8793 } catch ( error ) {
8894 // If we are unable to cleanly handle the callback, meaning that the
@@ -118,15 +124,6 @@ type OAuthStateData = {
118124 redirectPath : string
119125}
120126
121- const miniDocResponse = await fetch (
122- `https://${ SLINGSHOT_HOST } /xrpc/blue.microcosm.identity.resolveMiniDoc?identifier=${ agent . did } ` ,
123- { headers : { 'User-Agent' : 'npmx' } } ,
124- )
125-
126- if ( miniDocResponse . ok ) {
127- const miniDoc : PublicUserSession = await miniDocResponse . json ( )
128- const OAUTH_REQUEST_COOKIE_PREFIX = 'atproto_oauth_req'
129-
130127/**
131128 * This function encodes the OAuth state by generating a random SID, storing it
132129 * in a cookie, and returning a JSON string containing the original state and
@@ -175,46 +172,6 @@ function generateRandomHexString(byteLength: number = 16): string {
175172 ) . join ( '' )
176173}
177174
178- // get existing npmx profile OR create a new one
179- const profileUri = `at://${ agent . did } /dev.npmx.actor.profile/self`
180- const profileResponse = await fetch (
181- `https://${ SLINGSHOT_HOST } /xrpc/blue.microcosm.repo.getRecordByUri?at_uri=${ profileUri } ` ,
182- { headers : { 'User-Agent' : 'npmx' } } ,
183- )
184-
185- if ( profileResponse . ok ) {
186- const profile = await profileResponse . json ( )
187- await session . update ( {
188- public : {
189- ...miniDoc ,
190- avatar,
191- } ,
192- profile : profile . value ,
193- } )
194- } else {
195- const profile = {
196- website : '' ,
197- displayName : miniDoc . handle ,
198- description : '' ,
199- }
200-
201- await agent . com . atproto . repo . createRecord ( {
202- repo : miniDoc . handle ,
203- collection : 'dev.npmx.actor.profile' ,
204- rkey : 'self' ,
205- record : {
206- $type : 'dev.npmx.actor.profile' ,
207- ...profile ,
208- } ,
209- } )
210-
211- await session . update ( {
212- public : {
213- ...miniDoc ,
214- avatar,
215- } ,
216- profile : profile ,
217- } )
218175/**
219176 * This function ensures that an oauth state was indeed encoded for the browser
220177 * session performing the oauth callback.
@@ -326,3 +283,35 @@ async function getAvatar(did: DidString, pds: string) {
326283 }
327284 return avatar
328285}
286+
287+ async function getNpmxProfile ( handle : string , authSession : OAuthSession ) {
288+ const client = new Client ( authSession )
289+
290+ // get existing npmx profile OR create a new one
291+ const profileUri = `at://${ client . did } /dev.npmx.actor.profile/self`
292+ const profileResponse = await fetch (
293+ `https://${ SLINGSHOT_HOST } /xrpc/blue.microcosm.repo.getRecordByUri?at_uri=${ profileUri } ` ,
294+ { headers : { 'User-Agent' : 'npmx' } } ,
295+ )
296+
297+ if ( profileResponse . ok ) {
298+ const profile = await profileResponse . json ( )
299+ return profile
300+ } else {
301+ const profile = {
302+ website : '' ,
303+ displayName : handle ,
304+ description : '' ,
305+ }
306+
307+ await client . createRecord (
308+ {
309+ $type : 'dev.npmx.actor.profile' ,
310+ ...profile ,
311+ } ,
312+ 'self' ,
313+ )
314+
315+ return profile
316+ }
317+ }
0 commit comments