Code Monkey home page Code Monkey logo

merrn-boilerplate's Introduction

MERRN-Boilerplate

A boilerplate for MERRN [Mongo, Express, React, Redux, Node].

NOTE: You should be able to use the backend in this repo with any other JS Framework, the only thing you need to do is eliminte the client folder and then use the endpoints in the API detailed below to point your choosed JS Framework.

IMPORTANT:

In order to run this repo you need to create a file called default.json inside the folder config, this file must contain the MongoURI and the Secret that JWT is gonna use. Example:

{
  "mongoURI": "yourMongoURI",
  "jwtSecret": "yourSecretForJWT
}

INDEX

API Documentation

Introduction

This API allows you to have a prebuilt app that uses JWT to register and generate tokens for the login of the users. The goal is to have a simple and quick way to start a MERN project, for that reason this API also has all the configuration for Node, Express, Mongo and React.

Registering a user:

  • Route: api/users/new
  • Route type: POST
  • Description: New user creation
  • Access: Public
  • Expenting Object:
{
  "name": "Test Tester",
  "email": "[email protected]",
  "password": "12345678"
}

API answer on success:

On success you're gonna receive a token,you can use this token to authenticate the user that you just registered.

  • Return status: 200 Ok
  • JSON Response example:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNWNmNzQ4NGU5YThhZmYwYzQ1MjA2NTRiIiwicm9sZSI6IlVzZXIifSwiaWF0IjoxNTU5NzA5Nzc0LCJleHAiOjE1NjAwNjk3NzR9.bEpQsT-BkjAKFu_QJ2tMPDTBJ6bRUcEHH1c_A2xGPmo"
}

API answer on wrong inputs:

If any of the fields does not comply with the validation checks defined in the route you can receive one or more of the following errors. The goal in sending this object as response from the API in case of errors in the inputs is that you can provide feedback to your users when something is wrong. You can customize this validations and messages in the file: /routes/api/users.js

  • Return status: 400 Bad Request
  • JSON Response example:
{
  "errors": [
    {
      "location": "body",
      "param": "name",
      "value": "",
      "msg": "Name is required"
    },
    {
      "location": "body",
      "param": "email",
      "value": "",
      "msg": "Please enter a valid email"
    },
    {
      "location": "body",
      "param": "password",
      "value": "",
      "msg": "Password must contain at least 8 letters/symbols/numbers"
    }
  ]
}

API answer on duplicated user:

The API check if an email is already registered, if that's positive you will receive an object error. This is the same goal as the past answer, be able to provide feedback messages to the user.

  • Return status: 400 Bad Request
  • JSON Response example:
{
  "errors": [
    {
      "msg": "User already exists"
    }
  ]
}

API answer on system errors:

  • Return status: 500 Internal Server Error
  • JSON Response example:
Server Error

User authentication

  • Route: api/auth
  • Route type: POST
  • Description: Authenticate users and provides token.
  • Access: Public
  • Expecting Object:
{
  "email": "[email protected]",
  "password": "12345678"
}

API authentication success:

If the user and password are successfully validated you should receive an object containing the token.

  • Return status: 200 Ok
  • JSON Response example:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiNWNmNzQ4NGU5YThhZmYwYzQ1MjA2NTRiIiwicm9sZSI6IlVzZXIifSwiaWF0IjoxNTU5NzA5Nzc0LCJleHAiOjE1NjAwNjk3NzR9.bEpQsT-BkjAKFu_QJ2tMPDTBJ6bRUcEHH1c_A2xGPmo"
}

API authentication failed:

If the user or password provided don't match you will receive the following error:

  • Return status: 400 Bad Request
  • JSON Response example:
{
  "errors": [
    {
      "msg": "Invalid credentials"
    }
  ]
}

API answer on system errors:

  • Return status: 500 Internal Server Error
  • JSON Response example:
Server Error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.