redirect to landing page

This commit is contained in:
m5r 2021-08-27 03:17:46 +08:00
parent 20b6a7a996
commit 1c93ac00ef
5 changed files with 29 additions and 11 deletions

View File

@ -1,12 +1,23 @@
import { resolver, SecurePassword, AuthenticationError } from "blitz"; import { resolver, SecurePassword, AuthenticationError } from "blitz";
import db, { GlobalRole } from "../../../db"; import db from "../../../db";
import { Login } from "../validations"; import { Login } from "../validations";
export const authenticateUser = async (rawEmail: string, rawPassword: string) => { export const authenticateUser = async (rawEmail: string, rawPassword: string) => {
const email = rawEmail.toLowerCase().trim(); const email = rawEmail.toLowerCase().trim();
const password = rawPassword.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(); if (!user) throw new AuthenticationError();
const result = await SecurePassword.verify(user.hashedPassword, password); 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) { if (result === SecurePassword.VALID_NEEDS_REHASH) {
// Upgrade hashed password with a more secure hash // Upgrade hashed password with a more secure hash
const improvedHash = await SecurePassword.hash(password); 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; 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 // This throws an error if credentials are invalid
const user = await authenticateUser(email, password); 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({ await ctx.session.$create({
userId: user.id, userId: user.id,
roles: [user.role], roles: [user.role, user.memberships[0]!.role],
hasCompletedOnboarding, hasCompletedOnboarding: hasCompletedOnboarding || undefined,
orgId: "user.memberships[0].organizationId", orgId: organization.id,
}); });
return user; return user;

View File

@ -19,7 +19,7 @@ const ResetPasswordPage: BlitzPage = () => {
<div> <div>
<h2>Password Reset Successfully</h2> <h2>Password Reset Successfully</h2>
<p> <p>
Go to the <Link href={Routes.Home()}>homepage</Link> Go to the <Link href={Routes.LandingPage()}>homepage</Link>
</p> </p>
</div> </div>
) : ( ) : (

View File

@ -29,7 +29,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
await session.$revoke(); await session.$revoke();
return { return {
redirect: { redirect: {
destination: Routes.Home().pathname, destination: Routes.LandingPage().pathname,
permanent: false, permanent: false,
}, },
}; };

View File

@ -94,7 +94,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async ({ req, res }
await session.$revoke(); await session.$revoke();
return { return {
redirect: { redirect: {
destination: Routes.Home().pathname, destination: Routes.LandingPage().pathname,
permanent: false, permanent: false,
}, },
}; };

View File

@ -129,7 +129,7 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
await session.$revoke(); await session.$revoke();
return { return {
redirect: { redirect: {
destination: Routes.Home().pathname, destination: Routes.LandingPage().pathname,
permanent: false, permanent: false,
}, },
}; };