From 8be519c633709bc3e59b923be33bbe9da3727cff Mon Sep 17 00:00:00 2001 From: Jaleel Bennett Date: Fri, 23 Aug 2024 10:31:35 -0400 Subject: [PATCH] refactor(site-header): improve authentication flow and error handling when loading user session and profile --- components/icons.tsx | 2 +- components/site-header.tsx | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/components/icons.tsx b/components/icons.tsx index e27a349..b09f970 100644 --- a/components/icons.tsx +++ b/components/icons.tsx @@ -121,7 +121,7 @@ export const Icons = { {..._props} stroke="currentColor" fill="currentColor" - stroke-width="0" + strokeWidth="0" viewBox="0 0 488 512" height="1em" width="1em" diff --git a/components/site-header.tsx b/components/site-header.tsx index ea9efab..d35152a 100644 --- a/components/site-header.tsx +++ b/components/site-header.tsx @@ -4,6 +4,7 @@ import { getUser } from "@/data-access/users"; import Nav from "./nav"; import { Profile, User } from "@/server/db/schema"; import { getUserProfileUseCase } from "@/use-cases/users"; +import { redirect } from "next/navigation"; export type UserInfo = | { id: number; email: string | null; emailVerified: Date | null } @@ -11,17 +12,19 @@ export type UserInfo = | undefined; async function SiteHeader() { - let userSession = await getCurrentUser(); - let user: User | undefined = undefined; - let profile: Profile | undefined = undefined; + const userSession = await getCurrentUser(); if (!userSession) { - user = undefined; + redirect("/signin"); } - if (userSession) { - user = await getUser(userSession.id); - profile = await getUserProfileUseCase(userSession.id); + const [user, profile] = await Promise.all([ + getUser(userSession.id), + getUserProfileUseCase(userSession.id), + ]); + + if (!user || !profile) { + redirect("/signin"); } return