https://github.com/theoddysey/mern-backend-nodejs
A Node.js application designed to provide user authentication services 🤵
https://github.com/theoddysey/mern-backend-nodejs
authentication authentication-backend mern-stack mongodb node-js
Last synced: about 2 months ago
JSON representation
A Node.js application designed to provide user authentication services 🤵
- Host: GitHub
- URL: https://github.com/theoddysey/mern-backend-nodejs
- Owner: TheODDYSEY
- Created: 2023-11-11T13:47:23.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-23T11:24:29.000Z (over 2 years ago)
- Last Synced: 2025-03-26T07:23:20.945Z (over 1 year ago)
- Topics: authentication, authentication-backend, mern-stack, mongodb, node-js
- Language: JavaScript
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MERN Authentication Backend
This is the backend part of a MERN (MongoDB, Express, React, Node.js) stack application that provides user authentication functionality using JWT (JSON Web Tokens). It includes user registration, login, and JWT token generation.
## Prerequisites
Before you begin, ensure you have the following installed:
- Node.js and npm: [Node.js Downloads](https://nodejs.org/)
- MongoDB: [MongoDB Downloads](https://www.mongodb.com/try/download/community)
## Installation
1. **Clone the repository:**
```bash
git clone https://github.com/your-username/mern-auth-backend.git
```
2. **Install dependencies:**
```bash
cd mern-auth-backend
npm install
```
3. **Set up MongoDB:**
- Make sure MongoDB is running on your machine or update the `mongoURI` in `config/keys.js` with your MongoDB connection string.
4. **Create a `.env` file:**
- Create a `.env` file in the root directory and add the following:
```env
MONGO_DB_URL=your_mongodb_connection_string
SECRET_KEY=your_secret_key_for_jwt
```
5. **Start the server:**
```bash
npm start
```
This will run the server on `http://localhost:5000`.
## Project Structure
- **config/keys.js:** Contains MongoDB connection string and JWT secret key.
- **config/passport.js:** Configures Passport with JWT strategy for authentication.
- **models/User.js:** Defines the MongoDB user schema.
- **routes/api/users.js:** Defines API routes for user registration and login.
- **validation/login.js:** Validates login input.
- **validation/register.js:** Validates registration input.
- **server.js:** Express server setup, connects to MongoDB, initializes Passport, and defines routes.
## API Endpoints
- **POST /api/users/register:** Register a new user.
Example Request:
```json
{
"name": "John Doe",
"email": "john@example.com",
"password": "secretpassword",
"password2": "secretpassword"
}
```
- **POST /api/users/login:** Login with existing credentials.
Example Request:
```json
{
"email": "john@example.com",
"password": "secretpassword"
}
```
## Usage with Frontend
You can integrate this backend with a frontend application built with React. Update the frontend API calls to match the endpoints provided by this backend.
## Dependencies
- **bcryptjs:** For hashing passwords.
- **body-parser:** Middleware for parsing JSON and urlencoded request bodies.
- **dotenv:** Loads environment variables from a .env file.
- **express:** Web application framework for Node.js.
- **jsonwebtoken:** For generating and verifying JWTs.
- **mongoose:** MongoDB object modeling tool.
- **passport:** Authentication middleware for Node.js.
- **passport-jwt:** Passport strategy for authenticating with a JSON Web Token.
- **validator:** Library for string validation.
## License
This project is licensed under the MIT License - see the LICENSE file for details.