Scratch space for learning atproto app development

Factor out getSessionAgent

Changed files
+38 -22
src
auth
routes
+32 -4
src/auth/session.ts
···
import type { IncomingMessage, ServerResponse } from 'node:http'
import { getIronSession } from 'iron-session'
import { env } from '#/env'
+
import { AppContext } from '#/config'
export type Session = { did: string }
-
export async function createSession(req: IncomingMessage, res: ServerResponse<IncomingMessage>, did: string) {
+
export async function createSession(
+
req: IncomingMessage,
+
res: ServerResponse<IncomingMessage>,
+
did: string
+
) {
const session = await getSessionRaw(req, res)
assert(!session.did, 'session already exists')
session.did = did
···
return { did: session.did }
}
-
export async function destroySession(req: IncomingMessage, res: ServerResponse<IncomingMessage>) {
+
export async function destroySession(
+
req: IncomingMessage,
+
res: ServerResponse<IncomingMessage>
+
) {
const session = await getSessionRaw(req, res)
await session.destroy()
return null
}
-
export async function getSession(req: IncomingMessage, res: ServerResponse<IncomingMessage>) {
+
export async function getSession(
+
req: IncomingMessage,
+
res: ServerResponse<IncomingMessage>
+
) {
const session = await getSessionRaw(req, res)
if (!session.did) return null
return { did: session.did }
}
-
async function getSessionRaw(req: IncomingMessage, res: ServerResponse<IncomingMessage>) {
+
export async function getSessionAgent(
+
req: IncomingMessage,
+
res: ServerResponse<IncomingMessage>,
+
ctx: AppContext
+
) {
+
const session = await getSessionRaw(req, res)
+
if (!session.did) return null
+
return await ctx.oauthClient.restore(session.did).catch(async (err) => {
+
ctx.logger.warn({ err }, 'oauth restore failed')
+
await destroySession(req, res)
+
return null
+
})
+
}
+
+
async function getSessionRaw(
+
req: IncomingMessage,
+
res: ServerResponse<IncomingMessage>
+
) {
return await getIronSession<Session>(req, res, {
cookieName: 'sid',
password: env.COOKIE_SECRET,
+6 -18
src/routes/index.ts
···
import { OAuthResolverError } from '@atproto/oauth-client-node'
import { isValidHandle } from '@atproto/syntax'
import express from 'express'
-
import { createSession, destroySession, getSession } from '#/auth/session'
+
import { createSession, destroySession, getSessionAgent } from '#/auth/session'
import type { AppContext } from '#/config'
import { home } from '#/pages/home'
import { login } from '#/pages/login'
···
router.get(
'/',
handler(async (req, res) => {
-
const session = await getSession(req, res)
-
const agent =
-
session &&
-
(await ctx.oauthClient.restore(session.did).catch(async (err) => {
-
ctx.logger.warn({ err }, 'oauth restore failed')
-
await destroySession(req, res)
-
return null
-
}))
+
const agent = await getSessionAgent(req, res, ctx)
const statuses = await ctx.db
.selectFrom('status')
.selectAll()
···
if (!agent) {
return res.type('html').send(page(home({ statuses, didHandleMap })))
}
-
const { data: profile } = await agent.getProfile({ actor: session.did })
+
const { data: profile } = await agent.getProfile({
+
actor: agent.accountDid,
+
})
return res
.type('html')
.send(page(home({ statuses, didHandleMap, profile, myStatus })))
···
router.post(
'/status',
handler(async (req, res) => {
-
const session = await getSession(req, res)
-
const agent =
-
session &&
-
(await ctx.oauthClient.restore(session.did).catch(async (err) => {
-
ctx.logger.warn({ err }, 'oauth restore failed')
-
await destroySession(req, res)
-
return null
-
}))
+
const agent = await getSessionAgent(req, res, ctx)
if (!agent) {
return res.status(401).json({ error: 'Session required' })
}