+18
src/lib/profile.ts
+18
src/lib/profile.ts
···
1
+
import { Agent } from '@atproto/api'
2
+
import { ProfileViewDetailed } from '@atproto/api/dist/client/types/app/bsky/actor/defs'
3
+
4
+
export function parseProfile(profile: ProfileViewDetailed) {
5
+
return profile.handle !== 'handle.invalid' ? null : profile
6
+
}
7
+
8
+
export async function getProfile(agent: Agent, didOrHandle: string) {
9
+
// if no 'app.bsky.actor.profile' record is found on the user's PDS, this method will not return an error
10
+
// instead it will return an empty skeleton of a profile record with "handle" set to "handle.invalid"
11
+
const profile = await agent.getProfile({ actor: didOrHandle })
12
+
if (!profile.success) {
13
+
throw new Error('Failed to fetch profile')
14
+
}
15
+
16
+
const data = profile.data.handle === 'handle.invalid' ? null : profile.data
17
+
return data
18
+
}
+6
-5
src/oauth/routes.ts
+6
-5
src/oauth/routes.ts
···
7
7
import { createOrUpdateAccount } from '@/lib/account'
8
8
import { createSession, deleteSession, type SessionData } from '@/lib/session'
9
9
import { authMacro } from '@/lib/middleware'
10
+
import { getProfile, parseProfile } from '@/lib/profile'
10
11
11
12
const publicKeys = keys.map((k) => {
12
13
const { kty, alg, kid, crv, x, y } = k
···
27
28
}
28
29
29
30
const agent = new Agent(oauthSession)
30
-
const profile = await agent.getProfile({ actor: session.account.did })
31
-
if (!profile.success) {
32
-
return status(500, 'Failed to fetch profile')
33
-
}
31
+
const profile = await getProfile(agent, session.account.did)
34
32
35
-
return profile.data
33
+
return {
34
+
did: oauthSession.did,
35
+
profile
36
+
}
36
37
},
37
38
{
38
39
auth: true