Skip to content

Commit 7b58f16

Browse files
committed
refactor: ♻️ update auth flow and UI components
- Update login verification and OTP handling- Modify auth form and login modal components- Update dashboard settings actions- Refactor session management- Enhance header and pricing section components"
1 parent 4096286 commit 7b58f16

File tree

9 files changed

+24
-28
lines changed

9 files changed

+24
-28
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { redirect } from "next/navigation";
12
import LoginModal from "~/components/layout/login-modal";
3+
import { getCurrentSession } from "~/lib/session";
24

35
export default async function Login() {
6+
const { session } = await getCurrentSession();
7+
if (session) return redirect("/dashboard");
48
return <LoginModal />;
59
}

src/app/[locale]/dashboard/settings/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use server";
22

3-
import { revalidateTag } from "next/cache";
3+
import { revalidatePath, revalidateTag } from "next/cache";
44
import prisma from "~/lib/prisma";
55
import { utapi } from "~/lib/uploadthing-server";
66
import { getImageKeyFromUrl, isOurCdnUrl } from "~/lib/utils";
@@ -12,7 +12,7 @@ export const updateUser = async (id: string, payload: payload) => {
1212
data: { ...payload },
1313
});
1414

15-
revalidateTag("session");
15+
revalidatePath("/dashboard/settings");
1616
};
1717

1818
export async function removeUserOldImageFromCDN(

src/app/[locale]/login/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getCurrentSession } from "~/lib/session";
55

66
export default async function Login() {
77
const { session } = await getCurrentSession();
8-
if (session) redirect("/");
8+
if (session) return redirect("/dashboard");
99
return (
1010
<section>
1111
<div className="container">

src/app/api/auth/login/verify-otp/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { revalidateTag } from "next/cache";
1+
import { revalidatePath } from "next/cache";
22
import { verifyVerificationCode } from "~/actions/auth";
33
import { setSessionTokenCookie } from "~/lib/cookies";
44
import prisma from "~/lib/prisma";
@@ -55,7 +55,7 @@ export const POST = async (req: Request, response: Response) => {
5555
const sessionToken = generateSessionToken();
5656
const session = await createSession(sessionToken, user.id);
5757
await setSessionTokenCookie(sessionToken, session.expiresAt);
58-
revalidateTag("session");
58+
revalidatePath("/", "layout");
5959
return new Response(null, {
6060
status: 200,
6161
});

src/components/layout/auth-form.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { zodResolver } from "@hookform/resolvers/zod";
44
import Link from "next/link";
5-
import { useRouter } from "next/navigation";
65
import { useEffect, useState } from "react";
76
import { useForm } from "react-hook-form";
87
import { z } from "zod";
@@ -21,7 +20,6 @@ const userAuthSchema = z.object({
2120
type FormData = z.infer<typeof userAuthSchema>;
2221

2322
export default function AuthForm() {
24-
const router = useRouter();
2523
const [currentStep, setCurrentStep] = useState(1);
2624
const [isLoading, setIsLoading] = useState(false);
2725
const [isGithubLoading, setIsGithubLoading] = useState(false);
@@ -180,9 +178,11 @@ export default function AuthForm() {
180178
)}
181179
{currentStep === 2 && (
182180
<>
183-
<p className="mb-4 text-balance">
184-
We&apos;ve sent a 6-digit code to your email. Please enter it below
185-
to verify your account.
181+
<p className="mb-4">
182+
<span className="whitespace-nowrap">
183+
We&apos;ve sent a 6-digit code to {getValues("email")}
184+
</span>{" "}
185+
Please enter it below to verify your account.
186186
</p>
187187
<form
188188
onSubmit={handleSubmit(onOTPSubmit)}

src/components/layout/header/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { getCurrentSession } from "~/lib/session";
22
import { getScopedI18n } from "~/locales/server";
33
import Navbar from "./navbar";
4+
import { connection } from "next/server";
45

56
export default async function Header() {
7+
await connection();
68
const { session } = await getCurrentSession();
79
const scopedT = await getScopedI18n("header");
810
const headerText = {

src/components/layout/login-modal.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default function LoginModal() {
1414
const pathname = usePathname();
1515

1616
const IsOpen = pathname.includes("/login");
17-
1817
return (
1918
<Dialog open={IsOpen} onOpenChange={() => router.back()}>
2019
<DialogContent className="w-full max-w-[400px] rounded-md">

src/components/sections/pricing.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ export default async function Pricing() {
8686
</p>
8787
</CardContent>
8888
<CardFooter className="justify-center">
89-
<Link href="/login" className={buttonVariants()}>
89+
<Link
90+
href={user ? "/dashboard/billing" : "/login"}
91+
className={buttonVariants()}
92+
>
9093
{!subscription
9194
? "Get Started"
9295
: subscription?.isPro

src/lib/session.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
} from "@oslojs/encoding";
66
import type { Session, User } from "@prisma/client";
77
import { cookies } from "next/headers";
8-
// import { cache } from "react";
9-
import { unstable_cache as cache } from "next/cache";
108
import prisma from "./prisma";
119

1210
export function generateSessionToken(): string {
@@ -66,23 +64,13 @@ export async function validateSessionToken(
6664
return { session, user };
6765
}
6866

69-
export const getSession = cache(
70-
async (token: string | null): Promise<SessionValidationResult> => {
71-
if (token === null) {
72-
return { session: null, user: null };
73-
}
74-
return validateSessionToken(token);
75-
},
76-
["session"],
77-
{
78-
tags: ["session"],
79-
}
80-
);
81-
8267
export const getCurrentSession = async (): Promise<SessionValidationResult> => {
8368
const cookieStore = await cookies();
8469
const token = cookieStore.get("session")?.value ?? null;
85-
return getSession(token);
70+
if (token === null) {
71+
return { session: null, user: null };
72+
}
73+
return validateSessionToken(token);
8674
};
8775

8876
export async function invalidateSession(sessionId: string): Promise<void> {

0 commit comments

Comments
 (0)