MYK12397 / authentication-system

A basic authentication system build in Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Authentication-System-Api

A Basic Authentication system built in Node.js

Installation & Setup

  1. Install Node.js and MongoDB.
  2. Clone this repository and install its dependencies
$ git clone https://github.com/tariqiqbal27/authentication-system
$ cd authentication-system
$ npm install
  1. Edit the config.js file and enter database name
module.exports = {
        MongoURL: 'mongodb://127.0.0.1:27017/your-database-name
}
  1. From within the authentication-system directory start the server
$ npm run dev
  1. Access the Server Api from localhost:3000

Usage

  • For CREATING AN ACCOUNT

Request

POST /register

Body - JSON

{
	"email":"auth@example.com",
	"password":"anythingthatyoulike"
}

Response - JSON

{
    "code": "ACCOUNT_CREATED",
    "msg": "User Created"
}
  • For LOGIN

Request

POST /login

Body - JSON

{
	"email":"auth@example.com",
	"password":"anythingthatyoulike"
}

Response - JSON

{
    "id": "5d9908678d1d0e1144c87b2c"
}
  • For GETTING AUTHENTICATED USER DATA

Request

GET /user

Response - JSON

{
    "_id": "5d9908678d1d0e1144c54c2c",
    "email": "auth@example.com",
    "createdAt": "2019-10-05T21:17:27.379Z",
    "updatedAt": "2019-10-05T21:17:27.379Z"
}
  • For GETTING USER ID

Request

GET /user/userid

Response - String

5d9908678d1d0e1144c54c2c
  • For LOGOUT

Request

POST /user/logout

Response - JSON

{
    "code": "AUTH_LOGOUT_SUCCESS",
    "msg": "Logout Successfully"
}
  • For CHANGING USER PASSWORD

Request

PATCH /user/change_password

Body - JSON

{
	"old_password":"password1",
	"new_password":"password2"
}

Response - JSON

{
    "code": "AUTH_PASSWORD_CHANGE",
    "msg": "Password changes Successfully"
}
  • For DELETING USER

Request

DELETE /user/delete

Response

{
    "code": "DELETED_OK",
    "msg": "Account Deleted"
}

Exception Handling

Session Exception

  • If User is not Authenticated

Response

{
    "code": "AUTH_NOT_SUCCESS",
    "msg": "User not Authenticated"
}
  • If User is Authenticated

Response

{
    "code": "AUTH_OK",
    "msg": "User already authenticated"
}

Register Exception

  • if Password length is less than 8

Response

{
    "code": "AUTH_MIN_PASSWORD",
    "msg": "Password length must be greater than 7"
}
  • if Email Already registered

Response

{
    "code": "EMAIL_ALREADY_EXIST",
    "msg": "Email Already Exist"
}
  • if Email & Password Field are blank

Response

{
    "code": "AUTH_BLANK_FIELD",
    "msg": "Enter Email/Password"
}

Login Exception

  • If Email Or Password are wrong

Response

{
    "code": "LOGIN_INVALID",
    "msg": "Email & password wrong"
}

Change Password

  • if Old password is wrong

Response

{
    "code": "AUTH_PASSWORD_INCORRECT",
    "msg": "Incorrect Account Password"
}
  • if New password and Old password are same

Response

{
    "code": "AUTH_PASSWORD_SAME",
    "msg": "New Password cannot be old password"
}

About

A basic authentication system build in Node.js


Languages

Language:JavaScript 100.0%