From b4aa79bd85808d96584dfe938a6fe842af74aa46 Mon Sep 17 00:00:00 2001 From: m5r Date: Sun, 8 Aug 2021 13:34:51 +0800 Subject: [PATCH] implement session creation when logging in --- app/auth/mutations/login.ts | 32 +++++++++++++++---- .../components/conversations-list.tsx | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/auth/mutations/login.ts b/app/auth/mutations/login.ts index e4bf10f..cd8c111 100644 --- a/app/auth/mutations/login.ts +++ b/app/auth/mutations/login.ts @@ -6,7 +6,18 @@ import { Login } from "../validations"; export const authenticateUser = async (rawEmail: string, rawPassword: string) => { const email = rawEmail.toLowerCase().trim(); const password = rawPassword.trim(); - const user = await db.user.findFirst({ where: { email } }); + const user = await db.user.findFirst({ + where: { email }, + include: { + memberships: { + include: { + organization: { + include: { phoneNumbers: true }, + }, + }, + }, + }, + }); if (!user) throw new AuthenticationError(); const result = await SecurePassword.verify(user.hashedPassword, password); @@ -14,7 +25,10 @@ export const authenticateUser = async (rawEmail: string, rawPassword: string) => if (result === SecurePassword.VALID_NEEDS_REHASH) { // Upgrade hashed password with a more secure hash const improvedHash = await SecurePassword.hash(password); - await db.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } }); + await db.user.update({ + where: { id: user.id }, + data: { hashedPassword: improvedHash }, + }); } const { hashedPassword, ...rest } = user; @@ -25,12 +39,18 @@ export default resolver.pipe(resolver.zod(Login), async ({ email, password }, ct // This throws an error if credentials are invalid const user = await authenticateUser(email, password); - const hasCompletedOnboarding = undefined; // TODO + const organization = user.memberships[0]!.organization; + const hasCompletedOnboarding = + Boolean(organization.twilioAccountSid) && + Boolean(organization.twilioAuthToken) && + Boolean(organization.twilioApiKey) && + Boolean(organization.twilioApiSecret) && + Boolean(organization.phoneNumbers.length > 1); await ctx.session.$create({ userId: user.id, - roles: [user.role], - hasCompletedOnboarding, - orgId: "user.memberships[0].organizationId", + roles: [user.role, user.memberships[0]!.role], + hasCompletedOnboarding: hasCompletedOnboarding || undefined, + orgId: organization.id, }); return user; diff --git a/app/messages/components/conversations-list.tsx b/app/messages/components/conversations-list.tsx index d4b20ca..9baa4c2 100644 --- a/app/messages/components/conversations-list.tsx +++ b/app/messages/components/conversations-list.tsx @@ -17,7 +17,7 @@ export default function ConversationsList() {