Commit e3a817a4 authored by CUNY Florian's avatar CUNY Florian
Browse files

Added /api/users/me : Get Me (Connected User)

parent dfdc7e52
......@@ -5,6 +5,33 @@ require_once __DIR__ . '/../db/DBConnection.php';
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
/**
* Get Me (Connected User) (see documentation)
*/
$app->get('/api/users/me', function( Request $request, Response $response){
$token = get_token_infos($request);
try {
$dbconn = new DB\DBConnection();
$db = $dbconn->connect();
// query
$sql = "SELECT * FROM users WHERE (username='" . $token->username . "' AND first_name='" . $token->first_name . "')";
$stmt = $db->query( $sql );
$user = $stmt->fetchAll( PDO::FETCH_OBJ )[0];
$db = null; // clear db object
}
catch( PDOException $e ) {
echo $e;
// response : 500 : PDO Error (DB)
$response->getBody()->write('{"error": {"msg": "' . $e->getMessage() . '"}}');
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
}
$response->getBody()->write(json_encode($user));
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
});
/**
* Get All Users : admin only (see documentation)
*/
......@@ -77,4 +104,4 @@ function get_token_infos(Request $request){
else{
throw new Auth\UnauthenticatedException("Unable to find Authorization Header");
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment