Lee starts with a minimal Next.js example that reads the session in an async server component and uses server actions for login and logout. The login function reads form data, looks up user information, creates a session, and stores it in an HTTP-only cookie.
Next.js App Router Authentication
Lee Roberson, a developer at Vercel, explains Next.js App Router authentication and compares a basic cookie session with NextAuth.js.
The example signs a JWT with the Jose library, using a secret key that should be kept in an environment variable. Middleware checks for the session cookie, verifies it, and refreshes its expiration. The demo sets the cookie to expire after 10 seconds to show how expiration and renewal work.
The `getSession` function reads the session cookie and decrypts it to return session information. Logging out deletes the cookie. Lee describes this as a minimal starting point that developers can extend.
Lee then shows a NextAuth.js setup using the GitHub provider, catch-all route handlers, and the `auth`, `signIn`, and `signOut` functions. The demo creates a session token, callback URL, and CSRF token, while middleware refreshes the session and uses a route matcher to skip static assets.
Lee points to Next.js documentation covering authorization and protecting server actions and route handlers. He also mentions database sessions and examples using Clerk, Lucia, Auth0, and Supabase.
Takeaways
- Keep signing secrets in environment variables and mark session cookies HTTP-only.
- Verify and refresh sessions in middleware when using rolling expiration.
- Read session data on the server, and clear the cookie to log out.
- Consider an auth library such as NextAuth.js when you need provider integrations and built-in security features.
- Plan for authorization as well as authentication, especially when protecting server actions and route handlers.
Mentioned in this talk
Search inside this talk
What was claimed 7
- uses
“basic session authentication in Next.js without any additional libraries”
YouTube Unverified1 source· Talk
- featurecookie-based session authentication: Stores the session JWT in an HTTP-only cookie.
“say this is an HTTP-only cookie, so you can only read it on the server”
YouTube Unverified1 source· Talk
- uses
“call this function from the Jose library”
YouTube Unverified1 source· Talk
- uses
“the payload for our JWT”
YouTube Unverified1 source· Talk
- featurecookie-based session authentication: Middleware refreshes the session so it does not expire.
“otherwise, refresh the session so it doesn't expire”
YouTube Unverified1 source· Talk
- featureNextAuth.js: Provides sign-in, sign-out, and authentication methods with route handlers.
“set up some sign-in, sign-out, and auth methods, as well as some route handlers”
YouTube Unverified1 source· Talk
- usesNextAuth.js uses
GitHub
“we're using the GitHub provider for auth”
YouTube Unverified1 source· Talk
Related talks
23:33
17:11
50:44
14:16
51:55
12:33


