|
1 |
| -import { AtpAgent, AtpSessionData } from "npm:@atproto/api"; |
| 1 | +import { AtpAgent } from "npm:@atproto/api"; |
2 | 2 | import { LUCENE, TAGS } from "./config/index.ts";
|
3 | 3 | import { getPreviousSession, saveSession } from "./redis-client.ts";
|
4 | 4 |
|
5 | 5 | const agent = new AtpAgent({
|
6 | 6 | service: Deno.env.get("ATPROTO_SERVICE_URL")!,
|
| 7 | + persistSession: async (_evt, session) => { |
| 8 | + await saveSession(session!); |
| 9 | + }, |
7 | 10 | });
|
8 | 11 |
|
9 | 12 | 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 | + } |
22 | 31 | }
|
23 | 32 | }
|
24 | 33 |
|
|
0 commit comments