mirror of
https://github.com/lukevella/rallly.git
synced 2025-07-21 02:07:23 +02:00
♻️ Use middleware for protecting routes (#1010)
This commit is contained in:
parent
9d75e5112a
commit
47dd3a0ff2
2 changed files with 21 additions and 27 deletions
|
@ -4,6 +4,7 @@ import { NextResponse } from "next/server";
|
|||
import withAuth from "next-auth/middleware";
|
||||
|
||||
import { initGuest } from "@/app/guest";
|
||||
import { isSelfHosted } from "@/utils/constants";
|
||||
|
||||
const supportedLocales = Object.keys(languages);
|
||||
|
||||
|
@ -45,7 +46,26 @@ export const middleware = withAuth(
|
|||
{
|
||||
secret: process.env.SECRET_PASSWORD,
|
||||
callbacks: {
|
||||
authorized: () => true, // needs to be true to allow access to all pages
|
||||
authorized: ({ token, req }) => {
|
||||
const nextUrl = req.nextUrl;
|
||||
|
||||
if (
|
||||
isSelfHosted &&
|
||||
token?.email === null &&
|
||||
!(
|
||||
nextUrl.pathname.startsWith("/invite") ||
|
||||
nextUrl.pathname.startsWith("/login") ||
|
||||
nextUrl.pathname.startsWith("/register") ||
|
||||
nextUrl.pathname.startsWith("/auth") ||
|
||||
nextUrl.pathname.startsWith("/p")
|
||||
)
|
||||
) {
|
||||
// limit which pages guests can access for self-hosted instances
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue