Unified · Split
| @@ -54,7 +54,12 @@ export async function resolveAccountForRequest( · 53 unmodified lines | ||
| 54 | 54 | |
| 55 | 55 | if (identity) { |
| 56 | 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 | 63 | if (anonymousById?.provider === "anonymous") { |
| 59 | 64 | return anonymousById; |
| 60 | 65 | } |
| @@ -309,6 +309,29 @@ describe("authentication", () => { · 308 unmodified lines | ||
| 309 | 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 | 335 | test("getCurrentAccount prefers authenticated identity over anonymous session", async () => { |
| 313 | 336 | const t = setup(); |
| 314 | 337 | const session = await t.mutation(api.workspace.bootstrapAnonymousSession, {}); |