Start working on backend api
This commit is contained in:
parent
2022cb88e4
commit
291f4f446a
|
@ -5,4 +5,6 @@ public/*.js
|
|||
public/*.map
|
||||
public/assets/
|
||||
|
||||
backend/vendor/
|
||||
|
||||
*.log*
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
class Db {
|
||||
private $dbh;
|
||||
function _construct() {
|
||||
$this->dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password');
|
||||
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
public function query ($query, $params = array()) {
|
||||
$stmt = $this->dbh->prepare($query);
|
||||
$stmt->execute($params);
|
||||
return $stmt;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
class Response {
|
||||
public static function out ($data, $http_code) {
|
||||
header('Content-Type: application/json');
|
||||
http_response_code($http_code);
|
||||
echo json_encode($data);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
require_once('./vendor/firebase/php-jwt/src/JWT.php');
|
||||
use \Firebase\JWT\JWT;
|
||||
|
||||
class Token {
|
||||
private $key;
|
||||
function _construct() {
|
||||
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
|
||||
}
|
||||
|
||||
public function encode ($data) {
|
||||
return JWT::encode($data, $this->key);
|
||||
}
|
||||
|
||||
public function decode ($token) {
|
||||
return JWT::decode($token, $this->key, array('HS256'));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
require_once('./Db');
|
||||
require_once('./Token');
|
||||
|
||||
class User {
|
||||
private $db;
|
||||
private $token;
|
||||
function _construct () {
|
||||
$this->db = new Db();
|
||||
$this->token = new Token();
|
||||
}
|
||||
|
||||
public function logIn ($email, $password) {
|
||||
$query = 'SELECT * FROM users WHERE email=?';
|
||||
$user = $this->db->query($query, array($email))->fetch();
|
||||
if ($user) {
|
||||
if ($user['old_password'] !== 'NULL') {
|
||||
if ($user['old_password'] === crypt($password, $email)) {
|
||||
if ($this->upgradePassword($password)) {
|
||||
return $this->logIn($email, $password);
|
||||
}
|
||||
}
|
||||
} else if (password_verify($password, $user['password'])) {
|
||||
$user_data = array(
|
||||
'id' => $user['id'],
|
||||
'isMember' => $this->hasMembership($user['id']),
|
||||
'dictionary' => $user['current_dictionary'],
|
||||
);
|
||||
return $this->token->encode($user_data);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function hasMembership ($id) {
|
||||
$current_membership = "SELECT * FROM memberships WHERE user=$id AND start_date>=CURRENT_TIMESTAMP AND CURRENT_TIMESTAMP<expire_date";
|
||||
$stmt = $this->db->query($current_membership)->rowCount() > 0;
|
||||
}
|
||||
|
||||
public function upgradePassword ($password) {
|
||||
$new_password = password_hash($password, PASSWORD_DEFAULT);
|
||||
$update_query = 'UPDATE users SET old_password=NULL, password=? WHERE id=' . $user['id'];
|
||||
$stmt = $this->db->query($update_query, array($new_password));
|
||||
return $stmt->rowCount() === 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "lexiconga/backend",
|
||||
"description": "The PHP backend API for Lexiconga (lexicon.ga)",
|
||||
"type": "project",
|
||||
"require": {
|
||||
"firebase/php-jwt": "^5.0"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Robbie Antenesse",
|
||||
"email": "dev@alamantus.com"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "0032d2a2e253b895d7f3dbb3856674da",
|
||||
"packages": [
|
||||
{
|
||||
"name": "firebase/php-jwt",
|
||||
"version": "v5.0.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/firebase/php-jwt.git",
|
||||
"reference": "9984a4d3a32ae7673d6971ea00bae9d0a1abba0e"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/9984a4d3a32ae7673d6971ea00bae9d0a1abba0e",
|
||||
"reference": "9984a4d3a32ae7673d6971ea00bae9d0a1abba0e",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": " 4.8.35"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Firebase\\JWT\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Neuman Vong",
|
||||
"email": "neuman+pear@twilio.com",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Anant Narayanan",
|
||||
"email": "anant@php.net",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
|
||||
"homepage": "https://github.com/firebase/php-jwt",
|
||||
"time": "2017-06-27T22:17:23+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
"platform-dev": []
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
require_once('./Response.php');
|
||||
require_once('./User.php');
|
||||
|
||||
$action = $_POST['action'];
|
||||
$token = $_POST['token'];
|
||||
|
||||
switch ($action) {
|
||||
case 'login': {
|
||||
if ($_POST['email'] && $_POST['password']) {
|
||||
$user = new User();
|
||||
$token = $user->logIn($_POST['email'], $_POST['password']);
|
||||
if ($token !== false) {
|
||||
return Response::out(array(
|
||||
'data' => $token,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::out(array(
|
||||
'data' => 'Could not log in: incorrect data',
|
||||
'error' => true,
|
||||
), 400);
|
||||
}
|
||||
return Response::out(array(
|
||||
'data' => 'Could not log in: required information missing',
|
||||
'error' => true,
|
||||
), 500);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue