NextJs Middleware | How it Works & Real Use Cases

  Рет қаралды 26,032

Hamed Bahram

Hamed Bahram

Күн бұрын

Пікірлер: 112
@hakankaraahmetoglu490
@hakankaraahmetoglu490 Жыл бұрын
I love the concept. First explain the documentation and then make an example. Perfect for people who get bored easily while reading documents.
@hamedbahram
@hamedbahram Жыл бұрын
Glad you found the video helpful. I appreciate your comment.
@gyanaranjan6526
@gyanaranjan6526 Жыл бұрын
At first my nextjs project was crashed while production but I took help from the community and added a middlewere.ts file in root of it and it's working fine in production.
@hamedbahram
@hamedbahram Жыл бұрын
Glad you were able to figure it out.
@enjoy.....1365
@enjoy.....1365 3 ай бұрын
learning with this way , makes the task more easier
@hamedbahram
@hamedbahram 3 ай бұрын
Glad to hear that!
@JennySevilla-dk2gh
@JennySevilla-dk2gh 5 ай бұрын
Thank you! This really clarified it for me. 😊 You explained it so well.
@hamedbahram
@hamedbahram 5 ай бұрын
You're welcome! Glad it was helpful.
@uixmat
@uixmat 4 ай бұрын
Just came back to this bad boy to refresh my knowledge 💪🏼
@hamedbahram
@hamedbahram 4 ай бұрын
Welcome back 🙌🏼
@intruder2.0
@intruder2.0 Жыл бұрын
Damn this is great content tbh, ive made a full stack project just by combining what i learn from your videos. Its honestly so great keep up the amazing work and im looking forward for more uploads from u 🙏🙏
@hamedbahram
@hamedbahram Жыл бұрын
I'm glad to hear you're finding the videos useful! I appreciate your comment.
@regrioz
@regrioz Жыл бұрын
Keep your nextjs content coming. Very good video.
@hamedbahram
@hamedbahram Жыл бұрын
Absolutely 💯
@michaelmannucci8585
@michaelmannucci8585 Жыл бұрын
Hey it was nice to meet you at Tailwind Connect!
@hamedbahram
@hamedbahram Жыл бұрын
Hey Michael, it was nice meeting you as well, and thanks for reaching out.
@luizinhogm
@luizinhogm Жыл бұрын
Thanks a lot, dear teacher. You're amazing! For me now it's so clear.
@hamedbahram
@hamedbahram Жыл бұрын
Happy to hear that!
@musaamuhsen
@musaamuhsen Жыл бұрын
Great vid can you please share the notion document you created? many thanks
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for you comment. I'll publish the notion page and the notes on my site next week so you can check it out.
@afshinkaramifar4429
@afshinkaramifar4429 Жыл бұрын
Great explanation 👏, keep going...
@hamedbahram
@hamedbahram Жыл бұрын
Thank you 🙂
@heysahilsingh
@heysahilsingh Жыл бұрын
Such an amazing tutorial. But I have a doubt in my mind. Can we have different middlewares for different path? Can we also have multiple middlewares for the path? I mean a middleware only for "/about" path and other one is for "/user/dashboard"
@hamedbahram
@hamedbahram Жыл бұрын
Watch this video where I explain running multiple middlewares → kzbin.info/www/bejne/mHfVY6dsbaZ0eLM
@heysahilsingh
@heysahilsingh Жыл бұрын
Thank you 😄@@hamedbahram
@mostafaentezami8951
@mostafaentezami8951 Жыл бұрын
thank you Mr Hamed Bahram. Is it possible to access the information in the cookie in the middleware to write a condition according to that information?
@hamedbahram
@hamedbahram Жыл бұрын
My pleasure! Yes you can access the cookies, here is an example => nextjs.org/docs/app/building-your-application/routing/middleware#using-cookies
@mostafaentezami8951
@mostafaentezami8951 Жыл бұрын
thanks a lot 🙏 @@hamedbahram
@hamedbahram
@hamedbahram Жыл бұрын
@@mostafaentezami8951 my pleasure.
@lexsemenenko
@lexsemenenko Жыл бұрын
Does your ecommerce project implemented with Clerk have user roles?
@hamedbahram
@hamedbahram Жыл бұрын
Nah it doesn't, but I will create a video on role-based authentication soon. This has been requested from a lot of folks.
@ajayg2752
@ajayg2752 Жыл бұрын
I can't access local storage in the middleware for authentication, any solution?
@hamedbahram
@hamedbahram Жыл бұрын
You can use cookies for auth related tokens.
@ajayg2752
@ajayg2752 Жыл бұрын
@@hamedbahram 👍
@gcortinez
@gcortinez Жыл бұрын
Thanks for the video, great content. Whats the app you use to show the content??? Thanks
@hamedbahram
@hamedbahram Жыл бұрын
Thank you! I use notion for my notes.
@gcortinez
@gcortinez Жыл бұрын
Thanks @@hamedbahram
@lexsemenenko
@lexsemenenko Жыл бұрын
Great explanation.
@hamedbahram
@hamedbahram Жыл бұрын
Glad it was helpful!
@msarlak1512
@msarlak1512 2 ай бұрын
thanks hamed! how i can access this middleware document?
@hamedbahram
@hamedbahram 2 ай бұрын
Hmm that's part of the NextJs course modules.
@OleJrgensen
@OleJrgensen Жыл бұрын
Is it possible to have multiple middleware functions ? Can you please show how to have both next-auth and next-intl configured integrated in middleware.
@hamedbahram
@hamedbahram Жыл бұрын
Yes you can chain middlewares, sure I'll keep that in mind for another video.
@CoderSineNomine
@CoderSineNomine 9 ай бұрын
thx. here is a question, what if i want to protect some but exclude certain paths from next-auth/middleware such as excluding /api/auth but protect admin. i literally tried every way including regex, i tried to redirect with nextresponse if i am in that path or request.url ext.. noting works. let me know if you have any idea! thx.
@hamedbahram
@hamedbahram 9 ай бұрын
You can either match all the routes and use if conditionals inside your middleware to control which paths to protect, or only pass the paths you want to protect to the config matcher like the `matcher: "/admin/:path*" `
@TheEngineer-ef1jq
@TheEngineer-ef1jq Жыл бұрын
I'd love to buy your course sometime nicely explained
@hamedbahram
@hamedbahram Жыл бұрын
I'd glad to have you in the course. Appreciate your comment.
@27sosite73
@27sosite73 Жыл бұрын
great channel thank you, mate
@hamedbahram
@hamedbahram Жыл бұрын
I appreciate it! Welcome to the channel.
@Septumsempra8818
@Septumsempra8818 Жыл бұрын
How do we add production-grade auth if we're using a backend API like Django or Springboot? Suppose we wanna use an API that already has auth (either JWT or session-based) and all the password reset etc features built-in. How best do we set up auth and how best do we manage server state vs client state? Any resources or advice would be thoroughly appreciated.
@hamedbahram
@hamedbahram Жыл бұрын
Good question, I guess you'd have to set and manage cookies to communicate with you Django backend, but to be hones, I'm not the very familiar with Django so I won't be the best person to help with this.
@ThanHtutZaw3
@ThanHtutZaw3 10 ай бұрын
I was protecting api with middle ware based on cookies token . But when I make call second api from service worker Notification, I don't receive cookies .
@hamedbahram
@hamedbahram 10 ай бұрын
Watch this video for auth implementation using middleware functions → kzbin.info/www/bejne/iXe0qKelZdepptU
@amirrezaranjbar2853
@amirrezaranjbar2853 Жыл бұрын
How can I add my own extra logic in the middleware function if I’m using Clerk? Because clerk forced to me to just use their AuthMiddleware
@hamedbahram
@hamedbahram Жыл бұрын
You can chain you middlewares using the `beforeAuth` callback in Clerk. You can read more about it here: clerk.com/docs/nextjs/middleware#chaining-middleware-together
@jeremymunroe
@jeremymunroe Жыл бұрын
You have done sessions before using the getSession(). Between that and using middleware for authenticated users, which would be better?
@hamedbahram
@hamedbahram Жыл бұрын
I personally like the middleware approach, since it runs before the page is rendered, and It's typically faster.
@jeremymunroe
@jeremymunroe Жыл бұрын
@@hamedbahram Aaah thanks for that insight👏🏻.
@idunexist
@idunexist Жыл бұрын
I used to work with external APIs for authentication with JWTs, specifically using refresh token to provide better user experience. Then, after learning the new "app router", I don't know how to work with refresh tokens anymore. Any suggestions? Much appreciated!
@hamedbahram
@hamedbahram Жыл бұрын
For sure, please expand on how you were doing it before, so I can suggest how to do it in the app router. On the other hand I've used NextAuth and Clerk for auth in NextJs, both of which support refresh token rotation, so you don't have to implement it from scratch.
@princeonukwili996
@princeonukwili996 Жыл бұрын
Thanks☺☺! You just gained a subscriber😁. Please, i would like to know how pass data (returned by an API request called in the middleware), into server components. Similar to the way you did it for normal API endpoints
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear that! I don't think you can pass data to server components from you middleware. You can return JSON from it or redirect to a page but not sure how to send data to that page. Why not calling the API from the server component?
@princeonukwili996
@princeonukwili996 Жыл бұрын
@@hamedbahram Thanks! I’ll do that ☺️
@nicolasrodi3138
@nicolasrodi3138 Жыл бұрын
Hey there Hamed. Superb content you' ve been putting out there! I've been trying to extract the _id from the users mongo collection in the session object so I can persist that piece of data when I insert something like a todo object in a todos collection, so I can later have like an individual set of todo' s for each individual user. I' ve found that it can be done by doing a little bit of next auth module augmentantion and switching from the jwt to the database strategy. The caveat of this is that it doesn' t allow me to protect routes as easily as the middleware approach does. So, in short, can you think of a way to protect routes in the app router using next-auth and the database strategy? Thanks in advance!
@hamedbahram
@hamedbahram Жыл бұрын
Thanks Nicolás! To add the user id to the session object, you don't have to necessarily use the `database` strategy. You can pass a database adapter so you can persist user data but still explicitly set the session strategy to `jwt`. This way you can still use the middleware for authentication.
@nicolasrodi3138
@nicolasrodi3138 Жыл бұрын
@@hamedbahram Oh I don't know why I missed that! Alright, will give it a try. thanks!
@hamedbahram
@hamedbahram Жыл бұрын
@@nicolasrodi3138 Awesome!
@aburaihan-py4vi
@aburaihan-py4vi Жыл бұрын
Excellent!!!!
@hamedbahram
@hamedbahram Жыл бұрын
Thank you!
@GiorgiGvimradze1
@GiorgiGvimradze1 Жыл бұрын
Hi, I actually tied to find, here on KZbin, a way to set cookies of a client, in any request that I make to backend. They are done manually in every single route (endpoint) in NextJS, but that should be handleable by middleware right? Request flow Postman -> Next -> Nest -> Next -> Postman. In here If I don't particularly ask Next to add "Cookie" header, it won't automatically be sent to the backend on nest. How not repeat your code by using this middleware? Doesn't seem to work so far with getting and setting the cookies.
@hamedbahram
@hamedbahram Жыл бұрын
I'm not sure if I understand the question! Can you summarize your question?
@dawid_dahl
@dawid_dahl Жыл бұрын
Thank you!
@hamedbahram
@hamedbahram Жыл бұрын
Anytime!
@agustinmolina1027
@agustinmolina1027 Жыл бұрын
thanks for your videos! saludos!
@hamedbahram
@hamedbahram Жыл бұрын
My pleasure!
@saichandrajagu6141
@saichandrajagu6141 Жыл бұрын
how about using redux storage to get the values but not cookies, can I use like this. Cause I'm storing the user details in the Redux storage, where the jwt token exists, so i wanna take the help of that local storage for the protected routes using middleware(but not cookies). Can we achive this, please make something clear for me, Please explain the process if that works( i don't wanna involve coookies for protected routes
@hamedbahram
@hamedbahram Жыл бұрын
I think http only cookies are a better way for authentication, also I haven't seen redux implemented in middleware before.
@saichandrajagu6141
@saichandrajagu6141 Жыл бұрын
@@hamedbahram how can we implement those protected routes in app router. getServerSide Props doen't work on those router while using 'use client', in that case what can be the best pratice for the protected routes
@imkir4n
@imkir4n Жыл бұрын
Really helpful
@hamedbahram
@hamedbahram Жыл бұрын
Glad to hear that
@aloneking5388
@aloneking5388 Жыл бұрын
Sir please create role based authentication in nextauth
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for the suggestions, I appreciate it. I have that in my upcoming videos.
@sebastianfast3501
@sebastianfast3501 Жыл бұрын
Thank you!🙏
@hamedbahram
@hamedbahram Жыл бұрын
Any time!
@techknowledgebase4504
@techknowledgebase4504 Жыл бұрын
How Modularise middleware? Large middleware file will produce issue
@hamedbahram
@hamedbahram Жыл бұрын
Good question! You'd have to chain middlewares together. Here is a good read: reacthustle.com/blog/how-to-chain-multiple-middleware-functions-in-nextjs
@techknowledgebase4504
@techknowledgebase4504 Жыл бұрын
@@hamedbahram Thank you! Got it.
@TheEngineer-ef1jq
@TheEngineer-ef1jq Жыл бұрын
is it true nextjs middleware only works if deployed on vercel? At least for anything that needs nodejs.
@hamedbahram
@hamedbahram Жыл бұрын
It depends on the platform you choose to host your application, on Middlewares are deployed as an edge function.
@simonhylander7489
@simonhylander7489 Жыл бұрын
how can I make database calls in middleware
@hamedbahram
@hamedbahram Жыл бұрын
Since middlewares run on the edge runtime, you can't make a traditional database connection unless you use a database that allows you to connect over HTTP.
@dulonmahadi1837
@dulonmahadi1837 Жыл бұрын
where to find this document ?
@hamedbahram
@hamedbahram Жыл бұрын
The source code and all the related links are in the video description.
@ICOReviewtoken
@ICOReviewtoken Жыл бұрын
thank bro
@hamedbahram
@hamedbahram Жыл бұрын
Anytime
@abbaskareem5281
@abbaskareem5281 Жыл бұрын
I hope deep dive websockets with next js . There is zero content on KZbin on this topic
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for the suggestion Abbas. I appreciate it.
@samislam2746
@samislam2746 10 ай бұрын
why is the middleware file in the root of the project? can't we put it inside the app directory inside the folder "about" instead of doing regular expressions in `export const config = { matcher: '/about' }`
@hamedbahram
@hamedbahram 10 ай бұрын
You can only use the file `middleware.ts` (or .js) in the root of your project to define Middleware. Read more here → nextjs.org/docs/app/building-your-application/routing/middleware
@samislam2746
@samislam2746 10 ай бұрын
@@hamedbahram how many middleware.ts files a project can have?
@אליה-ז8י
@אליה-ז8י 4 ай бұрын
is it version 5?
@hamedbahram
@hamedbahram 4 ай бұрын
V4
@mohammadaghayari3661
@mohammadaghayari3661 Жыл бұрын
But you did not talk about auth in middleware.
@hamedbahram
@hamedbahram Жыл бұрын
This video is not about authentication; It's about what middlewares are and how they work. Watch this video for auth → kzbin.info/www/bejne/iXe0qKelZdepptU And this for chaining multiple middlewares → kzbin.info/www/bejne/mHfVY6dsbaZ0eLM
@mohammadaghayari3661
@mohammadaghayari3661 Жыл бұрын
yes but I think you said we will talk about authentication @@hamedbahram
@aminsoraya
@aminsoraya Жыл бұрын
Nice🎉
@hamedbahram
@hamedbahram Жыл бұрын
Thanks
@jeremymunroe
@jeremymunroe Жыл бұрын
Typescript plase😢
@hamedbahram
@hamedbahram Жыл бұрын
Thanks for your feedback, I hear you 🙂.
@alirezaahmadi9244
@alirezaahmadi9244 8 ай бұрын
thanks bro
@hamedbahram
@hamedbahram 8 ай бұрын
My pleasure!
Chain Multiple Middleware Functions in NextJs 13
19:47
Hamed Bahram
Рет қаралды 15 М.
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 7 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Protected routes in Nextjs 15 | Protect Routes with Middleware Nextjs
27:33
JavaScript Unleashed
Рет қаралды 2,2 М.
Server vs client components in NextJs 13 - When to use which
34:07
Hamed Bahram
Рет қаралды 35 М.
Next.js App Router Authentication (Sessions, Cookies, JWTs)
11:31
Nested Layouts in NextJs 13 Using Route Groups
13:33
Hamed Bahram
Рет қаралды 13 М.
Internationalization in NextJs 13
22:27
Hamed Bahram
Рет қаралды 57 М.
Next 14 + React Query COMBO with Server Actions and RSC
9:49
developedbyed
Рет қаралды 116 М.
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 7 МЛН