/smartbrain-backend-jwt

Backend Server for smartbrain-frontend-jwt

Primary LanguageJavaScript

All SmartBrain Repos


Build Status Heroku

Updated Version of this Repo: https://github.com/rbhachu/smartbrain-master-docker

SmartBrain v2 Server


Description

Back-End Server for SmartBrain Front-End Client, running Node, Express, Redis and PostgreSQL servers.

Requires SmartBrain Front-End Client, please see repo for full details; https://github.com/rbhachu/smartbrain-frontend-jwt


Dependencies/NPM Modules

Bcrypt: v5.0.1 * - For password encryption/decrytion between PostgreSQL DB
Clarifai: v2.9.1 * - For Face Detection API
Clarifai API Key ** - Free API Key required to use Clarifai Face Detection API
Cors: v2.8.5 * - Cross-Origin Resource Sharing (server to server)
Dotenv: v10.0.0 * - For environment variables
Express: v4.17.1 * - Node.js server
JSONwebtoken: v8.5.1 * - For JSON Web Tokens
Knex: v0.95.6 * - SQL Query Builder for JavaScript
Morgan: v1.10.0 * - HTTP request logger middleware for node.js (for testing server responses)
NodeMon: v2.0.7 * - Node.js development tool
PG: v8.6.0 * - Non-blocking PostgreSQL client for Node.js
PostgreSQL Database ** - Used for User Database
Redis: v3.1.2 * - Redis client for Node.js
Redis Database ** - For JSON Web Token Management

* Installed automatically
** Requires manual installation/setup - see below


Installation Prerequisites

Clarifai API Key

You will need to obtain a free API key from Clarifai

API Key Link: https://www.clarifai.com/models/ai-face-detection

PostgreSQL Database Installation

You will need to install a local instance of PostgreSQL server to install the User and Login tables required for the site.

PostgreSQL Download Link: https://www.postgresql.org/download/
PostgreSQL Installation Guide: https://www.postgresqltutorial.com/install-postgresql/

Once you have installed PostgreSQL Database, use the following SQL statements to create the Login, Users Tables and Seed them too.

Login Table SQL

BEGIN TRANSACTION;

CREATE TABLE login (
    id serial PRIMARY KEY,
    hash varchar(100) NOT NULL,
    email text UNIQUE NOT NULL
);

COMMIT;

Users Table SQL

BEGIN TRANSACTION;

CREATE TABLE users (
    id serial PRIMARY KEY,
    name VARCHAR(100),
    email text UNIQUE NOT NULL,
    entries BIGINT DEFAULT 0,
    joined TIMESTAMP NOT NULL
);

COMMIT;

Seed Data SQL - To populate with first user for testing

-- Seed data with a fake user for testing

BEGIN TRANSACTION;

insert into users (name, email, entries, joined) values ('aaa', 'a@a.com', 0, '2021-06-06');
insert into login (hash, email) values ('$2a$10$WAK21U0LWl7C//jJ.DOB2uPP1DJQh7KUDgasdyQeGzkop2Pzl8W7u', 'a@a.com');

COMMIT;

The password is hashed using Bcrypt, however the test login details are as follows; Email: a&a.com, Password: a

Redis Database Installation

You will need to install a local instance of Redis server for login authentication with JSON Web Tokens.

PostgreSQL Installation Guide: https://redislabs.com/blog/redis-on-windows-10/

Once you have completed the steps in the 'Installation Prerequisites' above you can continue on to the final installation steps below.

Installation

Open your Code Editor and 'CD' into your working directory, then download the repo to that location, by executing the following command in your terminal.

git clone https://github.com/rbhachu/smartbrain-backend-jwt.git

Once the repo has been downloaded, 'CD' to the newly downloaded project folder. Then execute the following command in your terminal.

npm install

Also check for any updates by running the following command too.

npm update

Configuration

There is a settings file in the root of the project folder called '.env' which is also known as the Environmental Variables file.

We now need to update this file to use actual values (remove brackets when you replace with actual values) for API_CLARIFAI, POSTGRES_URL and REDIS_URL.

#SERVER PORT
PORT=3001

#JWT SECRET KEY VALUE
JWT_SECRET=secret

#CLARIFAI API KEY
API_CLARIFAI=(your api key)

#POSTGRESQL DB
POSTGRES_URL=postgres://(<db_username>:<db_password><host_location:host_port>/<db_name>)

#REDIS DB
REDIS_URL=redis://:(<db_password><host_location>:<host_port>)

Deployment

Finally, to run the app, simply execute the following command in your terminal (ensuring you are in the correct project directory too).

npm start

After a few seconds, your browser should automatically open to the following link; http://localhost:3001 and display the following message in the browser.

SmartBrain Preview

Issues

If you encounter any issues, check the console in your browser, and also check your values for Clarifai API Key, PostgreSQL and Redis connection strings in the .env file are correct too.
If you continue to have issues, please drop me a message via LinkedIn and I will try to help you resolve the issue.

Author

👤 Rishi Singh Bhachu
Contact me via LinkedIn

Show your support

If you liked this project it would be greatly appreciated to show your support by simply giving this repo a ⭐️ rating too, many thanks!