···
1
+
import assert from 'node:assert'
import path from 'node:path'
3
+
import type { IncomingMessage, ServerResponse } from 'node:http'
import { OAuthResolverError } from '@atproto/oauth-client-node'
import { isValidHandle } from '@atproto/syntax'
import express from 'express'
5
-
import { createSession, destroySession, getSessionAgent } from '#/auth/session'
7
+
import { getIronSession } from 'iron-session'
import type { AppContext } from '#/index'
import { home } from '#/pages/home'
import { login } from '#/pages/login'
11
+
import { env } from '#/lib/env'
import { page } from '#/lib/view'
import * as Status from '#/lexicon/types/com/example/status'
15
+
type Session = { did: string }
// Helper function for defining routes
···
32
+
// Helper function to get the Atproto Agent for the active session
33
+
async function getSessionAgent(
34
+
req: IncomingMessage,
35
+
res: ServerResponse<IncomingMessage>,
38
+
const session = await getIronSession<Session>(req, res, {
40
+
password: env.COOKIE_SECRET,
42
+
if (!session.did) return null
43
+
return await ctx.oauthClient.restore(session.did).catch(async (err) => {
44
+
ctx.logger.warn({ err }, 'oauth restore failed')
45
+
await session.destroy()
export const createRouter = (ctx: AppContext) => {
const router = express.Router()
···
const params = new URLSearchParams(req.originalUrl.split('?')[1])
const { agent } = await ctx.oauthClient.callback(params)
48
-
await createSession(req, res, agent.accountDid)
71
+
const session = await getIronSession<Session>(req, res, {
73
+
password: env.COOKIE_SECRET,
75
+
assert(!session.did, 'session already exists')
76
+
session.did = agent.accountDid
77
+
await session.save()
ctx.logger.error({ err }, 'oauth callback failed')
return res.redirect('/?error')
···
handler(async (req, res) => {
99
-
await destroySession(req, res)
128
+
const session = await getIronSession<Session>(req, res, {
130
+
password: env.COOKIE_SECRET,
132
+
await session.destroy()