[JSONWEBTOKEN] PHP JWT & REST API Authentication Tutorial: Login and Signup full (ok)

Ví dụ đã hoàn thành :)

Inside your MySQL Database create a new Database called php_auth_api. And after that, use the following SQL code to create users table and the structure of this table –

SQL code for `users` Table
CREATE TABLE `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
 `email` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
 `password` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

C:\xampp\htdocs\abc\classes\Database.php

<?php
class Database {
  // CHANGE THE DB INFO ACCORDING TO YOUR DATABASE
  private $db_host     = 'localhost';
  private $db_name     = 'php_auth_api';
  private $db_username = 'root';
  private $db_password = '';
  public function dbConnection() {
    try {
      $conn = new PDO('mysql:host=' . $this->db_host . ';dbname=' . $this->db_name, $this->db_username, $this->db_password);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      return $conn;
    } catch (PDOException $e) {
      echo "Connection error " . $e->getMessage();
      exit;
    }
  }
}

C:\xampp\htdocs\abc\classes\JwtHandler.php

Chú ý: Những file sau đây được lấy từ jwt được lấy bằng "composer require firebase/php-jwt" JWT.php ExpiredException.php SignatureInvalidException.php BeforeValidException.php

C:\xampp\htdocs\abc\middlewares\Auth.php

C:\xampp\htdocs\abc\login.php

C:\xampp\htdocs\abc\register.php

C:\xampp\htdocs\abc\user-info.php

65B
Open

React JS + PHP + MySQL DB Login & Registration System

Chandan TuduByChandan TuduMay 14, 2020React JSReact JS + PHP + MySQL DB Login & Registration System

Learn how to create a simple Login and Registration System using the React JS, PHP, and MySQL Database. And in this tutorial, instead of redux, we will use the context API.

The purpose of this tutorial is to give an idea of creating a Login and Registration system using React JS & PHP.

Screenshots of this React JS Login & Registration System

React JS Login PageLogin PageReact JS Registration PageSign Up PageReact JS Home page after loginAfter Login (Home) Page

Follow the below to create this React JS & PHP Login System

React JS is used to building user interfaces, so we can’t handle the backend with this. To interact with the backend, we will use the PHP RESTful API.

I have already made a tutorial on how to create PHP login & registration RESTful API. And in this tutorial, I will use that PHP RESTful API. So, you must first check out that.

After making the PHP RESTful API, set up your React CLI Environment & Install Axios package (to perform the ajax requests we will use the Axios JS).

👉 API URLs

👉 Creating files & folders

Now we will create our application files and folders. But, before going further let’s take a look at the 📁src folder structure. See the following image –React JS Login & Registration app src folder structureApplication src folder structure

MyContext.js

First, we will set up our app context. So inside the src folder create a new folder called 📁context, and here we have to create the MyContext.js file.

👉 Creating Components

After creating the context file, Now we will create our components. So, again inside the src folder create a new folder called 📁components. And here we will create our components.

In the components folder we have to create three components –

  1. Login.js – Where a user can log in

  2. Register.js – Where a user can Sign up

  3. Home.js – A user can access this page after login.

App.js

After creating the components, now we have to edit the App.js file –

index.js

We don’t have to edit this file. Leave it as it is.

index.css

Here is the stylesheet of this application –

Now time to test it. To test this application, execute the npm start command in your terminal.

PHP Login and Registration RESTful API

PHP Login and Registration RESTful API

Chandan TuduByChandan TuduApril 21, 2020PHP ProjectsPHP Login and Registration RESTful API

In this tutorial, you will learn how to create a simple Login and Registration RESTful API using PHP and MySQL Database.

Before going further, if you wish you can read my already made tutorial on how to create CRUD RESTful API. Maybe this tutorial is a bit useful for you here.

And in this tutorial, we will use the JWT to authenticate a user. So, I suggest you read this tutorial first – How to implement JWT with PHP.

MySQL Database Setup

  • Database Namephp_auth_api

  • Table Nameusers

Inside your MySQL Database create a new Database called php_auth_api. And after that, use the following SQL code to create users table and the structure of this table –

Creating files & folders

First, Open your Xampp htdocs dir or you server www dir and create a new folder called php-login-registration-api. This is our application folder.

Inside the application folder, we will create the following files and folders –php-login-registration-api folder structurePHP Login Registration API Folder structure

📁 jwt Folder Setup

  1. Download the JWT Package from Here.

  2. Extract the ZIP file.

  3. Copy the src folder inside the php-login-registration-api folder.

  4. Rename the src folder to jwt.

📁 classes Folder Setup

Now inside the application folder, you have to create a new folder called classes. After that, inside the classes folder, we will create two files or classes –

  • Database.php – For making the database connection

  • JwtHandler.php – For handling the JWT actions like encoding and decoding token

📁 middlewares Folder Setup

After creating the classes now again inside the application folder create a new folder called middlewares. And inside the middlewares folder, you just have to create one class –

  • Auth.php – For validating the token (checking if the token is valid or not)

  • Valid Token – return the User

  • Invalid Token – return null

Creating other files

Now time to create the base files –

  • register.php – For user registration.

  • login.php – For user login.

  • user-info.php – After login, this page is accessible with a valid token.

Testing the APIs

🔗 API URLs –

👉 Register a user

Register a user api testRegister a user API Test

👉 Login the user

login the user api testLogin the user API Test

👉 Get the user information by giving the token

  • Headers Key Name – Authorization

  • Value of the KeyBearer your_token

Get User Information By Giving the token

Source

Last updated

Was this helpful?