GithubHelp home page GithubHelp logo

api_rest_sql's Introduction

API REST PostgreSQL

API REST of user login/sign-up, logout and view and edit a list of products with with Node.js, Express and PostgreSQL using jwt(jsonwebtoken) authentication

Libraries

  • Express
  • Bcrypt
  • Dotenv
  • Jsonwebtoken
  • Mongoose
  • Pg

Installation

Install dependencies and devdependencies

npm i 

Start project in localhost:3000

npm start

SQL Queries

First of all, you need to create the tables in your PostgreSQL database

CREATE TABLE users
(id_user serial NOT NULL PRIMARY KEY,
 username VARCHAR(50) NOT NULL,
 email VARCHAR(255) UNIQUE NOT NULL,
 password VARCHAR(200) NOT NULL,
 logged BOOLEAN NOT NULL
);
CREATE TABLE posts
(
id_post serial NOT NULL PRIMARY KEY,
title VARCHAR(50) NOT NULL,
content VARCHAR(140) NOT NULL,
date DATE default current_date,
id_user INT NOT NULL,
FOREIGN KEY (id_user) REFERENCES users (id_user)
)

Dotenv variables

DB_USER
DB_PASSWORD
DB_HOST
DB_PORT
DB_DATABASE
ULTRA_SECRET_KEY

How to use it

Go to your Rest Client (I use Advanced Rest Client) to make your requests.

First of all we will get the posts that we have in our database

img

Then, if we add an email in the URL, we can select posts by authors

img

Now, we are going to create a user profile to upload posts.

img

As you can see in your database, the create profile has a column called logged with the false value by default.

img

Now, make a post request to log in with the new account

img

The logged value will change in your database

img

Paste your token in the "access_token" header, we need the logged value true and our token to send posts

img

Then wi will make a GET request to logout our user, this only will change the logged value to false, so our token isn't value anymore and the API REST will send us the message:

{ msg: 'Token deleted' }

img

If we try to send a new post, the API REST will send the message:

{ msg: 'Invalid token' }

img

http requests are available to import to postman in utils folder (API_rest_sql_postman_requests.json)

api_rest_sql's People

Contributors

alejandroereyesb avatar alvaroariasrivero avatar guille-rubio 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.