From b163d70e9cd27b962a65e9958008abace8753e8a Mon Sep 17 00:00:00 2001 From: Josef Citrine Date: Mon, 2 Jan 2017 03:33:21 +0000 Subject: [PATCH] Added API to get logged in user --- .../Controllers/Api/Web/AccountController.php | 28 +++++++++++++++++++ routes/web.php | 1 + 2 files changed, 29 insertions(+) diff --git a/app/Http/Controllers/Api/Web/AccountController.php b/app/Http/Controllers/Api/Web/AccountController.php index 815ecb1a..4f20726d 100644 --- a/app/Http/Controllers/Api/Web/AccountController.php +++ b/app/Http/Controllers/Api/Web/AccountController.php @@ -23,6 +23,7 @@ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web; use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase; use Poniverse\Ponyfm\Commands\SaveAccountSettingsCommand; use Poniverse\Ponyfm\Models\User; +use Poniverse\Ponyfm\Models\Image; use Gate; use Auth; use Request; @@ -39,6 +40,33 @@ class AccountController extends ApiControllerBase ]); } + public function getCurrentUser() { + $current_user = Auth::user(); + + if ($current_user != null) { + $user = User::where('id', $current_user->id)->whereNull('disabled_at')->first(); + + if ($user == null) { + return Response::json(['error' => 'You are not logged in'], 404); + } + + return Response::json([ + 'id' => $user->id, + 'name' => $user->display_name, + 'slug' => $user->slug, + 'url' => $user->url, + 'is_archived' => $user->is_archived, + 'avatars' => [ + 'small' => $user->getAvatarUrl(Image::SMALL), + 'normal' => $user->getAvatarUrl(Image::NORMAL) + ], + 'created_at' => $user->created_at + ], 200); + } else { + return Response::json(['error' => 'You are not logged in'], 404); + } + } + public function getSettings($slug) { $user = null; diff --git a/routes/web.php b/routes/web.php index 53d63d0c..751fc104 100644 --- a/routes/web.php +++ b/routes/web.php @@ -214,6 +214,7 @@ Route::group(['prefix' => 'api/web'], function () { Route::delete('/announcements/{id}', 'Api\Web\AnnouncementsController@deleteItem')->where('id', '\d+'); }); + Route::get('/auth/current', 'Api\Web\AccountController@getCurrentUser'); Route::post('/auth/logout', 'Api\Web\AuthController@postLogout'); });