Add current user endpoint to db authentication (#724)

This commit is contained in:
Roland Boon 2020-11-20 08:54:36 +01:00 • committed by GitHub
commit c3337d890b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 2 deletions

View file

@ -705,12 +705,13 @@ Below you find more information on each of the authentication types.
#### Database authentication
The database authentication middleware defines two new routes:
The database authentication middleware defines three new routes:
method path - parameters - description
----------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
POST /login - username + password - logs a user in by username and password
POST /logout - - logs out the currently logged in user
GET /me - - returns the user as which you're currently logged in
A user can be logged in by sending it's username and password to the login endpoint (in JSON format).
The authenticated user (with all it's properties) will be stored in the `$_SESSION['user']` variable.

View file

@ -7601,6 +7601,12 @@ namespace Tqdev\PhpCrudApi\Middleware {
}
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
}
if ($method == 'GET' && $path == 'me') {
if (isset($_SESSION['user'])) {
return $this->responder->success($_SESSION['user']);
}
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
}
if (!isset($_SESSION['user']) || !$_SESSION['user']) {
$authenticationMode = $this->getProperty('mode', 'required');
if ($authenticationMode == 'required') {

View file

@ -85,6 +85,12 @@ class DbAuthMiddleware extends Middleware
}
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
}
if ($method == 'GET' && $path == 'me') {
if (isset($_SESSION['user'])) {
return $this->responder->success($_SESSION['user']);
}
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
}
if (!isset($_SESSION['user']) || !$_SESSION['user']) {
$authenticationMode = $this->getProperty('mode', 'required');
if ($authenticationMode == 'required') {

View file

@ -16,6 +16,14 @@ Content-Type: application/json; charset=utf-8
Content-Type: application/json; charset=utf-8
Content-Length: 27
{"id":2,"username":"user2"}
===
GET /me
===
200
Content-Type: application/json; charset=utf-8
Content-Length: 27
{"id":2,"username":"user2"}
===
GET /records/invisibles/e42c77c6-06a4-4502-816c-d112c7142e6d