Skip to content

Commit

Permalink
Add user route to zod next validation
Browse files Browse the repository at this point in the history
  • Loading branch information
T1LT committed Jan 16, 2024
1 parent 04c0fa7 commit 6ad6be7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/(auth)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const UserSchema = z.object({
"/recipes?filter=all",
"/recipes?filter=popular",
"/add-recipe",
"/user",
])
.optional()
.default("/"),
Expand Down
13 changes: 11 additions & 2 deletions app/user/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { redirect } from "next/navigation";
import { db, likesTable, recipesTable, usersTable } from "@/app/db";
import { eq, sql } from "drizzle-orm";
import Link from "next/link";
import { cookies } from "next/headers";

async function getSubmittedRecipes(userId: string) {
return await db
Expand Down Expand Up @@ -32,9 +33,17 @@ async function getLikedRecipes(userId: string) {
}

export default async function UserPage() {
const cookieJar = cookies();

if (!cookieJar.getAll().length) {
redirect("/login/next/add-recipe");
}

const session = await auth();

if (!session?.user?.id) redirect("/login/next/user");
if (!session?.user?.id) {
return redirect("/login/next/user");
}

const user = (
await db
Expand All @@ -44,7 +53,7 @@ export default async function UserPage() {
.limit(1)
)[0];

if (!user) redirect("/login/next/user");
if (!user) redirect("/login");

const submittedRecipes = await getSubmittedRecipes(user.id);
const likedRecipes = await getLikedRecipes(user.id);
Expand Down

0 comments on commit 6ad6be7

Please sign in to comment.