Skip to content

Commit 0e212d8

Browse files
committed
middleware fix
1 parent a3d8bb1 commit 0e212d8

File tree

12 files changed

+230
-64
lines changed

12 files changed

+230
-64
lines changed

Diff for: app/actions/action.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use server'
2+
3+
import { revalidatePath } from 'next/cache'
4+
import { redirect } from 'next/navigation'
5+
import { createClient } from '@/utils/supabase/server'
6+
// import { signinSchema } from '@/validations/validation'
7+
8+
// export async function login(data: { email: string, password: string }) {
9+
// const supabase = await createClient()
10+
// const { success, error } = signinSchema.safeParse(data);
11+
// if (!success) {
12+
// console.log(error);
13+
// return;
14+
// }
15+
// // const data = {
16+
// // email: formData.get('email') as string,
17+
// // password: formData.get('password') as string,
18+
// // }
19+
// const { error: signInError } = await supabase.auth.signInWithPassword(data)
20+
// if (signInError) {
21+
// console.log(signInError);
22+
// return
23+
// }
24+
25+
// revalidatePath('/', 'layout')
26+
// redirect('/dashboard');
27+
// }
28+
29+
// export async function signup(formData: FormData) {
30+
// const supabase = await createClient()
31+
// const data = {
32+
// email: formData.get('email') as string,
33+
// password: formData.get('password') as string,
34+
// }
35+
36+
// const { error } = await supabase.auth.signUp(data)
37+
38+
// if (error) {
39+
// redirect('/error')
40+
// }
41+
42+
// revalidatePath('/', 'layout')
43+
// redirect('/')
44+
// }
45+
46+
export async function signout() {
47+
const supabase = await createClient();
48+
await supabase.auth.signOut();
49+
revalidatePath('/', 'layout');
50+
redirect('/auth/signin');
51+
}

Diff for: app/api/auth/register/route.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import prisma from "@/lib/database/prismaClient";
2-
import { supabase } from "@/lib/supabaseClient";
32
import { hashPassword } from "@/utils/hashing";
3+
import { createClient } from "@/utils/supabase/server";
44
import { signupSchema } from "@/validations/validation";
55
import { NextRequest, NextResponse } from "next/server";
66

77
export async function POST(request: NextRequest) {
8+
const supabase = await createClient();
89
const body = await request.json();
910
const { email, password, fullName, leetcodeUsername, gender } = body;
1011
try {

Diff for: app/api/auth/user/route.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import prisma from '@/lib/database/prismaClient';
22
import { NextResponse } from 'next/server';
3+
import { createClient } from '@/utils/supabase/server'
34

4-
export async function POST(request: Request) {
5+
export async function GET(request: Request) {
56
try {
6-
7-
const { supabaseId } = await request.json();
7+
const supabase = await createClient();
8+
const supabaseId = (await supabase.auth.getUser()).data.user?.id;
89
const userData = await prisma.user.findUnique({
910
where: { supabaseId },
1011
select: {
@@ -23,7 +24,7 @@ export async function POST(request: Request) {
2324
return NextResponse.json({ message: 'User not found' }, { status: 404 });
2425
}
2526

26-
return NextResponse.json({ user: userData },{status: 200});
27+
return NextResponse.json({ user: userData }, { status: 200 });
2728
} catch (err) {
2829
console.error('Error fetching user data:', err);
2930
return NextResponse.json({ message: 'Error fetching user data' }, { status: 500 });

Diff for: components/AuthComponent/SigninForm.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import { signinSchema } from "@/validations/validation";
3333
type SigninFormValues = z.infer<typeof signinSchema>;
3434

3535
export default function SigninForm() {
36-
const { isSigningIn, signin, signinError } = useAuthStore();
37-
const router = useRouter();
36+
const router = useRouter()
37+
const { isSigningIn,signin, signinError } = useAuthStore();
3838
const [showPassword, setShowPassword] = useState(false);
3939

4040
const form = useForm<SigninFormValues>({
@@ -46,7 +46,7 @@ export default function SigninForm() {
4646
});
4747

4848
const onSubmit = (data: SigninFormValues) => {
49-
signin(data, router);
49+
signin(data, router)
5050
};
5151

5252
return (

Diff for: components/header.tsx

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
"use client"
1+
"use client";
22

33
import { useState } from "react";
44
import Link from "next/link";
55
import { Button } from "@/components/ui/button";
66
import { ThemeToggle } from "@/components/theme-toggle";
77
import { supabase } from "@/lib/supabaseClient";
8-
import { useRouter } from 'next/navigation';
8+
import { useRouter } from "next/navigation";
9+
import { signout } from "@/app/actions/action";
910

1011
const Navbar = ({ userId }: { userId?: string }) => {
1112
const [isMenuOpen, setIsMenuOpen] = useState(false);
12-
const router = useRouter();
13+
const router = useRouter();
1314
return (
1415
<header>
1516
<nav className="flex justify-between items-center px-6 py-4 bg-neutral-900 text-white dark:bg-neutral-800">
1617
<div className="flex items-center space-x-2">
1718
<span className="text-2xl font-bold">📓</span>
18-
<span className="text-xl font-semibold">
19-
LeetCodeJournal
20-
</span>
19+
<span className="text-xl font-semibold">LeetCodeJournal</span>
2120
</div>
2221
<div className="hidden lg:flex space-x-6">
2322
<Link href="/" className="hover:text-purple-400 ">
@@ -32,14 +31,7 @@ const Navbar = ({ userId }: { userId?: string }) => {
3231
</div>
3332
<div className="hidden lg:flex items-center space-x-4">
3433
{userId ? (
35-
<Button
36-
variant="destructive"
37-
onClick={() =>
38-
supabase.auth
39-
.signOut()
40-
.then(() => router.push("/"))
41-
}
42-
>
34+
<Button variant="destructive" onClick={signout}>
4335
Sign Out
4436
</Button>
4537
) : (

Diff for: middleware.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import { NextResponse } from 'next/server';
2-
import { supabase } from '@/lib/supabaseClient';
1+
import { type NextRequest } from 'next/server'
2+
import { updateSession } from '@/utils/supabase/middleware'
33

4-
export async function middleware(req: Request) {
5-
const { data: session } = await supabase.auth.getSession();
6-
const { pathname } = new URL(req.url);
7-
8-
if (pathname.startsWith('/dashboard') && !session) {
9-
return NextResponse.redirect(('/login'));
10-
}
11-
12-
return NextResponse.next();
4+
export async function middleware(request: NextRequest) {
5+
return await updateSession(request)
136
}
147

158
export const config = {
16-
matcher: ['/dashboard/:path*'],
17-
};
9+
matcher: [
10+
/*
11+
* Match all request paths except for the ones starting with:
12+
* - _next/static (static files)
13+
* - _next/image (image optimization files)
14+
* - favicon.ico (favicon file)
15+
* Feel free to modify this pattern to include more paths.
16+
*/
17+
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
18+
],
19+
}

Diff for: package-lock.json

+26-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@radix-ui/react-separator": "^1.1.1",
2121
"@radix-ui/react-slot": "^1.1.1",
2222
"@radix-ui/react-tabs": "^1.1.2",
23+
"@supabase/ssr": "^0.5.2",
2324
"@types/bcryptjs": "^2.4.6",
2425
"axios": "^1.7.9",
2526
"bcryptjs": "^2.4.3",

0 commit comments

Comments
 (0)