You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

<?php
include_once './config/bootstrap.php';
require "../vendor/autoload.php";
use \Firebase\JWT\JWT;
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");
$email = '';
$password = '';
$databaseService = new DatabaseService();
$conn = $databaseService->getConnection();
$data = json_decode(file_get_contents("php://input"));
$email = trim($data->email);
$password = trim($data->password);
$query = "SELECT id, first_name, last_name, password
FROM users
WHERE email = :email
LIMIT 0,1";
$stmt = $conn->prepare( $query );
$stmt->bindParam(':email', $email);
$stmt->execute();
$num = $stmt->rowCount();
if($num > 0) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $row['id'];
$firstname = $row['first_name'];
$lastname = $row['last_name'];
$password2 = $row['password'];
if(password_verify($password, $password2)) {
$token = array(
"iss" => $GLOBALS['CONF']['CLAIM']['ISSUER'],
"aud" => $GLOBALS['CONF']['CLAIM']['AUDIENCE'],
"iat" => $GLOBALS['CONF']['CLAIM']['ISSUE_DATE'],
"nbf" => $GLOBALS['CONF']['CLAIM']['NOT_BEFORE'],
"exp" => $GLOBALS['CONF']['CLAIM']['EXPIRE'],
"data" => array(
"id" => $id,
"firstname" => $firstname,
"lastname" => $lastname,
"email" => $email
));
http_response_code(200);
$jwt = JWT::encode($token, $GLOBALS['CONF']['CLAIM']['SECRET']);
echo json_encode(
array(
"message" => "Successful login.",
"jwt" => $jwt,
"email" => $email,
"expireAt" => $GLOBALS['CONF']['CLAIM']['EXPIRE']
));
} else {
http_response_code(401);
echo json_encode(array("message" => "Login failed"));
}
} else {
http_response_code(401);
echo json_encode(array("message" => "Login failed"));
}
?>