|
1 | | -import type { H3Event } from 'h3' |
2 | 1 | import { Agent } from '@atproto/api' |
3 | 2 | import { 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' |
12 | 6 |
|
13 | 7 | export default defineEventHandler(async event => { |
14 | 8 | const query = getQuery(event) |
15 | 9 | 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) |
18 | 12 | const atclient = new NodeOAuthClient({ |
19 | 13 | stateStore, |
20 | 14 | sessionStore, |
@@ -46,55 +40,14 @@ export default defineEventHandler(async event => { |
46 | 40 | }) |
47 | 41 |
|
48 | 42 | const response = await fetch( |
49 | | - `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}`, |
| 44 | + { headers: { 'User-Agent': 'npmx' } }, |
50 | 45 | ) |
51 | 46 | const miniDoc = (await response.json()) as { did: string; handle: string; pds: string } |
52 | 47 |
|
53 | 48 | await session.update({ |
54 | 49 | miniDoc, |
55 | 50 | }) |
56 | 51 |
|
57 | | - await sessionStore.del() |
58 | | - |
59 | 52 | return sendRedirect(event, '/') |
60 | 53 | }) |
61 | | - |
62 | | -export class StateStore implements NodeSavedStateStore { |
63 | | - private readonly stateKey = 'oauth:bluesky:stat' |
64 | | - |
65 | | - constructor(private event: H3Event) {} |
66 | | - |
67 | | - async get(): Promise<NodeSavedState | undefined> { |
68 | | - const result = getCookie(this.event, this.stateKey) |
69 | | - if (!result) return |
70 | | - return JSON.parse(atob(result)) |
71 | | - } |
72 | | - |
73 | | - async set(key: string, val: NodeSavedState) { |
74 | | - setCookie(this.event, this.stateKey, btoa(JSON.stringify(val))) |
75 | | - } |
76 | | - |
77 | | - async del() { |
78 | | - deleteCookie(this.event, this.stateKey) |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -export class SessionStore implements NodeSavedSessionStore { |
83 | | - private readonly sessionKey = 'oauth:bluesky:session' |
84 | | - |
85 | | - constructor(private event: H3Event) {} |
86 | | - |
87 | | - async get(): Promise<NodeSavedSession | undefined> { |
88 | | - const result = getCookie(this.event, this.sessionKey) |
89 | | - if (!result) return |
90 | | - return JSON.parse(atob(result)) |
91 | | - } |
92 | | - |
93 | | - async set(key: string, val: NodeSavedSession) { |
94 | | - setCookie(this.event, this.sessionKey, btoa(JSON.stringify(val))) |
95 | | - } |
96 | | - |
97 | | - async del() { |
98 | | - deleteCookie(this.event, this.sessionKey) |
99 | | - } |
100 | | -} |
0 commit comments