From 1c93ac00ef0f5c0c9fd40882af4052585e54d5d5 Mon Sep 17 00:00:00 2001 From: m5r Date: Fri, 27 Aug 2021 03:17:46 +0800 Subject: [PATCH] redirect to landing page --- app/auth/mutations/login.ts | 32 ++++++++++++++++----- app/auth/pages/reset-password.tsx | 2 +- app/onboarding/pages/welcome/step-one.tsx | 2 +- app/onboarding/pages/welcome/step-three.tsx | 2 +- app/onboarding/pages/welcome/step-two.tsx | 2 +- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/auth/mutations/login.ts b/app/auth/mutations/login.ts index e4bf10f..2eeaf05 100644 --- a/app/auth/mutations/login.ts +++ b/app/auth/mutations/login.ts @@ -1,12 +1,23 @@ import { resolver, SecurePassword, AuthenticationError } from "blitz"; -import db, { GlobalRole } from "../../../db"; +import db from "../../../db"; 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,16 @@ 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.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/auth/pages/reset-password.tsx b/app/auth/pages/reset-password.tsx index ed3c810..56ba921 100644 --- a/app/auth/pages/reset-password.tsx +++ b/app/auth/pages/reset-password.tsx @@ -19,7 +19,7 @@ const ResetPasswordPage: BlitzPage = () => {

Password Reset Successfully

- Go to the homepage + Go to the homepage

) : ( diff --git a/app/onboarding/pages/welcome/step-one.tsx b/app/onboarding/pages/welcome/step-one.tsx index 3bf0fff..8f834d4 100644 --- a/app/onboarding/pages/welcome/step-one.tsx +++ b/app/onboarding/pages/welcome/step-one.tsx @@ -29,7 +29,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { await session.$revoke(); return { redirect: { - destination: Routes.Home().pathname, + destination: Routes.LandingPage().pathname, permanent: false, }, }; diff --git a/app/onboarding/pages/welcome/step-three.tsx b/app/onboarding/pages/welcome/step-three.tsx index deacb34..f46d31f 100644 --- a/app/onboarding/pages/welcome/step-three.tsx +++ b/app/onboarding/pages/welcome/step-three.tsx @@ -94,7 +94,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res } await session.$revoke(); return { redirect: { - destination: Routes.Home().pathname, + destination: Routes.LandingPage().pathname, permanent: false, }, }; diff --git a/app/onboarding/pages/welcome/step-two.tsx b/app/onboarding/pages/welcome/step-two.tsx index 1140c62..7bf4078 100644 --- a/app/onboarding/pages/welcome/step-two.tsx +++ b/app/onboarding/pages/welcome/step-two.tsx @@ -129,7 +129,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { await session.$revoke(); return { redirect: { - destination: Routes.Home().pathname, + destination: Routes.LandingPage().pathname, permanent: false, }, };