feat: add addAccountByToken tRPC mutation with upsert logic and tests

Adds a new mutation that accepts an email + raw OAuth token and upserts
the account — creating it if it doesn't exist, updating credentials if
it does. Covers all four scenarios with unit tests (new, existing,
empty-email, empty-token validation).
This commit is contained in:
Lukas May
2026-03-06 10:58:02 +01:00
parent f3042abe04
commit 9c4131c814
3 changed files with 94 additions and 0 deletions

View File

@@ -72,5 +72,29 @@ export function accountProcedures(publicProcedure: ProcedureBuilder) {
.query(() => {
return listProviderNames();
}),
addAccountByToken: publicProcedure
.input(z.object({
email: z.string().min(1),
provider: z.string().default('claude'),
token: z.string().min(1),
}))
.mutation(async ({ ctx, input }) => {
const repo = requireAccountRepository(ctx);
const credentials = JSON.stringify({ claudeAiOauth: { accessToken: input.token } });
const configJson = JSON.stringify({ hasCompletedOnboarding: true });
const existing = await repo.findByEmail(input.email);
if (existing) {
const account = await repo.updateAccountAuth(existing.id, configJson, credentials);
return { upserted: true, account };
}
const account = await repo.create({
email: input.email,
provider: input.provider,
configJson,
credentials,
});
return { upserted: false, account };
}),
};
}