An open API service indexing awesome lists of open source software.

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 🤵

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.