next.js:_middleware with nextresponse blodering的图像

发布于 2025-01-27 22:20:38 字数 343 浏览 1 评论 0原文

这个问题扩展了这个问题。 Next.js中的_middleware带有import {nextresponse}从“ Next/Server”;可用于JWT身份验证,但会阻止所有包括图像的路由。这意味着,如果您想通过CSS或图像在重定向路由中加载的图像,则不会加载。下面的代码封锁地址栏重定向并允许图像加载。访问令牌可能会更好

This question extends this question. The _middleware in Next.js with import { NextResponse } from "next/server"; can be used for JWT authentication but blocks all the routes including images. This means that if you have images that you want to load in the redirect route by CSS or Image, will not load. The code below blocks address bar redirect and allows image load. Access Token would probably be better

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2025-02-03 22:20:38

更新:经过一些调试后,这就是我想到的。我编写的先前代码不会让您在登录后重定向到主页。原因是_middleware似乎在 /api /登录之前运行并基于预先条件,只需将它们重定向到登录,然后返回void(_Middleware'in Redirect上)。

此更新的代码允许 /api /登录无需刷新令牌即可进行路由,如果它们在没有令牌的情况下浏览地址栏,则将其发送回登录

import { NextResponse } from "next/server";
export default function (req: {
    url?: any;
    cookies?: any;
}): void | NextResponse {
    const { cookies } = req;
    const url: string = req.url;
    const refreshToken: string | undefined = cookies?.refresh_token_extreme;
    const baseUrl: string = "http://localhost:3000";

    // vercel.svg is used in /login
    const unprotectedPaths: string[] = [
        `${baseUrl}/login`,
        `${baseUrl}/favicon.ico`,
        `${baseUrl}/vercel.svg`,
        `${baseUrl}/_next/webpack-hmr`,
        `${baseUrl}/attachables/campus-images/image1.jpg`,
        `${baseUrl}/attachables/mnhs-images/logos/login_logo.png`,
        `${baseUrl}/attachables/mnhs-images/logos/mnhs_favicon_og.ico`,
    ]
    
    
    if (unprotectedPaths.includes(url)) {
        return void 0;
    } else if (!refreshToken && url === "http://localhost:3000/api/login") {   
        return NextResponse.next();
    } else if (!refreshToken) {
        return NextResponse.redirect(`${baseUrl}/login`);
    } else { 
        return NextResponse.next();
    }
}

Update: after some debugging, this is what I've come up with. The previous code that I wrote does not let you be redirected to the home page after login. The reason being that the _Middleware seems to runs before /api/login and based on the prev conditional, just redirects them to the login again and returns void (_Middleware "includes" on redirect).

This updated code allows /api/login to be routed on without a refresh token and sends them back to login if they navigate through address bar without a token

import { NextResponse } from "next/server";
export default function (req: {
    url?: any;
    cookies?: any;
}): void | NextResponse {
    const { cookies } = req;
    const url: string = req.url;
    const refreshToken: string | undefined = cookies?.refresh_token_extreme;
    const baseUrl: string = "http://localhost:3000";

    // vercel.svg is used in /login
    const unprotectedPaths: string[] = [
        `${baseUrl}/login`,
        `${baseUrl}/favicon.ico`,
        `${baseUrl}/vercel.svg`,
        `${baseUrl}/_next/webpack-hmr`,
        `${baseUrl}/attachables/campus-images/image1.jpg`,
        `${baseUrl}/attachables/mnhs-images/logos/login_logo.png`,
        `${baseUrl}/attachables/mnhs-images/logos/mnhs_favicon_og.ico`,
    ]
    
    
    if (unprotectedPaths.includes(url)) {
        return void 0;
    } else if (!refreshToken && url === "http://localhost:3000/api/login") {   
        return NextResponse.next();
    } else if (!refreshToken) {
        return NextResponse.redirect(`${baseUrl}/login`);
    } else { 
        return NextResponse.next();
    }
}

救星 2025-02-03 22:20:38

项目中的每条路线将调用中间件。以下是执行订单:

  1. Next.config.js的标题
  2. 从Next.config.js
  3. Middleware(重写,重定向等)
  4. 从Next.config.config.js
  5. filesystem routes(public/_next/_next/static/static/static/static/static/static/ ,
  6. 页面

Custom matcher config
Conditional statements

​a href =“ https://nextjs.org/docs/advanced-features/middleware” rel =“ nofollow noreferrer”>以获取更多信息

Middleware will be invoked for every route in your project. The following is the execution order:

  1. headers from next.config.js
  2. redirects from next.config.js
  3. Middleware (rewrites, redirects, etc.)
  4. beforeFiles (rewrites) from next.config.js
  5. Filesystem routes (public/, _next/static/, Pages, etc.)
  6. afterFiles (rewrites) from next.config.js
  7. Dynamic Routes (/blog/[slug])
  8. fallback (rewrites) from next.config.js

There are two ways to define which paths Middleware will run on:

Custom matcher config
Conditional statements

for more informations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文