Add composer and some things to help with dev + basics

This commit is contained in:
Robbie Antenesse 2019-05-13 17:18:31 -06:00
parent b1516021bd
commit 04176ef92f
8 changed files with 894 additions and 21 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/ node_modules/
.cache/ .cache/
dist/ dist/
vendor/

9
composer.json Normal file
View File

@ -0,0 +1,9 @@
{
"config": {
"vendor-dir": "src/php/vendor"
},
"require": {
"firebase/php-jwt": "^5.0",
"hashids/hashids": "^4.0"
}
}

131
composer.lock generated Normal file
View File

@ -0,0 +1,131 @@
{
"_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#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b2cfd40da6d3f90b0110c83afb8c3c0e",
"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"
},
{
"name": "hashids/hashids",
"version": "4.0.0",
"source": {
"type": "git",
"url": "https://github.com/ivanakimov/hashids.php.git",
"reference": "43bb2407f16a631f0128f47bcb67ff986c63dde2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ivanakimov/hashids.php/zipball/43bb2407f16a631f0128f47bcb67ff986c63dde2",
"reference": "43bb2407f16a631f0128f47bcb67ff986c63dde2",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"php": "^7.2"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
},
"suggest": {
"ext-bcmath": "Required to use BC Math arbitrary precision mathematics (*).",
"ext-gmp": "Required to use GNU multiple precision mathematics (*)."
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"autoload": {
"psr-4": {
"Hashids\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ivan Akimov",
"email": "ivan@barreleye.com",
"homepage": "https://twitter.com/IvanAkimov"
},
{
"name": "Vincent Klaiber",
"email": "hello@doubledip.se",
"homepage": "https://doubledip.se"
}
],
"description": "Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers",
"homepage": "http://hashids.org/php",
"keywords": [
"bitly",
"decode",
"encode",
"hash",
"hashid",
"hashids",
"ids",
"obfuscate",
"youtube"
],
"time": "2019-04-03T13:40:29+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

View File

@ -7,15 +7,19 @@
"author": "Robbie Antenesse <dev@alamantus.com>", "author": "Robbie Antenesse <dev@alamantus.com>",
"license": "UNLICENCED", "license": "UNLICENCED",
"scripts": { "scripts": {
"start": "parcel watch index.html", "start": "concurrently \"npm run watch-js\" \"npm run watch-php\"",
"watch-js": "parcel watch index.html",
"watch-php": "cpx src/php/**/* dist -v -w",
"bundle": "parcel build index.html && cpx src/php/**/* dist",
"serve-frontend-only": "parcel index.html", "serve-frontend-only": "parcel index.html",
"bundle": "parcel build index.html",
"clear": "npm run clear-dist && npm run clear-cache", "clear": "npm run clear-dist && npm run clear-cache",
"clear-dist": "rimraf dist/*", "clear-dist": "rimraf dist/*",
"clear-cache": "rimraf .cache/*" "clear-cache": "rimraf .cache/*"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^9.5.1", "autoprefixer": "^9.5.1",
"concurrently": "^4.1.0",
"cpx": "^1.5.0",
"parcel-bundler": "^1.12.3", "parcel-bundler": "^1.12.3",
"rimraf": "^2.6.3", "rimraf": "^2.6.3",
"sass": "^1.19.0" "sass": "^1.19.0"

24
src/php/api/Response.php Normal file
View File

@ -0,0 +1,24 @@
<?php
class Response {
private static function defaultHeaders () {
header('Access-Control-Allow-Origin: *');
header('Expires: Sun, 01 Nov 2015 22:46:51 GMT');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
public static function json ($data, $http_code = 200) {
header('Content-Type: application/json; charset=utf-8');
Response::defaultHeaders();
http_response_code($http_code);
echo json_encode($data);
}
public static function html ($html, $http_code = 200) {
header('Content-Type: text/html; charset=utf-8');
Response::defaultHeaders();
http_response_code($http_code);
echo $html;
}
}

39
src/php/api/Token.php Normal file
View File

@ -0,0 +1,39 @@
<?php
require_once('../vendor/autoload.php');
use \Firebase\JWT\JWT;
use \Hashids\Hashids;
class Token {
private $key;
private $hashids;
function __construct() {
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
$this->hashids = new Hashids($this->key, 10);
}
public function encode ($data) {
return JWT::encode($data, $this->key);
}
public function decode ($token) {
try {
return JWT::decode($token, $this->key, array('HS256'));
} catch (Exception $ex) {
return false;
}
}
public function hash ($id) {
return $this->hashids->encode($id);
}
public function unhash ($hash) {
$unhashed = $this->hashids->decode($hash);
if (count($unhashed) > 0) {
return $unhashed[0];
}
return false;
}
}

26
src/php/api/index.php Normal file
View File

@ -0,0 +1,26 @@
<?php
require_once('./Response.php');
require_once('./Token.php');
$inputJSON = file_get_contents('php://input');
$request= json_decode($inputJSON, true);
$action = isset($request['action']) ? $request['action'] : '';
switch ($action) {
case 'login': {
if (isset($request['email']) && isset($request['password'])) {
$token = new Token();
$token = $token->encode([
'email' => $request['email'],
'password' => $request['password'],
]);
setcookie('token', $token);
return Response::json([
'data' => 'cookie saved',
'error' => false,
], 200);
}
break;
}
}

675
yarn.lock

File diff suppressed because it is too large Load Diff