History

fix anonymous identity lookup for self-hosted auth

Rhys Sullivan committed 8 months ago

Commit f80371e1 parent 640e95e2 files · +29 −1Browse files

Unified · Split

2 files
  1. executor/packages/core/src/identity.ts +6 −1
  2. executor/packages/database/convex/access-controls.test.ts +23 −0
executor/packages/core/src/identity.ts+6−1
@@ -54,7 +54,12 @@ export async function resolveAccountForRequest( · 53 unmodified lines
5454
5555 if (identity) {
5656 if (!hasNonAnonymousIdentity) {
57− const anonymousById = await ctx.db.get(identity.subject as Id<"accounts">);
57+ let anonymousById: Doc<"accounts"> | null = null;
58+ try {
59+ anonymousById = await ctx.db.get(identity.subject as Id<"accounts">);
60+ } catch {
61+ anonymousById = null;
62+ }
5863 if (anonymousById?.provider === "anonymous") {
5964 return anonymousById;
6065 }
executor/packages/database/convex/access-controls.test.ts+23−0
@@ -309,6 +309,29 @@ describe("authentication", () => { · 308 unmodified lines
309309 expect(account).toBeNull();
310310 });
311311
312+ test("getCurrentAccount resolves anonymous identity by provider account id", async () => {
313+ const t = setup();
314+ const session = await t.mutation(api.workspace.bootstrapAnonymousSession, {});
315+
316+ const providerAccountId = await t.run(async (ctx) => {
317+ const account = await ctx.db.get(session.accountId as Id<"accounts">);
318+ if (!account) {
319+ throw new Error("Expected anonymous account to exist");
320+ }
321+ return account.providerAccountId;
322+ });
323+
324+ const anonymousIdentity = t.withIdentity({
325+ subject: providerAccountId,
326+ provider: "anonymous",
327+ });
328+
329+ const account = await anonymousIdentity.query(api.app.getCurrentAccount, {});
330+ expect(account).not.toBeNull();
331+ expect(account!.provider).toBe("anonymous");
332+ expect(account!.providerAccountId).toBe(providerAccountId);
333+ });
334+
312335 test("getCurrentAccount prefers authenticated identity over anonymous session", async () => {
313336 const t = setup();
314337 const session = await t.mutation(api.workspace.bootstrapAnonymousSession, {});