-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
46 lines (39 loc) · 1.6 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { auth } from "@/auth";
import { DEFAULT_LOGIN_REDIRECT, authRoutes, publicRoutes } from "./routes";
import { NextResponse } from "next/server";
export default auth((req) => {
const { nextUrl } = req;
//eger kullanici giris yapmis ise true olur
const isLoggedIn = !!req.auth;
//createname veya error gibi sayfalar
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
//herkese acik olan sayfalar
const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
/* kullanici createname veya error gibi bir sayfada durabilsin diye
* aksi takdirde otomatik login sayfasına yonlenir */
if (isAuthRoute) return NextResponse.next();
/** eger hem public bir yerde hemde giriş yapmıs ise chat sayfasina yonlendiriyoruz */
if (isPublicRoute) {
if (isLoggedIn) {
return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl));
}
return NextResponse.next();
}
// giris yapmasina ragmen halen daha ilk ekranda ise otomatik chat sayfasına yonlendirme
if (isAuthRoute) {
if (isLoggedIn)
return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl));
return NextResponse.next();
}
//eger giris yapmamis ve public bir route icinde degil ise anasayfaya yonlendirme
if (!isLoggedIn && !isPublicRoute) {
// return NextResponse.next();
return Response.redirect(new URL("/", nextUrl));
}
//return null demek yerine alttaki kodu yazdik yeni surumde null hata veriyor
return NextResponse.next();
});
// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};