Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
A Next.js authentication starter with MongoDB, bcrypt password hashing, cookies, and serverless functions.
Next.js Local Authentication with MongoDB is a serverless authentication starter built by Ty Mick that demonstrates local username/password authentication on Next.js with bcrypt password hashing and cookie-based sessions backed by MongoDB Atlas. The project is hosted as a live demo at nextjs-local-authentication.tymick.me and its source code is available on GitHub.
This demo shell consists of five pages: a homepage, a dashboard, a settings page, a register page, and a login page. The dashboard and settings pages are protected and redirect unauthenticated visitors to /login. Users can register with any username not already in the database and any password; the password is hashed with bcrypt before storage in a MongoDB Atlas database on the free plan. Authentication and session state are handled by a browser cookie named "token". If the "remember me" box is checked on login, the cookie expires in two weeks; otherwise it expires when the browser session ends. Logging out in one browser tab automatically logs out every other open tab.
ty, password password) for trying protected pages without creating an account.ty account is restricted: the profile update and delete account APIs return 403 errors for it.Yes. You can log in with username ty and password password. This account cannot be updated or deleted; the update and delete endpoints return 403 errors for it.
If you check "remember me" on the login page, the "token" cookie expires in two weeks. If you don't, it expires when you end your browser session (quitting the browser, not just closing a tab).
You can register any username that does not already exist in the database. The system blocks duplicate usernames on registration, and you also cannot change your username to one already used by another account.
Credentials are stored in a MongoDB Atlas database on a free plan. Passwords are hashed with bcrypt before being saved.
Yes. If you have the dashboard or settings pages open in multiple tabs, logging out in any tab automatically ends the session in all other tabs.
