A learning project demonstrating session-based authentication in Express using Passport.js with the local strategy, plus authenticated file/photo uploads.
- User registration & login with
passport-local - Session-based authentication (
express-session) - Password hashing with
bcryptjs - Protected routes (
secret,photos) accessible only when logged in - Authenticated image upload handling via
multer - Server-rendered views with EJS
- Node.js, Express
- Passport.js (
passport,passport-local) express-sessionfor session management- MongoDB via Mongoose
- EJS for templating
- Multer for file uploads
app.js # Express app entry point
auth/
├── auth.js # Passport local strategy config
└── passportConf.js # Passport serialize/deserialize + session setup
db/
└── connection.js # MongoDB connection
middleware/
└── imageHandler.js # Multer upload middleware
models/
├── user.js # User schema
└── file.js # Uploaded file schema
routes/
└── route.js # Auth & photo routes
Views/
├── home.ejs
├── login.ejs
├── register.ejs
├── secret.ejs # Protected route example
├── photos.ejs # Authenticated photo upload/view
└── test.ejs
- Node.js 18+
- A running MongoDB instance
git clone https://github.com/vsmm-world/Authentication-in-Node-using-Passportjs.git
cd Authentication-in-Node-using-Passportjs
npm installUpdate the MongoDB connection string in db/connection.js to point at your local/remote database, then start the server:
npm startThe app will be available at http://localhost:3000 (see app.js for the exact port).
- Configuring Passport's local strategy end-to-end (register → hash password → login → serialize/deserialize user in session)
- Guarding routes with an
isAuthenticatedstyle middleware - Handling multipart form uploads for logged-in users only
Ravindra Valand
- GitHub: @vsmm-world
- Instagram: @ravindra_valand
Personal learning project, not intended for production use.