feat(tooling): add lint-staged with Prettier + ESLint on pre-commit
- Install prettier, lint-staged, eslint-config-prettier - Configure lint-staged in package.json: Prettier write on staged files, ESLint check on .ts/.html - Add eslint-config-prettier to .eslintrc.json extends to disable formatting rules that conflict with Prettier - Configure no-unused-vars to allow _ prefix (intentional destructuring pattern) - Replace manual prettier/git update-index lines in pre-commit hook with npx lint-staged - Fix pre-existing ESLint errors: remove unused imports in herowars-guildraid, fix useless escapes in middleware.ts, fix eslint-disable comment in utilityTypes.ts
This commit is contained in:
+16
-16
@@ -1,34 +1,34 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getSession } from "./utils/auth/session";
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getSession } from './utils/auth/session';
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],
|
||||
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
|
||||
};
|
||||
|
||||
const publicRoutes = [
|
||||
/^\/article\/[^\/]+?\/?$/, // /article/sample-slug
|
||||
/^\/login\/?$/,
|
||||
/^\/profile\/[^\/]+?\/?$/, // /profile/sample-username
|
||||
/^\/profile\/[^\/]+?\/favorites\/?$/, // /profile/sample-username/favorites
|
||||
/^\/register\/?$/,
|
||||
/^\/$/,
|
||||
/^\/article\/[^/]+?\/?$/, // /article/sample-slug
|
||||
/^\/login\/?$/,
|
||||
/^\/profile\/[^/]+?\/?$/, // /profile/sample-username
|
||||
/^\/profile\/[^/]+?\/favorites\/?$/, // /profile/sample-username/favorites
|
||||
/^\/register\/?$/,
|
||||
/^\/$/,
|
||||
] as const;
|
||||
|
||||
const isPublicRoutes = (pathname: string) => {
|
||||
return publicRoutes.some((route) => route.test(pathname));
|
||||
return publicRoutes.some((route) => route.test(pathname));
|
||||
};
|
||||
|
||||
const isPrivateRoutes = (pathname: string) => {
|
||||
return !isPublicRoutes(pathname);
|
||||
return !isPublicRoutes(pathname);
|
||||
};
|
||||
|
||||
const middleware = async (req: NextRequest) => {
|
||||
const session = await getSession();
|
||||
if (isPrivateRoutes(req.nextUrl.pathname) && session == null) {
|
||||
return NextResponse.redirect(new URL("/login", req.nextUrl));
|
||||
}
|
||||
const session = await getSession();
|
||||
if (isPrivateRoutes(req.nextUrl.pathname) && session == null) {
|
||||
return NextResponse.redirect(new URL('/login', req.nextUrl));
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
return NextResponse.next();
|
||||
};
|
||||
|
||||
export default middleware;
|
||||
|
||||
Reference in New Issue
Block a user