From b7352024bfaf4dcfa4b593a9f29baac08d8a81af Mon Sep 17 00:00:00 2001 From: rotorsoft Date: Mon, 23 Dec 2024 13:34:18 -0500 Subject: [PATCH] rename flag, add to unit test --- libs/model/src/user/SignIn.command.ts | 6 +++--- libs/model/test/user/signin-lifecycle.spec.ts | 3 +++ libs/schemas/src/commands/user.schemas.ts | 2 +- packages/commonwealth/server/api/user.ts | 7 +++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/libs/model/src/user/SignIn.command.ts b/libs/model/src/user/SignIn.command.ts index 5a3172f1958..94f11e1d68f 100644 --- a/libs/model/src/user/SignIn.command.ts +++ b/libs/model/src/user/SignIn.command.ts @@ -59,8 +59,8 @@ export function SignIn(): Command { const { base, encodedAddress, ss58Prefix, hex, existingHexUserId } = actor .user.auth as VerifiedAddress; - const signed_in = actor.user.id > 0; - let user_id = signed_in ? actor.user.id : (existingHexUserId ?? null); + const was_signed_in = actor.user.id > 0; + let user_id = was_signed_in ? actor.user.id : (existingHexUserId ?? null); await verifySessionSignature( deserializeCanvas(session), @@ -199,7 +199,7 @@ export function SignIn(): Command { ...addr.toJSON(), community_base: base, community_ss58_prefix: ss58Prefix, - signed_in, + was_signed_in, user_created, address_created, first_community, diff --git a/libs/model/test/user/signin-lifecycle.spec.ts b/libs/model/test/user/signin-lifecycle.spec.ts index 98b2def3d32..7b1be408a54 100644 --- a/libs/model/test/user/signin-lifecycle.spec.ts +++ b/libs/model/test/user/signin-lifecycle.spec.ts @@ -125,6 +125,7 @@ describe('SignIn Lifecycle', async () => { expect(addr!.verification_token).to.be.not.null; expect(addr!.verified).to.be.not.null; + expect(addr!.was_signed_in).to.be.false; expect(addr!.first_community).to.be.true; expect(addr!.user_created).to.be.true; expect(addr!.address_created).to.be.true; @@ -160,6 +161,7 @@ describe('SignIn Lifecycle', async () => { expect(addr!).to.not.be.null; expect(addr!.User).to.be.not.null; + expect(addr!.was_signed_in).to.be.true; expect(addr!.first_community).to.be.false; expect(addr!.user_created).to.be.false; expect(addr!.address_created).to.be.false; @@ -315,6 +317,7 @@ describe('SignIn Lifecycle', async () => { }, }); expect(transferred).to.not.be.null; + expect(transferred!.was_signed_in).to.be.true; expect(transferred!.address).to.be.equal(ref.address); // check that user 2 now owns 2 addresses from user 1 diff --git a/libs/schemas/src/commands/user.schemas.ts b/libs/schemas/src/commands/user.schemas.ts index 05b9e562e03..4aca0073d33 100644 --- a/libs/schemas/src/commands/user.schemas.ts +++ b/libs/schemas/src/commands/user.schemas.ts @@ -15,7 +15,7 @@ export const SignIn = { output: Address.extend({ community_base: z.nativeEnum(ChainBase), community_ss58_prefix: z.number().nullish(), - signed_in: z.boolean().describe('True when user was already signed in'), + was_signed_in: z.boolean().describe('True when user was already signed in'), user_created: z .boolean() .describe( diff --git a/packages/commonwealth/server/api/user.ts b/packages/commonwealth/server/api/user.ts index c26d67a77af..264f0ce725b 100644 --- a/packages/commonwealth/server/api/user.ts +++ b/packages/commonwealth/server/api/user.ts @@ -10,7 +10,10 @@ export const trpcRouter = trpc.router({ signIn: trpc.command(User.SignIn, trpc.Tag.User, [ async (input, output, ctx) => { await new Promise((resolve, reject) => { - if (output.signed_in) return resolve(true); + // no need to login if we're already signed in + if (output.was_signed_in) return resolve(true); + + // complete passport login ctx.req.login(output.User as Express.User, (err) => { if (err) { analytics().track( @@ -33,7 +36,7 @@ export const trpcRouter = trpc.router({ MixpanelUserSignupEvent.NEW_USER_SIGNUP, { community_id: output.community_id }, ] - : !output.signed_in + : !output.was_signed_in ? [ MixpanelLoginEvent.LOGIN_COMPLETED, {