-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refactor): add user roles & admin panel with major code changes
- Loading branch information
Showing
37 changed files
with
2,090 additions
and
836 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use server"; | ||
|
||
import { revalidatePath } from "next/cache"; | ||
import { auth } from "@/auth"; | ||
import { UserRole } from "@prisma/client"; | ||
|
||
import { prisma } from "@/lib/db"; | ||
import { userRoleSchema } from "@/lib/validations/user"; | ||
|
||
export type FormData = { | ||
role: UserRole; | ||
}; | ||
|
||
export async function updateUserRole(userId: string, data: FormData) { | ||
try { | ||
const session = await auth(); | ||
|
||
if (!session?.user || session?.user.id !== userId) { | ||
throw new Error("Unauthorized"); | ||
} | ||
|
||
const { role } = userRoleSchema.parse(data); | ||
|
||
// Update the user role. | ||
await prisma.user.update({ | ||
where: { | ||
id: userId, | ||
}, | ||
data: { | ||
role: role, | ||
}, | ||
}); | ||
|
||
revalidatePath("/dashboard/settings"); | ||
return { status: "success" }; | ||
} catch (error) { | ||
// console.log(error) | ||
return { status: "error" }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
import { redirect } from "next/navigation"; | ||
|
||
import { getCurrentUser } from "@/lib/session"; | ||
|
||
interface AuthLayoutProps { | ||
children: React.ReactNode | ||
children: React.ReactNode; | ||
} | ||
|
||
export default function AuthLayout({ children }: AuthLayoutProps) { | ||
return <div className="min-h-screen">{children}</div> | ||
export default async function AuthLayout({ children }: AuthLayoutProps) { | ||
const user = await getCurrentUser(); | ||
|
||
if (user) { | ||
if (user.role === "ADMIN") redirect("/admin"); | ||
redirect("/dashboard"); | ||
} | ||
|
||
return <div className="min-h-screen">{children}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { redirect } from "next/navigation"; | ||
|
||
import { getCurrentUser } from "@/lib/session"; | ||
import { constructMetadata } from "@/lib/utils"; | ||
import { DashboardHeader } from "@/components/dashboard/header"; | ||
import InfoCard from "@/components/dashboard/info-card"; | ||
import { DashboardShell } from "@/components/dashboard/shell"; | ||
import TransactionsList from "@/components/dashboard/transactions-list"; | ||
|
||
export const metadata = constructMetadata({ | ||
title: "Admin – SaaS Starter", | ||
description: "Admin page for only admin management.", | ||
}); | ||
|
||
export default async function AdminPage() { | ||
const user = await getCurrentUser(); | ||
if (!user || user.role !== "ADMIN") redirect("/login"); | ||
|
||
return ( | ||
<DashboardShell> | ||
<DashboardHeader heading="Admin" text="Access only for admin." /> | ||
<div className="flex flex-col gap-5"> | ||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3"> | ||
<InfoCard /> | ||
<InfoCard /> | ||
<InfoCard /> | ||
</div> | ||
<TransactionsList /> | ||
</div> | ||
</DashboardShell> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 9 additions & 12 deletions
21
app/(dashboard)/dashboard/page.tsx → app/(protected)/dashboard/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.