-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
103 lines (79 loc) · 2.38 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
use App\Middlewares\AuthMiddleware;
use App\Middlewares\MethodMiddleware;
use App\Middlewares\ResponseMiddleware;
use Phalcon\Di\FactoryDefault;
use Phalcon\Events\Manager;
error_reporting(E_ALL);
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/src');
require_once(BASE_PATH . '/vendor/autoload.php');
try {
/**
* The FactoryDefault Dependency Injector automatically registers
* the services that provide a full stack framework.
*/
$di = new FactoryDefault();
/**
* Read services
*/
include APP_PATH . '/config/services.php';
/**
* Get config service for use in inline setup below
*/
$config = $di->getConfig();
/**
* Handle the request
*/
$application = new \Phalcon\Mvc\Micro($di);
$eventsManager = new Manager();
$eventsManager->attach('micro', new AuthMiddleware());
$application->before(new AuthMiddleware());
$eventsManager->attach('micro', new MethodMiddleware());
$application->before(new MethodMiddleware());
$eventsManager->attach('micro', new ResponseMiddleware());
$application->after(new ResponseMiddleware());
$application->post(
"/auth/logout",
function () {
$this->auth->logout();
return ['message' => 'Successfully logged out'];
}
);
$application->post(
"/auth/refresh",
function () {
$token = $this->auth->refresh();
return $token->toResponse();
}
);
$application->post(
'/auth/login',
function () {
$credentials = [
'email' => $this->request->getJsonRawBody()->email,
'password' => $this->request->getJsonRawBody()->password
];
$this->auth->claims(['aud' => [
$this->request->getURI()
]]);
if (! $token = $this->auth->attempt($credentials)) {
return ['error' => 'Unauthorized'];
}
return $token->toResponse();
}
);
$application->get(
'/',
function () {
return [
'message' => 'hello, my friend'
];
}
);
$application->handle($_SERVER['REQUEST_URI']);
} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
}