Skip to content

Commit 23d5b66

Browse files
committed
Add register endpoint to DbAuthMiddleware, fixes #732
1 parent 38f59ed commit 23d5b66

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

api.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7558,7 +7558,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
75587558
}
75597559
$path = RequestUtils::getPathSegment($request, 1);
75607560
$method = $request->getMethod();
7561-
if ($method == 'POST' && $path == 'login') {
7561+
if ($method == 'POST' && in_array($path, ['login', 'register'])) {
75627562
$body = $request->getParsedBody();
75637563
$username = isset($body->username) ? $body->username : '';
75647564
$password = isset($body->password) ? $body->password : '';
@@ -7568,6 +7568,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
75687568
$usernameColumn = $table->getColumn($usernameColumnName);
75697569
$passwordColumnName = $this->getProperty('passwordColumn', 'password');
75707570
$passwordColumn = $table->getColumn($passwordColumnName);
7571+
$registerUser = $this->getProperty('registerUser', '');
7572+
if ($path == 'register' && $registerUser) {
7573+
$data = json_decode($registerUser, true);
7574+
$data = is_array($data) ? $data : [];
7575+
$data[$usernameColumnName] = $username;
7576+
$data[$passwordColumnName] = password_hash($password, PASSWORD_DEFAULT);
7577+
$this->db->createSingle($table, $data);
7578+
}
75717579
$condition = new ColumnCondition($usernameColumn, 'eq', $username);
75727580
$returnedColumns = $this->getProperty('returnedColumns', '');
75737581
if (!$returnedColumns) {

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
4242
}
4343
$path = RequestUtils::getPathSegment($request, 1);
4444
$method = $request->getMethod();
45-
if ($method == 'POST' && $path == 'login') {
45+
if ($method == 'POST' && in_array($path, ['login', 'register'])) {
4646
$body = $request->getParsedBody();
4747
$username = isset($body->username) ? $body->username : '';
4848
$password = isset($body->password) ? $body->password : '';
@@ -52,6 +52,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5252
$usernameColumn = $table->getColumn($usernameColumnName);
5353
$passwordColumnName = $this->getProperty('passwordColumn', 'password');
5454
$passwordColumn = $table->getColumn($passwordColumnName);
55+
$registerUser = $this->getProperty('registerUser', '');
56+
if ($path == 'register' && $registerUser) {
57+
$data = json_decode($registerUser, true);
58+
$data = is_array($data) ? $data : [];
59+
$data[$usernameColumnName] = $username;
60+
$data[$passwordColumnName] = password_hash($password, PASSWORD_DEFAULT);
61+
$this->db->createSingle($table, $data);
62+
}
5563
$condition = new ColumnCondition($usernameColumn, 'eq', $username);
5664
$returnedColumns = $this->getProperty('returnedColumns', '');
5765
if (!$returnedColumns) {

tests/config/base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'middlewares' => 'sslRedirect,xml,cors,reconnect,dbAuth,jwtAuth,basicAuth,authorization,sanitation,validation,ipAddress,multiTenancy,pageLimits,joinLimits,customization',
88
'dbAuth.mode' => 'optional',
99
'dbAuth.returnedColumns' => 'id,username,password',
10+
'dbAuth.registerUser' => '1',
1011
'jwtAuth.mode' => 'optional',
1112
'jwtAuth.time' => '1538207605',
1213
'jwtAuth.secrets' => 'axpIrCGNGqxzx2R9dtXLIPUSqPo778uhb8CA0F4Hx',

tests/functional/002_auth/003_db_auth.log

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,38 @@ Content-Type: application/json; charset=utf-8
7676
Content-Length: 49
7777

7878
{"code":1011,"message":"Authentication required"}
79+
===
80+
POST /register
81+
Content-Type: application/json; charset=utf-8
82+
83+
{"username":"user3","password":"pass3"}
84+
===
85+
200
86+
Content-Type: application/json; charset=utf-8
87+
Content-Length: 27
88+
89+
{"id":3,"username":"user3"}
90+
===
91+
GET /me
92+
===
93+
200
94+
Content-Type: application/json; charset=utf-8
95+
Content-Length: 27
96+
97+
{"id":3,"username":"user3"}
98+
===
99+
POST /logout
100+
===
101+
200
102+
Content-Type: application/json; charset=utf-8
103+
Content-Length: 27
104+
105+
{"id":3,"username":"user3"}
106+
===
107+
DELETE /records/users/3
108+
===
109+
200
110+
Content-Type: application/json; charset=utf-8
111+
Content-Length: 1
112+
113+
1

0 commit comments

Comments
 (0)