Sniper-Code / AuthJs

A simple authentication project for beginners to start frontend integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AuthJs

"JS" "Express" "Node" "yarn" "SQLITE" "Markdown" "VSCode" "JSON"

Table of Contents

Introduction

This is a dummy project that helps you to start your frontend project and setup a basic authentication flow.

Requirements

  • NodeJs installed v12.x.x or higher.
  • Yarn installed -> use npm install --global yarn if you want to setup yarn

Thats it for the requirements.

Setup

Setting up project is relatively easy if you have Node.js installed in your pc.

  1. Clone the repo or download the repo from https://github.com/Sniper-Code/AuthJs
  2. Open terminal pointing to cloned directory and follow the Instructions section.

Pre-Requisites

  1. Internet connection to download required packages.
  2. Node.js version 12.x.x or higher. (v14+ recommended)
  3. VisualStudio Code (VsCode) latest version.

Assumptions

I assume that the user has installed Node.js version 12.x.x or higher. (v14+ recommended) and the user has internet connection to download required packages. The user knows how to install Node.js version 12.x.x or higher if not installed. The user also have basic knowledge of how to use command line and VsCode.

Suggestions

Please use VisualStudio Code(VsCode) to have a better experience exploring files and all the documents of the project. Open the "Readme.md" file in VsCode and use "ctrl+shift+v" command to have a better experience.

Instructions

To run the project open your command line interface (CLI) and navigate to the current directory and observe whether the node_modules folder is created. If not then run the following command:

  yarn setup

This will install all the required packages. Now simply run another command to start the project:

  yarn dev

You would see your browser opening URL http://localhost:8080/docs. If it does not check the terminal [CLI] which would show logs as shown in below.

Index: Working on Development Mode.
πŸ“‘ Server is running in Development mode
🌎 Setting up HTTP Server
πŸ“š Swagger Documentation is available at http://localhost:8080/docs/
πŸ“š Swagger Documentation is available at http://localhost:8080/docs/
πŸ›  Configuring HTTP Routes
πŸ”— Establishing API Controller [Auth] at [/api/auth/] with E:/Practice/authJs/backend/controllers/api/Auth.js
Index: Working on Development Mode.
πŸ“‘ Server is running in Development mode
Index: Working on Development Mode.
πŸ“‘ Server is running in Development mode
🌎 Setting up HTTP Server
πŸ“š Swagger Documentation is available at http://localhost:8080/docs/
πŸ“š Swagger Documentation is available at http://localhost:8080/docs/
πŸ›  Configuring HTTP Routes
πŸ”— Establishing API Controller [Auth] at [/api/auth/] with E:/Practice/authJs/backend/controllers/api/Auth.js
πŸ”— Establishing API Controller [User] at [/api/user/] with E:/Practice/authJs/backend/controllers/api/User.js
πŸ‘‚ Connection Established, Serving HTTP with URL [http://localhost:8080]

Open your browser navigate to http://localhost:8080/docs URl and you are good to go.

Directory Structure

Folder Structure

  1. backend directory -> All the web/api controllers.
  2. configuration directory -> All the Configurations.
  3. database directory -> All the Scripts and Database files.
  4. documents directory -> All necessary images and documents.
  5. library directory -> All custom written Codes
  6. node_modules -> All the packages and dependencies.

index.js file is the initial file that server requires whereas cleanup.js file is used to remove create a fresh setup of project wiping all information in database.

⚠️ Warning : Don't Run yarn clean or yarn setup unless you want a fresh setup.

What is exposed

When you navigate to browser and open http://localhost:8080/docs you will see. Swagger Implementation

Every endpoints requires a csrf token which can be obtained from http://localhost:8080/api/auth/csrf and every user routs along with login_check routes requires Authorization header which can be achieved once you hit http://localhost:8080/api/auth/login route.

Resolve PORT Conflict

When you see following message in console it means a PORT conflict has arise.

node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::8080
    at Server.setupListenHandle [as _listen2] (node:net:1319:16)
    at listenInCluster (node:net:1367:12)
    at Server.listen (node:net:1454:7)
    at Function.listen (F:\AuthJs\node_modules\express\lib\application.js:635:24)
    at Server.#listenHttp (F:\AuthJs\library\server\lib.server.express.js:418:14)
    at Server.setupHTTP (F:\AuthJs\library\server\lib.server.express.js:521:25)
    at Server.setUpServer (F:\AuthJs\library\server\lib.server.express.js:535:31)
    at Object.<anonymous> (F:\AuthJs\index.js:85:6)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1346:8)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'EADDRINUSE',
  errno: -4091,
  syscall: 'listen',
  address: '::',
  port: 8080
}

To Fix this issue we have different solution as per the operating system.

Ubuntu

Find PID using following command

lsof -i TCP:8080 | grep LISTEN

It would result in something like

node    2464 user   21u  IPv6 4392639      0t0  TCP *:http-alt

The Second colum after node is PID, kill the PID using following command

kill -9 2464

Windows

Find the PID using following command

netstat -ano | findstr :8080

WHich would return something like

 TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       24552
 TCP    [::]:8080              [::]:0                 LISTENING       24552

Kill the PID at last after LISTENING using following command

taskkill /PID 24552 /F

Support

If you have any issues please contact me at:

Email : dibeshrsubedi@gmail.com LinkedIn : https://www.linkedin.com/in/itsubedibesh

Issues

Checkout https://github.com/Sniper-Code/AuthJs/issues for any issues or to fill an issue.

About

A simple authentication project for beginners to start frontend integration

License:MIT License


Languages

Language:JavaScript 99.8%Language:Shell 0.2%