This repository showcases a full-stack authentication solution, combining:
- Next.js for frontend development, styled with the aesthetics of TailwindCSS. π¨
- Express.js for backend API routes and logic. π₯οΈ
- MongoDB as the go-to database. ποΈ
-
Frontend: Combining Next.js, TailwindCSS, and the
next-auth
package, we offer a seamless user experience and session management. -
Backend: Express.js provides our API routes handling user registration, login, JWT creation, and validation. π©
-
Database: MongoDB ensures secure storage of user details. Passwords are stored securely, hashed with bcrypt. π
-
User Registration:
- Input email and password. π§
- Passwords are hashed using bcrypt before storage.
- Successful registration redirects to the login page. π£
-
User Login:
- Provide your email and password. π
- Express.js verifies credentials against MongoDB.
- On successful authentication, receive a JWT as an HttpOnly cookie. πͺ
-
Authenticated Requests:
- For authenticated requests, the browser sends the JWT cookie automatically. π
- Express verifies the JWT on each request.
-
Session Management on Frontend:
next-auth
manages user sessions on the frontend. π- Use utilities like
useSession
to render content based on authentication state. π
-
Logout:
- Logout to end the session. π
- The JWT cookie is cleared. π
-
Clone the Repo:
git clone https://github.com/adherb/next-auth-express-mongo.git
-
Install Dependencies:
yarn install
-
Environment Variables:
- Rename
.env.example
to.env
. π§ - Update with required environment variables.
- Rename
-
Run the Development Server:
yarn dev
-
Visit
http://localhost:3000
in your browser. π