Skip to content

Commit 971fb30

Browse files
committed
feat(roles): add roles management with constants and types; update middleware for role validation
1 parent 3c3b438 commit 971fb30

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/db/schema.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const profiles = pgTable("profiles", {
66
id: uuid("id").primaryKey(),
77
firstName: text("first_name"),
88
lastName: text("last_name"),
9-
avatarUrl: text("avatar_url"),
109
stripeCustomerId: text("stripe_customer_id"),
1110
role: userRoleEnum("role").default("user").notNull(),
1211
createdAt: timestamp("created_at").defaultNow(),

lib/roles.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const ROLES = {
2+
ADMIN: "admin",
3+
COACH: "coach",
4+
USER: "user",
5+
} as const;
6+
7+
export type Role = (typeof ROLES)[keyof typeof ROLES];

utils/supabase/middleware.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createServerClient } from "@supabase/ssr";
22
import { type NextRequest, NextResponse } from "next/server";
3+
import { ROLES } from "@/lib/roles";
34

45
export async function updateSession(request: NextRequest) {
56
let supabaseResponse = NextResponse.next({
@@ -46,8 +47,9 @@ export async function updateSession(request: NextRequest) {
4647

4748
// Protect /dashboard/* — admin only
4849
if (request.nextUrl.pathname.startsWith("/dashboard")) {
49-
const role = user?.app_metadata?.role;
50-
if (role !== "admin") {
50+
const { data: claimsData } = await supabase.auth.getClaims();
51+
const role = claimsData?.claims?.user_role;
52+
if (role !== ROLES.ADMIN) {
5153
const url = request.nextUrl.clone();
5254
url.pathname = "/";
5355
return NextResponse.redirect(url);

0 commit comments

Comments
 (0)