Protected Routes In Under 5 Minutes: Next.js

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024
  • Code Repo: github.com/asi...
    NextResponse documentation: nextjs.org/doc...

Комментарии • 61

  • @HeavyMachinery512
    @HeavyMachinery512 9 месяцев назад +1

    Bro, speak up!!

  • @AkbarKadyrov-w2j
    @AkbarKadyrov-w2j 9 месяцев назад

    Firstable thank you bro, but why are whispering ? More confidance an louder pls)

  • @smackalpha6386
    @smackalpha6386 19 дней назад

    cookie is shared between tabs. i want like session storage. every new tab, user need to log in. is any approach there

  • @alitonoliveira1700
    @alitonoliveira1700 Год назад +1

    Can this approach be used with firebase?

  • @hrithikrudra4292
    @hrithikrudra4292 Год назад +1

    Can we use localStorage access token here?

  • @awekeningbro1207
    @awekeningbro1207 4 месяца назад

    but you are using the old pages router, is there a way to do this in app router?

  • @tharsilaborges3792
    @tharsilaborges3792 2 года назад +3

    It's works for me. And you help me a lot. Thank you so much!

  • @shahreazneeloy2119
    @shahreazneeloy2119 Год назад +1

    Thank you so much. Great video. If I have to protect multiple routes than what should I do?

  • @kostabrillakis36
    @kostabrillakis36 Год назад +1

    how does Next.js know to run this function as a middleware? Does it know automatically because you named the file middleware? what if you wanted to add multiple middleware functions?

    • @farrukhahmad1430
      @farrukhahmad1430 Год назад

      you cannot add multiple middleware functions. `middleware` file can only have one function exported that will run on every route change unless you filter middleware function for specific routes using `matcher`.

    • @kostabrillakis36
      @kostabrillakis36 Год назад

      @@farrukhahmad1430 thanks for clarifying

    • @tunamusic2314
      @tunamusic2314 2 месяца назад

      @@farrukhahmad1430 where he has configuration to use middleware in the video, bro

  • @CodeSprint_site
    @CodeSprint_site 11 месяцев назад

    What's gonna be alternative approach if we are on next 9, I'm on next 9.3.6 and updating cause a lot of issues. What do you suggest in that case? Thanks in advance for the consideration!

  • @farrukhahmad1430
    @farrukhahmad1430 2 года назад +2

    Great video sir. Learned a-lot. You made it simple to understand. Thanks.

  • @АлександрГерасимов-с3щ

    Does relative path work for middleware? Such as "/" for home page and "/dashboard" for dashboard page

  • @Amer_Mohamed
    @Amer_Mohamed Год назад

    Thanks bro

  • @Pause4Code
    @Pause4Code 7 месяцев назад

    I am trying to implement here a middleware because the project I am working on was configured to be only on client side so every time I try to use the middleware I keep getting:
    Middleware cannot be used with "output: export".
    Do you know a workaround for that?

  • @arrpit-tiwari
    @arrpit-tiwari 6 месяцев назад

    simple and great

  • @matthewmubvaruri3248
    @matthewmubvaruri3248 Год назад +1

    Thanks 👏👏...... spent quite some time trying to find a simple tut as like this one

  • @mutantthegreat7963
    @mutantthegreat7963 Год назад

    I've been trying to make a mongoose connection in middleware, but it errors. Trying to figure out if this kind of operation may take place in middleware.js..

  • @knuseski
    @knuseski 6 месяцев назад

    How can we hide / show header based on the cookie ?

  • @thebishalniroula
    @thebishalniroula 2 года назад +3

    I have a question. What happens if a user sets cookie by himself from the browser console. Would not that let him visit protected routes without logging in?💁

    • @devar1133
      @devar1133  2 года назад +3

      Yap! The code above is very basic just to show you guys the fundamentals. In a real application, to prevent what you described, you would set your cookie to a jwt token and check to see if that jwt token is valid, then if it is valid, you let the user view the protected routes. This is one way of doing it.

    • @iruafeimi
      @iruafeimi 2 года назад

      @@devar1133 yeah.. I noticed it, you could set verify to verify secret using jwt and token fetched.. Great tutorial though it help understand the logic

    • @mithatercan
      @mithatercan 2 года назад

      You should set cookies in the backend with HttpOnly flag. So the cookies gets to be accessible only from the server. Client can't read the HttpOnly flagged cookies.

  • @m.laxminarayanreddy
    @m.laxminarayanreddy 3 месяца назад

    Thanks man

  • @rajraman987
    @rajraman987 3 месяца назад

    but if cookie is set from backent , and cookie is a jwt token, then how to check?

  • @mohamedkalid8713
    @mohamedkalid8713 Год назад

    how it will work for multiple pages in protected routes ? is any method ?

  • @gorkemyagc7330
    @gorkemyagc7330 Год назад

    Thank you so much bro you are awesome I hope you get 10000 subscribers on youtube

  • @mariodark4012
    @mariodark4012 2 года назад +1

    Hey ! Thank you for the tutorial. I actually imported NextRequest from the path you've used in your code and it didn't work. I also tried importing it fron "next/server" and that didn't help either. Anything I could be doing wrong ? I'm copying the exact same code that you're writing.

    • @devar1133
      @devar1133  2 года назад +1

      What version of Next.js are you on. You'll need version 12 or higher

    • @netTraverser
      @netTraverser 2 года назад +1

      I just found out that the new way to do this is like so
      export default function middleware(req) {
      const authenticated = req.cookies.get("authenticated");
      const { pathname, origin } = req.nextUrl;
      if (!authenticated && pathname === "/") {
      return NextResponse.redirect(`${origin}/user/login`);
      }
      if (
      (authenticated && pathname === "/user/login") ||
      pathname === "/user/signup"
      ) {
      return NextResponse.redirect(`${origin}/`);
      }
      }
      Happy to help

  • @trixpks
    @trixpks 2 года назад +1

    Thank you so much

  • @caducoder
    @caducoder Год назад

    how can i do this with JWT in localStorage?

  • @dhondup10
    @dhondup10 Год назад

    Is there another way to do this without using cookie?

  • @joyadas3003
    @joyadas3003 10 месяцев назад

    thank you very much sir.❤

  • @BuntyWP
    @BuntyWP Год назад

    Short and simple

  • @davidoginga3993
    @davidoginga3993 Год назад

    Can you use this approach with Firebase?

  • @MohamedMahmoud-pe6qm
    @MohamedMahmoud-pe6qm Год назад

    Perfection thank you sir you made my day

  • @nicholasgabrielmendezbertr378
    @nicholasgabrielmendezbertr378 11 месяцев назад

    I Love you man!

  • @GorkhaliCode
    @GorkhaliCode Год назад

    wah dude, this is amazing

  • @focusroom_
    @focusroom_ Год назад

    Excelent basic example, thanks!

  • @taratarinipatnaik2380
    @taratarinipatnaik2380 2 года назад

    please make new next.js video

  • @FaishalHanif
    @FaishalHanif Год назад

    thanks, very helpful

  • @mooder3247
    @mooder3247 2 года назад

    I used another approach but it has a similar logic so when i deployed it on vercel on hobby plan sometimes vercel serverless functions crushes do you have any idea why this happens

  • @tonyartz
    @tonyartz Год назад

    Amazing! Thank you so much, you deserve so many more subscribers! This was easy to understand and straight to point! Love it, keep it coming

  • @gabriel_lk
    @gabriel_lk Год назад

    Thank you!

  • @Th3Younesse
    @Th3Younesse Год назад

    thank you you went straight to the point

  • @sibainpathan
    @sibainpathan Год назад

    thank dear

  • @taratarinipatnaik2380
    @taratarinipatnaik2380 2 года назад

    Thank you for sharing valuable video bro

  • @keshavsaini1756
    @keshavsaini1756 Год назад

    smooth

  • @heguer87
    @heguer87 2 года назад

    Lets see how it goes with this approach

  • @rfryanfavour4369
    @rfryanfavour4369 Год назад

    Thank you for making it simple to understand and open for us to tweek its conditions 🎉❤

  • @ralphwealth163
    @ralphwealth163 Год назад

    this is the most straight forward and simple tutorial with good implementation

  • @madvier_8336
    @madvier_8336 Год назад

    clean!

  • @raunaksingh4381
    @raunaksingh4381 2 года назад

    💎 gem

  • @abdulshaikh3465
    @abdulshaikh3465 Год назад

    bravo.