Skip to content

Commit 46d966b

Browse files
committed
fix: BSky token refresh
1 parent a876759 commit 46d966b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

supabase/functions/repost-bot/bsky-client.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1-
import { AtpAgent, AtpSessionData } from "npm:@atproto/api";
1+
import { AtpAgent } from "npm:@atproto/api";
22
import { LUCENE, TAGS } from "./config/index.ts";
33
import { getPreviousSession, saveSession } from "./redis-client.ts";
44

55
const agent = new AtpAgent({
66
service: Deno.env.get("ATPROTO_SERVICE_URL")!,
7+
persistSession: async (_evt, session) => {
8+
await saveSession(session!);
9+
},
710
});
811

912
export async function login() {
10-
let response;
11-
const previousSession = await getPreviousSession();
12-
if (previousSession) {
13-
response = await agent.resumeSession(previousSession as AtpSessionData);
14-
}
15-
16-
if (!response?.success) {
17-
response = await agent.login({
18-
identifier: Deno.env.get("BSKY_IDENTIFIER")!,
19-
password: Deno.env.get("BSKY_APP_PASSWORD")!,
20-
});
21-
await saveSession(response.data as AtpSessionData);
13+
try {
14+
const previousSession = await getPreviousSession();
15+
if (previousSession) {
16+
await agent.resumeSession({
17+
did: previousSession.did,
18+
active: previousSession.active,
19+
handle: previousSession.handle,
20+
accessJwt: previousSession.accessJwt,
21+
refreshJwt: previousSession.refreshJwt,
22+
});
23+
}
24+
} catch (error) {
25+
if (error.error === "ExpiredToken") {
26+
await agent.login({
27+
identifier: Deno.env.get("BSKY_IDENTIFIER")!,
28+
password: Deno.env.get("BSKY_APP_PASSWORD")!,
29+
});
30+
}
2231
}
2332
}
2433

supabase/functions/repost-bot/redis-client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const redis = Redis.fromEnv({
55
enableAutoPipelining: false,
66
});
77

8-
export async function getPreviousSession() {
8+
export async function getPreviousSession(): Promise<AtpSessionData> {
99
const session = await redis.get(`session-${Deno.env.get("BSKY_IDENTIFIER")}`);
1010

11-
return session;
11+
return session as AtpSessionData;
1212
}
1313

1414
export async function saveSession(session: AtpSessionData) {

0 commit comments

Comments
 (0)