Payload CMS Fullstack To Do App + Next.js: Authentication & Route Protection Tutorial

preview_player
Показать описание


We'll cover:

* Login with Payload's REST API: Creating a login form and authenticating users (ensuring proper cookie handling!).
* Implementing Logout: Building a logout button and clearing user sessions.
* Two Powerful Route Protection Methods:
* Direct User Checks: Fetching user data and redirecting unauthenticated users directly within your page components.
* Handling API Routes: Configuring middleware to properly handle API requests and delegate security to Payload.
* Debugging & Best Practices: We'll troubleshoot common issues and show you how to avoid performance problems.
* Environment Variables: Understand how to set and use env variables, like `NEXT_PUBLIC_PAYLOAD_URL` and `NEXT_PUBLIC_SERVER_URL`

⏱️ Chapters
=================
0:00:00 Introduction & Overview
0:01:28 Project Setup & Organization
0:03:20 Creating the Login Page
0:05:20 Implementing Login Functionality (REST API)
0:10:12 Implementing Logout Functionality (REST API, and Client Component LogoutButton)
0:13:33 Route Protection: Direct User Check (getUser)
0:15:21 Creating the getUser Utility Function
0:16:50 Applying getUser to Protect Pages
0:20:33 Route Protection: Middleware
0:22:02 Middleware Logic: Redirects & Session Check
0:24:10 Middleware Configuration (matcher) and Handling API Routes in Middleware
0:26:04 Debugging App After Changes & Bug Fixes (Media Upload & Env Variables)
0:31:00 Wrap Up and summary of features

🔥 Recent Payload Content
————————————————
* Need Real-Time Data (websockets) in Payload CMS? Watch This NOW!

🔗 Helpful Links:
=================
* Source Code: [Link to GitHub Repository]

#nextjs #payloadcms #authentication #routeprotection #middleware #webdev #javascript #tutorial #coding #security #nextjs14 #todoapp #restapi #cookies
Рекомендации по теме
Комментарии
Автор

Nice content! Please make a video on how to make an Ecommerce website with Next.js 15 and Payload CMD to sell it to client. Would really be great.

brancode
Автор

Inside the matcher you can also ignore routs by adding for example: "/((?!_next/static|_next/image|favicon.ico|api|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)", by doing this the middleware won't run on the api calls or any other routs you set here. This is a negative lookahead regex that excludes paths

RobinGiel
Автор

Why did you hardcode the login page, it could have been added in the pages collections from admin?

mominanadeem
Автор

great - i am following everyone talking about payload in here. Keep it up! btw whats your discord ?

samoltman
Автор

may i ask why dont just use local api to do login?

chillmind
Автор

Can you clone a figma to a Landing page with your own custom blocks. and make a tutorial on how we can have our own custom blocks and use payload for backend for those blocks

And please use payload to manage the pages instad of making it in the code directly

mrfabulous
join shbcf.ru