···
+
import assert from 'node:assert'
import path from 'node:path'
+
import type { IncomingMessage, ServerResponse } from 'node:http'
import { OAuthResolverError } from '@atproto/oauth-client-node'
import { isValidHandle } from '@atproto/syntax'
import express from 'express'
+
import { getIronSession } from 'iron-session'
import type { AppContext } from '#/index'
import { home } from '#/pages/home'
import { login } from '#/pages/login'
+
import { env } from '#/lib/env'
import { page } from '#/lib/view'
import * as Status from '#/lexicon/types/com/example/status'
+
type Session = { did: string }
// Helper function for defining routes
···
+
// Helper function to get the Atproto Agent for the active session
+
async function getSessionAgent(
+
res: ServerResponse<IncomingMessage>,
+
const session = await getIronSession<Session>(req, res, {
+
password: env.COOKIE_SECRET,
+
if (!session.did) return null
+
return await ctx.oauthClient.restore(session.did).catch(async (err) => {
+
ctx.logger.warn({ err }, 'oauth restore failed')
+
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)
+
const session = await getIronSession<Session>(req, res, {
+
password: env.COOKIE_SECRET,
+
assert(!session.did, 'session already exists')
+
session.did = agent.accountDid
ctx.logger.error({ err }, 'oauth callback failed')
return res.redirect('/?error')
···
handler(async (req, res) => {
+
const session = await getIronSession<Session>(req, res, {
+
password: env.COOKIE_SECRET,
+
await session.destroy()