Files
outline/plugins/passkeys/server/api/passkeys.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

87 lines
2.2 KiB
TypeScript

import auth from "@server/middlewares/authentication";
import validate from "@server/middlewares/validate";
import { UserPasskey } from "@server/models";
import type { Context } from "@server/types";
import { AuthenticationType } from "@server/types";
import Router from "@koa/router";
import * as T from "./schema";
import { authorize } from "@server/policies";
import { transaction } from "@server/middlewares/transaction";
import pagination from "@server/routes/api/middlewares/pagination";
import presentUserPasskey from "../presenters/userPasskey";
const router = new Router();
router.post(
"passkeys.list",
auth({ type: AuthenticationType.APP }),
pagination(),
validate(T.PasskeysListSchema),
async (ctx: Context<T.PasskeysListReq>) => {
const { user } = ctx.auth;
const { pagination } = ctx.state;
const passkeys = await UserPasskey.findAll({
where: { userId: user.id },
order: [["createdAt", "DESC"]],
offset: pagination.offset,
limit: pagination.limit,
});
ctx.body = {
pagination,
data: passkeys.map(presentUserPasskey),
};
}
);
router.post(
"passkeys.update",
auth({ type: AuthenticationType.APP }),
validate(T.PasskeysUpdateSchema),
transaction(),
async (ctx: Context<T.PasskeysUpdateReq>) => {
const { id, name } = ctx.input.body;
const { user } = ctx.auth;
const { transaction } = ctx;
const passkey = await UserPasskey.findByPk(id, {
rejectOnEmpty: true,
lock: transaction.LOCK.UPDATE,
});
authorize(user, "update", passkey);
await passkey.updateWithCtx(ctx, {
name,
});
ctx.body = { data: presentUserPasskey(passkey) };
}
);
router.post(
"passkeys.delete",
auth({ type: AuthenticationType.APP }),
validate(T.PasskeysDeleteSchema),
transaction(),
async (ctx: Context<T.PasskeysDeleteReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.auth;
const { transaction } = ctx;
const passkey = await UserPasskey.findByPk(id, {
rejectOnEmpty: true,
lock: transaction.LOCK.UPDATE,
});
authorize(user, "delete", passkey);
await passkey.destroyWithCtx(ctx, { data: { name: passkey.name } });
ctx.body = {
success: true,
};
}
);
export default router;