Skip to content

Commit 1464aa1

Browse files
committed
fix(web): route guard to use the new better auth implementation
1 parent e97a465 commit 1464aa1

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

apps/api/src/lib/auth.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export const auth = betterAuth({
2424
required: true,
2525
unique: true,
2626
},
27+
role: {
28+
type: "string",
29+
required: false,
30+
defaultValue: "USER",
31+
},
2732
},
2833
},
2934
session: {
@@ -36,6 +41,17 @@ export const auth = betterAuth({
3641
"http://localhost:3000",
3742
env.APP_URL || "http://localhost:3000",
3843
],
44+
onAfterSignUp: async (user: { user: { id: string } }) => {
45+
// Check if this is the first user - make them admin
46+
const userCount = await prisma.user.count();
47+
if (userCount === 1) {
48+
// This is the first user
49+
await prisma.user.update({
50+
where: { id: user.user.id },
51+
data: { role: "ADMIN" },
52+
});
53+
}
54+
},
3955
});
4056

4157
export const authHandler = async (req: Request, res: Response) => {

apps/web/src/contexts/AuthContext.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
1212
const { data: session, isPending } = useSession();
1313

1414
const login = async (credentials: LoginCredentials) => {
15+
// Convert username to email format if it doesn't contain @
16+
const emailOrUsername = credentials.username.includes("@")
17+
? credentials.username
18+
: `${credentials.username}@dester.local`;
19+
1520
const result = await signIn.email({
16-
email: credentials.username,
21+
email: emailOrUsername,
1722
password: credentials.password || credentials.pin || "",
1823
fetchOptions: {
1924
onSuccess: () => {
@@ -73,7 +78,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
7378
id: session.user.id,
7479
username: session.user.name || session.user.email || "",
7580
email: session.user.email || "",
76-
role: "USER", // Better-auth uses different role system
81+
role:
82+
((session.user as unknown as { role?: string }).role as
83+
| "USER"
84+
| "ADMIN"
85+
| "GUEST") || "USER",
7786
createdAt:
7887
session.user.createdAt?.toString() || new Date().toISOString(),
7988
updatedAt:

apps/web/src/routes/settings/route.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ import {
33
Link,
44
Outlet,
55
redirect,
6+
useNavigate,
67
} from "@tanstack/react-router";
78
import { CogIcon, LibraryIcon, VideoIcon, ActivityIcon } from "lucide-react";
89
import { useAuth } from "@/hooks/useAuth";
10+
import { useEffect } from "react";
911

1012
export const Route = createFileRoute("/settings")({
1113
component: RouteComponent,
12-
beforeLoad: ({ location, context }) => {
13-
// Block unauthenticated users and guests from accessing settings
14-
const user = (context as { auth?: { user?: { role?: string } } })?.auth
15-
?.user;
16-
if (!user || user?.role === "GUEST") {
17-
throw redirect({ to: "/login" });
18-
}
19-
14+
beforeLoad: ({ location }) => {
2015
// If we're at the exact settings route, redirect to libraries
2116
if (location.pathname === "/settings") {
2217
throw redirect({ to: "/settings/libraries" });
@@ -25,9 +20,17 @@ export const Route = createFileRoute("/settings")({
2520
});
2621

2722
function RouteComponent() {
28-
const { user } = useAuth();
23+
const { user, isAuthenticated, isLoading } = useAuth();
24+
const navigate = useNavigate();
2925
const isAdmin = user?.role === "ADMIN";
3026

27+
// Redirect unauthenticated users or guests
28+
useEffect(() => {
29+
if (!isLoading && (!isAuthenticated || user?.role === "GUEST")) {
30+
navigate({ to: "/" });
31+
}
32+
}, [isAuthenticated, user?.role, isLoading, navigate]);
33+
3134
return (
3235
<div className="pt-[138px] px-4 max-w-7xl mx-auto flex gap-4 h-[calc(100vh-138px)]">
3336
<nav className="max-w-sm w-full bg-white/10 backdrop-blur-lg rounded-xl p-2 space-y-2">

0 commit comments

Comments
 (0)