fix: missing account id in response

This commit is contained in:
Steve Korshakov 2025-07-18 23:33:44 -07:00
parent c17aaf25cb
commit 7dcb7f30af
3 changed files with 26 additions and 17 deletions

View File

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "TerminalAuthRequest" ADD COLUMN "responseAccountId" TEXT;
-- AddForeignKey
ALTER TABLE "TerminalAuthRequest" ADD CONSTRAINT "TerminalAuthRequest_responseAccountId_fkey" FOREIGN KEY ("responseAccountId") REFERENCES "Account"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@ -19,22 +19,25 @@ datasource db {
// //
model Account { model Account {
id String @id @default(cuid()) id String @id @default(cuid())
publicKey String @unique publicKey String @unique
seq Int @default(0) seq Int @default(0)
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
Session Session[] Session Session[]
Update Update[] Update Update[]
AccountPushToken AccountPushToken[] AccountPushToken AccountPushToken[]
TerminalAuthRequest TerminalAuthRequest[]
} }
model TerminalAuthRequest { model TerminalAuthRequest {
id String @id @default(cuid()) id String @id @default(cuid())
publicKey String @unique publicKey String @unique
response String? response String?
createdAt DateTime @default(now()) responseAccountId String?
updatedAt DateTime @updatedAt responseAccount Account? @relation(fields: [responseAccountId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
} }
model AccountPushToken { model AccountPushToken {

View File

@ -188,12 +188,12 @@ export async function startApi() {
const answer = await db.terminalAuthRequest.upsert({ const answer = await db.terminalAuthRequest.upsert({
where: { publicKey: privacyKit.encodeHex(publicKey) }, where: { publicKey: privacyKit.encodeHex(publicKey) },
update: { updatedAt: new Date() }, update: {},
create: { publicKey: privacyKit.encodeHex(publicKey) } create: { publicKey: privacyKit.encodeHex(publicKey) }
}); });
if (answer.response) { if (answer.response && answer.responseAccountId) {
const token = await tokenGenerator.new({ user: answer.id, extras: { session: answer.id } }); const token = await tokenGenerator.new({ user: answer.responseAccountId!, extras: { session: answer.id } });
return reply.send({ return reply.send({
state: 'authorized', state: 'authorized',
token: token, token: token,
@ -206,6 +206,7 @@ export async function startApi() {
// Approve auth request // Approve auth request
typed.post('/v1/auth/response', { typed.post('/v1/auth/response', {
preHandler: app.authenticate,
schema: { schema: {
body: z.object({ body: z.object({
response: z.string(), response: z.string(),
@ -227,7 +228,7 @@ export async function startApi() {
if (!authRequest.response) { if (!authRequest.response) {
await db.terminalAuthRequest.update({ await db.terminalAuthRequest.update({
where: { id: authRequest.id }, where: { id: authRequest.id },
data: { response: request.body.response } data: { response: request.body.response, responseAccountId: request.user.id }
}); });
} }
return reply.send({ success: true }); return reply.send({ success: true });