Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Authentication in Node.js using Passport.js

A learning project demonstrating session-based authentication in Express using Passport.js with the local strategy, plus authenticated file/photo uploads.

Features

  • 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

Tech Stack

  • Node.js, Express
  • Passport.js (passport, passport-local)
  • express-session for session management
  • MongoDB via Mongoose
  • EJS for templating
  • Multer for file uploads

Project Structure

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

Getting Started

Prerequisites

  • Node.js 18+
  • A running MongoDB instance

Setup

git clone https://github.com/vsmm-world/Authentication-in-Node-using-Passportjs.git
cd Authentication-in-Node-using-Passportjs
npm install

Update the MongoDB connection string in db/connection.js to point at your local/remote database, then start the server:

npm start

The app will be available at http://localhost:3000 (see app.js for the exact port).

What This Demonstrates

  • Configuring Passport's local strategy end-to-end (register → hash password → login → serialize/deserialize user in session)
  • Guarding routes with an isAuthenticated style middleware
  • Handling multipart form uploads for logged-in users only

Author

Ravindra Valand

License

Personal learning project, not intended for production use.

About

Session-based authentication in Express using Passport.js (local strategy) with authenticated file uploads

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages