forked from joyofpw/pw-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
85 lines (63 loc) · 1.68 KB
/
login.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
<?php
/**
* login.php
* Create a new template named login
* and a page with that template
* for testing the endpoint
*/
include_once './rest/core/rest.php';
include_once './rest/languages/languages.php';
include_once './rest/login/errors.php';
use Rest\Errors\MethodNotAllowed as MethodNotAllowed;
use Rest\Request as Request;
use Rest\Response as Response;
use Rest\Method as Method;
use Rest\Header as Header;
use Login\Errors;
use Languages\Language as Language;
$response = new Response();
$response->allowMethod(Method::POST);
$params = Request::params();
if (!Request::isPost()) {
$response->setError(MethodNotAllowed::error());
} else {
$username = $params['username'];
$password = $params['password'];
if (!$username || !$password) {
// Basic Auth
$username = Header::username();
$password = Header::password();
}
if ((!isset($username) || $username == '') ||
(!isset($password) || $password == '')) {
$response->setError(Login\Errors\InvalidCredentials::error());
} else {
if ($username == 'hello' && $password == 'world') {
$response->output['data']['name'] = 'Tony';
$response->output['data']['lastname'] = 'Stark';
$response->output['data']['job'] = 'Ironman';
} else {
$response->setError(Login\Errors\InvalidCredentials::error());
}
}
}
$response->addArray(Language::current());
$headerParam = Header::get('origin');
$response->addArray(['_request_headers' => Header::getAll()]);
$response->addArray(['headerParam' => $headerParam]);
/*
Should render
{
"data": {
"name": "Tony",
"lastname": "Stark",
"job": "Ironman"
},
"_language": {
"id": 1017,
"code": "default",
"name": "English"
}
}
*/
$response->render();