Skip to content

Commit c3337d8

Browse files
authored
Add current user endpoint to db authentication (#724)
1 parent 87d01fc commit c3337d8

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,13 @@ Below you find more information on each of the authentication types.
705705

706706
#### Database authentication
707707

708-
The database authentication middleware defines two new routes:
708+
The database authentication middleware defines three new routes:
709709

710710
method path - parameters - description
711-
----------------------------------------------------------------------------------------
711+
---------------------------------------------------------------------------------------------------
712712
POST /login - username + password - logs a user in by username and password
713713
POST /logout - - logs out the currently logged in user
714+
GET /me - - returns the user as which you're currently logged in
714715

715716
A user can be logged in by sending it's username and password to the login endpoint (in JSON format).
716717
The authenticated user (with all it's properties) will be stored in the `$_SESSION['user']` variable.

api.php

+6
Original file line numberDiff line numberDiff line change
@@ -7601,6 +7601,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
76017601
}
76027602
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
76037603
}
7604+
if ($method == 'GET' && $path == 'me') {
7605+
if (isset($_SESSION['user'])) {
7606+
return $this->responder->success($_SESSION['user']);
7607+
}
7608+
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
7609+
}
76047610
if (!isset($_SESSION['user']) || !$_SESSION['user']) {
76057611
$authenticationMode = $this->getProperty('mode', 'required');
76067612
if ($authenticationMode == 'required') {

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8585
}
8686
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
8787
}
88+
if ($method == 'GET' && $path == 'me') {
89+
if (isset($_SESSION['user'])) {
90+
return $this->responder->success($_SESSION['user']);
91+
}
92+
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
93+
}
8894
if (!isset($_SESSION['user']) || !$_SESSION['user']) {
8995
$authenticationMode = $this->getProperty('mode', 'required');
9096
if ($authenticationMode == 'required') {

tests/functional/002_auth/003_db_auth.log

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Content-Type: application/json; charset=utf-8
1616
Content-Type: application/json; charset=utf-8
1717
Content-Length: 27
1818

19+
{"id":2,"username":"user2"}
20+
===
21+
GET /me
22+
===
23+
200
24+
Content-Type: application/json; charset=utf-8
25+
Content-Length: 27
26+
1927
{"id":2,"username":"user2"}
2028
===
2129
GET /records/invisibles/e42c77c6-06a4-4502-816c-d112c7142e6d

0 commit comments

Comments
 (0)