Giter VIP home page Giter VIP logo

auth-node-express-server's Introduction

Auth Server API - Node.js Express App

DEMO


Run DB: mongodb://localhost/auth || http://127.0.0.1:27017/

mongod

Run App: http://localhost:3090/

npm run start
npm run dev

open T3 Studio => new connection => create connection

open Postman => create new user account by sending POST request:

// raw tab, POST http://localhost:3090/signup

{
"email": "[email protected]",
"password": "password"
} 


// response with res.json(user)

{
    "_id": "5ee0a54cbe66392266bc20af",
    "email": "[email protected]",
    "password": "password",
    "__v": 0
}

// response with  res.json({token: tokenForUser(user)})

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1ZWUwYjk2YjY1MDIxYTNlMzU5MDQxYzIiLCJpYXQiOjE1OTE3ODU4MzU0ODJ9._VUDO9vFjEcHasKGkHi7EPMWzejdTEJLrMQgdgT0a7Q"
}

T3 Studio: localhost:27017 / auth / Collections / users /

5ee0a54cbe66392266bc20af
[email protected]
password

Tech Stack

  • HTTP module (handle HTTP requests)

  • BodyParser (help parse incoming HTTP requests)

  • Morgan (login)

  • Express (parse response & routing)

  • MongoDB (storing data)

  • Mongoose (working with mongoDB)

  • Bcrypt-Node.js (storing a users passwords safely)

  • Passport-JWT (authenticating users with a JWT)

  • Passport-Local (authenticating users with a username/password)

  • Passport JS (authenticating users)

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

sudo apt-get update

sudo apt-get install -y mongodb-org

sudo systemctl start mongod
sudo systemctl daemon-reload
sudo systemctl status mongod
sudo systemctl stop mongod
sudo systemctl restart mongod

  • MongoDB launches with bindIp set to 127.0.0.1 means that the mongod can only accept connections from clients that are running on the same machine.
  • data directory: /var/lib/mongodb
  • the log directory: /var/log/mongodb
  • MongoDB package includes a configuration file: /etc/mongod.conf
  • errors or important messages by watching the output in the /var/log/mongodb/mongod.log file
  • MongoDB runs using the mongodb user account
  • If you change the user that runs the MongoDB process, you must also modify the permission to the data and log directories to give this user access to these directories:
cd / && ls
sudo mkdir -p /data/db
sudo chown -R $USER /data/db
mongod 
  • Start The mongo Shell on the same host machine as the mongod. You can run the mongo shell without any command-line options to connect to a mongod that is running on your localhost with default port 27017:
mongo
 --bind_ip <hostnames|ipaddresses|Unix domain socket paths> 

Uninstall MongoDB Community Edition

sudo service mongod stop
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

netstat -an | grep 27017
sudo rm /tmp/mongodb-27017.sock

MongoDB GUI power & Robo 3T - MongoDB management tool:

sudo apt install unzip
sudo unzip -l studio-3t-robo-3t-linux-double-pack.zip
sudo sh studio-3t-linux-x64.sh

If you’ve reached the end of your 30-day trial, please contact [email protected] to request an extension.

Atlas Cluster / Connect to Cluster0 / I have the mongo shell installed

mongo "mongodb+srv://cluster0-ful45.gcp.mongodb.net/test" --username tom
TestAtlas3000
MongoDB server version: 4.2.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
MongoDB Enterprise Cluster0-shard-0:PRIMARY> 

Testing API with curl

curl -v -H "Content-Type: application/json" -X POST \
     -d '{"email":"[email protected]","password":"111-222"}' http://localhost:3090/signup

curl -X POST http://localhost:3090/signup/ -d 'username=yourusername&password=yourpassword'

curl -I http://localhost:3090/test
curl --request GET http://localhost:3090/test
curl -X GET http://localhost:3090/test

SECRETS

// config.js

module.exports = {
    secret: 'laiejrfia34ta34995235235'
}

AUTH PROCESS

  • Test request

curl -X GET http://localhost:3090/test
  • Sign up

curl -X POST -d '{"email":"[email protected]","password":"111-222"}' http://localhost:3090/signup

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1ZWUxNDMwNDc2MTIwZjI0YWY2ZTExYWMiLCJpYXQiOjE1OTE4MjEwNjExNDB9.YM7ulDHZ-1a-I5vfhoJ27T93BsqNfhqbWPm6lzAeLAw"}

curl -X POST -d '{"email":"[email protected]","password":"111-222"}' http://localhost:3090/signup

{"error":"Email is in use!"}

curl -X POST  http://localhost:3090/signup

{"error":"You must provide email & password"}

  • Sign in

curl --location --request POST 'http://localhost:3090/signin' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "[email protected]",
    "password": "111-222"
}'

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1ZWUxNDMwNDc2MTIwZjI0YWY2ZTExYWMiLCJpYXQiOjE1OTE4Mjg5Mjc4MzV9.vmQl8tP6iguCaazSZcBsRCtDddG9gD5hrbjbf-lXK94"}


curl --location --request POST 'http://localhost:3090/signin' \
--header 'Content-Type: application/json' \
--data-raw '{
    "email": "[email protected]",
    "password": "bad password"
}'

Unauthorized
  • User/Password Access

curl -X GET  http://localhost:3090/

Unauthorized

curl --location --request GET 'http://localhost:3090/' \
--header 'Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1ZWUwZDk5ODY3N2RkZTFmZjAyNTU5MTMiLCJpYXQiOjE1OTE3OTQwNzMzNDh9.xxR3yqsecxgRh9VyqQd1dUm8-sHUhPy2-kBpz1A2ML8' \
--header 'Content-Type: text/plain' \
--data-raw '{"email":"[email protected]","password":"111-222"}'

{"hi":"there!"}

curl --location --request GET 'http://localhost:3090/' \
--header 'Content-Type: text/plain' \
--data-raw '{"email":"[email protected]","password":"111-222"}'

Unauthorized
  • Token Access

curl -X GET  http://localhost:3090/

Unauthorized

curl -H 'Accept: application/json' -H "Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1ZWUxNDMwNDc2MTIwZjI0YWY2ZTExYWMiLCJpYXQiOjE1OTE4MjEwNjExNDB9.YM7ulDHZ-1a-I5vfhoJ27T93BsqNfhqbWPm6lzAeLAw" http://localhost:3090/

{"hi":"there!"}

auth-node-express-server's People

Contributors

tom2kota 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.