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
54 54
55 if (identity) {55 if (identity) {
56 if (!hasNonAnonymousIdentity) {56 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 }
58 if (anonymousById?.provider === "anonymous") {63 if (anonymousById?.provider === "anonymous") {
59 return anonymousById;64 return anonymousById;
60 }65 }
executor/packages/database/convex/access-controls.test.ts+23−0
@@ -309,6 +309,29 @@ describe("authentication", () => { · 308 unmodified lines
309 expect(account).toBeNull();309 expect(account).toBeNull();
310 });310 });
311 311
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
312 test("getCurrentAccount prefers authenticated identity over anonymous session", async () => {335 test("getCurrentAccount prefers authenticated identity over anonymous session", async () => {
313 const t = setup();336 const t = setup();
314 const session = await t.mutation(api.workspace.bootstrapAnonymousSession, {});337 const session = await t.mutation(api.workspace.bootstrapAnonymousSession, {});