User Authentication using REST API and JSON Web Tokens (JWT) – Step 5

User Authentication using REST API and JSON Web Tokens (JWT) – Step 5

Simple Login via Unique ID

Create a new file login.php file inside user folder and use the following code

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers,Authorization, X-Requested-With");

// files needed to connect to database
include '../config/database.php';
include '../objects/user.php';

// get database connection
$database = new Database();
$db = $database->getConection();

// instantiate user object
$user = new User($db);

// get posted data
$json = json_encode($_POST);
$data = json_decode($json);

if (isset($_GET["id"])) {
    $stmt = $user->readSingleRecord($_GET["id"]);
    if ($stmt) {
        $row = $stmt->fetchAll();
        http_response_code(200);
        echo json_encode(array("message" => "Login Successfully.", "data" => $row));
    } else {
        http_response_code(401);
        echo json_encode(array("message" => "Invalid Credential."));

    }

}

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*