GithubHelp home page GithubHelp logo

semklim / expressjs_mongo_api_example Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 130 KB

Website example of Authentication and Registration API

Home Page: https://webapi-example.vercel.app

License: MIT License

JavaScript 71.01% CSS 17.35% HTML 11.64%
api css express expressjs html-css-javascript html5 javascript jwt-authentication mongodb web-authentication email-sender nodejs registration web-register-form cors

expressjs_mongo_api_example's Introduction

Website example of Authentication and Registration API using CORS, JSON Web Tokens, nodemailer, Express.js and MongoDB

This README provides a comprehensive guide on how to set up and use the Authentication and Registration API using Express.js and MongoDB. This API allows you to handle user registration, activation, authentication, token refresh, retrieve users' information from the database, and manage email-based activation, as well as handle user logout.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  • Node.js and npm installed
  • Basic understanding of Express.js and MongoDB

Getting Started

  1. Install dependencies:
npm i
  1. Create a .env file in the root directory. Look inside the file .env.example for details.

  2. Check how to use google smtp server for sending email. Otherwise you will get an error.

https://support.google.com/a/answer/176600?hl=en https://support.google.com/a/answer/2956491?sjid=4893799287504753245-EU#sendinglimitsforrelay

Usage

User Registration

Endpoint: POST /api/register

To register a new user, send a POST request to the /api/register endpoint with the following JSON payload:

{
  "userName": "semklim",
  "password": "12312",
  "email": "[email protected]"
}

User Activation

Endpoint: GET /api/activate/:link

When a user clicks the activation link received via email, they will be activate and redirected to the main page of your app.

User Authentication

Endpoint: POST /api/login

To authenticate a user, send a POST request to the /api/login endpoint with the following JSON payload:

{
  "userEmail": "[email protected]",
  "password": "200996Ro"
}

Token Refresh

Endpoint: POST /api/refresh

To refresh the access token, send a POST request to the /api/refresh endpoint. Make sure to include the refreshToken in the cookies of the request.

User Logout

Endpoint: POST /api/logout

To log out a user, send a POST request to the /api/logout endpoint. This should delete the refreshToken cookie on the client side.

Protected Route - Retrieve Users

Endpoint: GET /api/users

To retrieve a list of all users from the database, send a GET request to the /api/users endpoint. Ensure that you include the Authorization header in your request with the format: Bearer <token>, where <token> is the valid access token obtained upon successful login.

Error Handling

  • If registration, activation, authentication, token refresh, or logout fails due to invalid input or other reasons, you will receive an appropriate error response.
  • Ensure to handle errors gracefully on the client-side by checking the response status and the content of the response body.

Running the Server

Start the Express server by running:

npm start

The server will run on the port 5000.

Conclusion

This API provides a secure and feature-rich way to handle user registration, activation, authentication, token refresh, user information retrieval, email-based activation, and user logout using Express.js and MongoDB. Feel free to extend and customize the API to suit your application's specific requirements.

If you have any questions or need further assistance, please don't hesitate to reach out.

Happy coding!

expressjs_mongo_api_example's People

Contributors

semklim avatar

Watchers

 avatar

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.