Тёмный

Protect Your API - Next Auth Middleware 

Full Stack Niraj
Подписаться 12 тыс.
Просмотров 34 тыс.
50% 1

Pro Next JS: bit.ly/3MXZMGG
In this video, we will look at how we can protect our API made with next js using middleware or to be specific next auth middleware.
Previous Video:
Next Auth Sign in With Credentials:
• Next Auth Sign in With...
Source code: github.com/ndp...
Other Next JS Related Videos
• Next JS
Social Media Links
Instagram - / fsniraj
Twitter - / fsniraj

Наука

Опубликовано:

 

7 авг 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 54   
@fsniraj
@fsniraj Год назад
Some of you have this concern, when you use ['/admin'] for matcher config. only the /admin is going to be private not all admin routes like /admin/posts. You can use ["/admin/:path*"] to restrict all of the admin path.
@richardkirigaya8254
@richardkirigaya8254 Год назад
what is :path* ? lets say i want to restrict a "create" route in the admin page, so it becomes ["/admin/:create*] ? And if i have 5 pages in the admin page i have to repeat this 5 more times ??
@rogerhoffmann
@rogerhoffmann Год назад
@@richardkirigaya8254 path is anything after admin, you don't need to replicate for each route
@markkkkk07
@markkkkk07 Год назад
AMAZING!! Simple, fast and understandable explanation
@ingolesubodh
@ingolesubodh Год назад
goddamn bro you are awesome !! you will go big soon
@faruksardar8829
@faruksardar8829 Год назад
Simple, Easy to Understand, Thank for so much Niraj for this video
@wazzadev7209
@wazzadev7209 Год назад
Your next auth tutorials have been helpful and very straight forward, thanks.
@Iacapuca
@Iacapuca Год назад
This approach only works for JWT tokens at the moment, next-auth middleware dos not support session based auth.
@mertdr
@mertdr 2 года назад
Thanks for the video. One question though, how could you use withAuth for multiple cases on middleware routes? After NextJS removed support for multiple middleware files, withAuth looks like pretty useless. Creating a standard NextJS middleware function and catching session and then applying rules is a better idea, am I right?
@antoniuneacsu6811
@antoniuneacsu6811 Год назад
Can you declare matchers so that it matches all routes except /login?
@himanshu3984
@himanshu3984 Месяц назад
when deployed on vercel middleware is broken it just redirect to signin even i try login and successfully loggedin it do not consider protected routes of nextauth and always redirect to signin when i access them
@vladsvyrydonov1793
@vladsvyrydonov1793 Год назад
Very nice, just everything I was looking for.
@BuntyWP
@BuntyWP Год назад
Very clean and simple explanation. Thank You
@vatsalcodes
@vatsalcodes 10 месяцев назад
how to change the redirect url when we try to access secured page without being authenticated
@user-cz7sb2iz4b
@user-cz7sb2iz4b 8 месяцев назад
2 days looking for solution thank you
@evgeniy3370
@evgeniy3370 Год назад
Hi. if the "pages" folder is nested in the "src" folder, which is located along with the middleware file in the root of the project, will the middleware work?
@tomyyoung2624
@tomyyoung2624 2 месяца назад
Yes a fix, Especially for a client website
@beverlyHillsAgent
@beverlyHillsAgent Год назад
in this example, no matter which page you go to, if you are an admin, the url will be rewritten to admin page. In other words, no matter which page you go to, it will always show the admin page?
@isaacpriestley
@isaacpriestley Год назад
I'm using Rails devise-jwt as my backend, and I'm trying to figure out how to take the JWT created by my backend and use it in my Next client...
@deathdefier45
@deathdefier45 11 месяцев назад
Hey Niraj, is it possible to user a matcher that works on dynamic pages that use a slug? Please help me with a solution if you know how to, Thanks!
@blu5037
@blu5037 Год назад
Awesome video, man! many thanks :)
@giorgosterzis3331
@giorgosterzis3331 Год назад
I have added this to export const config = { matcher: ["/shop/portal/:path*"] }. But, when I try to access/shop/portal I get "TypeError: Class extends value undefined is not a constructor or null". Can anybody help?
@user-xu1xb5iz5w
@user-xu1xb5iz5w 10 месяцев назад
do one video of how to connect with an api and fetch users in login
@ivanmakevic8258
@ivanmakevic8258 Год назад
how do you authenticate users if they want to login with gmail or github for example?
@rh9037
@rh9037 Год назад
I'm running into the issues where role doesn't seem to exist in the (params.user?.role) callback function of [...nextauth].ts. I added it in the list of returns for a user but it still doesn't seem to exist. Has anyone else run into this issue?
@mpowereer6992
@mpowereer6992 Год назад
But i can still go to /signup page or /signin page, how to prevent from visiting this routes when being authenticated?
@cdromance2705
@cdromance2705 Год назад
How do you get your VSCode to show the param definitions on hover for the jwt callback at 6:22? Having something like this in my ide would make my life much easier.
@k2nnethtan949
@k2nnethtan949 Год назад
Possible to further the next episode with Prisma mongo?
@SSango-hk9sm
@SSango-hk9sm Год назад
Hie Niraj great tut once again. I have a question. I have two tables a users and workspaces table linked by a one to many relationship. I would like to implement an onboarding experience whereby after the user is signed in I would like to check if that user has a workspace linked to his/her user account if not then redirect to the page where you can create the workspace. I'm failing to figure out how I can implement that in the middleware. I'm using postgre and prisma adapter by the way.
@amadacruse4193
@amadacruse4193 Год назад
Thx for awesome tutorial sir. My problem is that i added exactly these two lines on my middleware.js on root folder export { default } from "next-auth/middleware" export const config = { matcher: ["/", "/admin/privilages/dashboard"] } and i can still visit these pages..... its like middleware doesnt woek at all. any idea why. ty in advance!
@Peter-yd2ok
@Peter-yd2ok Год назад
did you solve this?
@madhawapriyashantha3722
@madhawapriyashantha3722 Год назад
i had the same issue .i fixed it by renaming middleware.ts to _middleware.ts
@Peter-yd2ok
@Peter-yd2ok Год назад
@@madhawapriyashantha3722 but isn't that old way of creating a middleware?
@madhawapriyashantha3722
@madhawapriyashantha3722 Год назад
@@Peter-yd2ok yep but i had installed next 12.0
@Peter-yd2ok
@Peter-yd2ok Год назад
@@madhawapriyashantha3722 ohh. I had tried this old middleware syntax but then i got errors while deploying.
@shahriardhruvo6281
@shahriardhruvo6281 Год назад
Wow, great video
@iqbalherlambang5865
@iqbalherlambang5865 2 месяца назад
thanks
@prasadbeligala
@prasadbeligala Год назад
Thanks for the simplified explanation. At 6:46 can we add more user roles and redirect to different routes?
@fsniraj
@fsniraj Год назад
I think so the only way to find out is just try.
@prasadbeligala
@prasadbeligala Год назад
@@fsniraj At the end found a way to do it.. :) Looks like middleware is the best way to restrict access on the server. Thanks again for this valuable video Niraj
@shfsakib339
@shfsakib339 Год назад
Hi. Is there any way to prevent user not to go admin pages and also prevent admin for go to user pages. I don't wanna make folder for each user and check startwith [ if(url.startwith('/admin')) ] the example given in google. Please help if you can. I'm new to nextjs.
@masonpool5257
@masonpool5257 Год назад
He explains it in the video, if you set up the 'export const config = { matcher: []}', you can specify which routes require an auth token. example: export const config = { matcher: ["/admin", "/dashboard/:page*"](for all pages along the dashboard route)}
@dawid_dahl
@dawid_dahl 11 месяцев назад
"Go to hell" 😂
@fsniraj
@fsniraj 11 месяцев назад
I will definitely book a seat for you as well.😆
@kakun7238
@kakun7238 Год назад
the jwt callback is not working for me the roles is there when i console log the params object but not in the response after signing in
@fsniraj
@fsniraj Год назад
Session and the jwt callback only works on the first login so make sure you removed the cookie from application tab and then try.
@kakun7238
@kakun7238 Год назад
@@fsniraj I did that too but if I bind it to email or any other pre fixed token attribute I get the response through jwt callback Sorry to bother you but another question too can't we redirect the user after login when using a custom login?
@fsniraj
@fsniraj Год назад
If you need any custom behavior when you have to do some experiments with your code. And to redirect after login you can use useSession hook and the next/router.
@wazzadev7209
@wazzadev7209 Год назад
I have a question, what if you want to make some routes admin protected and others basic user protected routes?
@fsniraj
@fsniraj Год назад
You can check the incoming request url and try to log the req object. And according to the request you can change rewrite or redirect response.
@wazzadev7209
@wazzadev7209 Год назад
@@fsniraj thanks again
@haian_
@haian_ Год назад
Hi, please implement with next js 13
@ashimov1970
@ashimov1970 11 месяцев назад
why among indians pronouncing "sh" correctly is a huge issue?
@glibaudio
@glibaudio Год назад
Далее
How to Load Draft JS inside Next JS
3:20
Просмотров 7 тыс.
Classic Italian Pasta Dog
00:20
Просмотров 1,1 млн
Next Auth Sign in With Credentials
14:51
Просмотров 135 тыс.
How to ACTUALLY Secure Your API (5 Steps)
7:42
Просмотров 59 тыс.
WTF Do These Even Mean
13:44
Просмотров 82 тыс.
Next-Auth on App Router - Solid Auth, Super Fast
17:20
Просмотров 110 тыс.
Role-based Authentication in NextJs 13 using NextAuth
13:29
NextJs Middleware | How it Works & Real Use Cases
17:26
The Story of Next.js
12:13
Просмотров 563 тыс.
БОЛЬШЕ ВИДЕОКАРТ - БОЛЬШЕ FPS??
1:00
Почти ИМПОРТОЗАМЕЩЕНИЕ.
10:09
Просмотров 41 тыс.
#samsung #retrophone #nostalgia #x100
0:14
Просмотров 15 млн