Code Monkey home page Code Monkey logo

react-bhx-auth's Introduction

react-bhx-auth

Authentication is one of the most time-consuming things to do when you have to deal with web projects. This projects is a ReactJS authentication using JWT (json web token) with REST calls to a server.

There's a backend folder inside the project with PHP pages (Apache server) for login.

The token is saved locally using local storage.

Dependencies

  • Axios

Feat

  • ReactJS with create-react-app
  • Typescript
  • JWT - Token
  • PHP backend
  • REST
  • Axios

How to use

In order to use this project get the component's folder (components/Auth) and the helper's folder. In components, the component Auth has two main functions:

  • serverLoginUser: to send a post request to login
  • getAuth: get the current status of the user.

Login form

In the example inside this project you can test one form login but you can pretty much use the form validator you like.

Local storage

When the user loggin successfully, the component save a json inside the local storage.

{
  "auth": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJiaHhzaXRlcyIsImVtYWlsIjoiZm9vQG1haWwuY29tIn0.Sk0VtWo_UNSaW-TfGjvMGpiOwxx4cz9SEDkjx70MZvE",
    "email": "[email protected]",
    "timestamp": "2020-09-02 21:14:13"
  }
}

Backend

Even though is not the main purpose, inside this project you will find a backend folder with PHP files.

You can put this folder inside your localhost and change the endpoint inside the components folder. The default value for this is pointing to some BHX Sites path with the same file but you can do this locally or even try use NodeJS instead of PHP (Apache).

Htaccess

PHP - Apache: In order to make the authorization call using apache, provide the htaccess configuration in the header:

<IfModule mod_rewrite.c>
    RewriteEngine On
    SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
</IfModule>

Fake data inside PHP

For testing purposes you will need to put [email protected] as e-mail and 123 as password.

Inside the PHP (login.php) page you will find the a simple checker to check if the login is correct.

// fake database return
$DATABASE_PASSWORD = md5('123');
$DATABASE_EMAIL = '[email protected]';

JWT serverKey

In order to use JWT you have to define a key (string). Set this inside the PHP page (auth.php)

// JWT config
$JWTServerkey = '123123';

Example

login.gif

react-bhx-auth's People

Contributors

coffezilla avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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