From a7ab7f56ef583f63a7e99215286f13ad3756237d Mon Sep 17 00:00:00 2001 From: Kevin ANATOLE Date: Sun, 1 Nov 2020 08:50:51 +0000 Subject: [PATCH] Profile - Save the profile into the current request when the user is authenticated - Created the migration for the profiles table - Created the Profile model - Created route to retrieve the current user's profile --- app/Http/Controllers/ProfilesController.php | 19 ++ app/Http/Middleware/FirebaseMiddleware.php | 23 +- app/Profile.php | 23 ++ app/User.php | 64 ++--- bootstrap/app.php | 246 +++++++++--------- ...020_10_31_230717_create_profiles_table.php | 24 ++ routes/web.php | 2 + 7 files changed, 235 insertions(+), 166 deletions(-) create mode 100644 app/Http/Controllers/ProfilesController.php create mode 100644 app/Profile.php create mode 100644 database/migrations/2020_10_31_230717_create_profiles_table.php diff --git a/app/Http/Controllers/ProfilesController.php b/app/Http/Controllers/ProfilesController.php new file mode 100644 index 0000000..cafbf3b --- /dev/null +++ b/app/Http/Controllers/ProfilesController.php @@ -0,0 +1,19 @@ +query(); + $user = $params['user']; + return response()->json([ + 'id' => $user['id'], + 'display_name' => $user['display_name'], + 'picture' => $user['picture'], + ]); + } +} diff --git a/app/Http/Middleware/FirebaseMiddleware.php b/app/Http/Middleware/FirebaseMiddleware.php index f5e2a90..b3e03b5 100644 --- a/app/Http/Middleware/FirebaseMiddleware.php +++ b/app/Http/Middleware/FirebaseMiddleware.php @@ -3,6 +3,7 @@ namespace App\Http\Middleware; +use App\Profile; use App\User; use Closure; use Kreait\Laravel\Firebase\Facades\FirebaseAuth; @@ -44,23 +45,23 @@ class FirebaseMiddleware try { $decoded = FirebaseAuth::verifyIdToken($token); $uid = $decoded->getClaim('sub'); - $user = User::where('provider_id', $uid)->get(); - $user = $user[0] ?? null; + $profile = Profile::where('uid', $uid)->get(); + $profile = $profile[0] ?? null; - if (!$user) { + if (!$profile) { /** @var \Kreait\Firebase\Auth\UserRecord $data */ $data = FirebaseAuth::getUser($uid); - $user = new User(); - $user->provider_id = $data->uid; - $user->email = $data->email; - $user->name = $data->displayName ?? 'No Name User'; - $user->image = $data->photoUrl ?? ''; - $user->save(); + $profile = new Profile(); + $profile->uid = $data->uid; + $profile->display_name = $data->displayName ?? 'No Name User'; + $profile->picture = $data->photoUrl ?? ''; + $profile->save(); } - return $user; + return $profile; } catch (\Exception $e) { + \Log::error($e->getMessage()); return null; - }; + } } } diff --git a/app/Profile.php b/app/Profile.php new file mode 100644 index 0000000..93e1747 --- /dev/null +++ b/app/Profile.php @@ -0,0 +1,23 @@ +bootstrap(); - -date_default_timezone_set(env('APP_TIMEZONE', 'UTC')); - -/* -|-------------------------------------------------------------------------- -| Create The Application -|-------------------------------------------------------------------------- -| -| Here we will load the environment and create the application instance -| that serves as the central piece of this framework. We'll use this -| application as an "IoC" container and router for this framework. -| -*/ - -$app = new Laravel\Lumen\Application( - dirname(__DIR__) -); - -$app->withFacades(); -$app->withEloquent(); - -/* -|-------------------------------------------------------------------------- -| Register Container Bindings -|-------------------------------------------------------------------------- -| -| Now we will register a few bindings in the service container. We will -| register the exception handler and the console kernel. You may add -| your own bindings here if you like or you can make another file. -| -*/ - -$app->singleton( - Illuminate\Contracts\Debug\ExceptionHandler::class, - App\Exceptions\Handler::class -); - -$app->singleton( - Illuminate\Contracts\Console\Kernel::class, - App\Console\Kernel::class -); - -/* -|-------------------------------------------------------------------------- -| Register Config Files -|-------------------------------------------------------------------------- -| -| Now we will register the "app" configuration file. If the file exists in -| your configuration directory it will be loaded; otherwise, we'll load -| the default version. You may register other files below as needed. -| -*/ - -$app->configure('app'); - -/* -|-------------------------------------------------------------------------- -| Register Middleware -|-------------------------------------------------------------------------- -| -| Next, we will register the middleware with the application. These can -| be global middleware that run before and after each request into a -| route or middleware that'll be assigned to some specific routes. -| -*/ - -// $app->middleware([ -// App\Http\Middleware\ExampleMiddleware::class -// ]); - -// $app->routeMiddleware([ -// 'auth' => App\Http\Middleware\Authenticate::class, -// ]); - -$app->middleware([ - \App\Http\Middleware\CorsMiddleware::class -]); - -$app->routeMiddleware([ - 'auth' => App\Http\Middleware\FirebaseMiddleware::class, -]); - -/* -|-------------------------------------------------------------------------- -| Register Service Providers -|-------------------------------------------------------------------------- -| -| Here we will register all of the application's service providers which -| are used to bind services into the container. Service providers are -| totally optional, so you are not required to uncomment this line. -| -*/ - -// $app->register(App\Providers\AppServiceProvider::class); -// $app->register(App\Providers\AuthServiceProvider::class); -// $app->register(App\Providers\EventServiceProvider::class); -$app->register(Kreait\Laravel\Firebase\ServiceProvider::class); - -/* -|-------------------------------------------------------------------------- -| Load The Application Routes -|-------------------------------------------------------------------------- -| -| Next we will include the routes file so that they can all be added to -| the application. This will provide all of the URLs the application -| can respond to, as well as the controllers that may handle them. -| -*/ - -$app->router->group([ - 'namespace' => 'App\Http\Controllers', -], function ($router) { - require __DIR__.'/../routes/web.php'; -}); - -return $app; +bootstrap(); + +date_default_timezone_set(env('APP_TIMEZONE', 'UTC')); + +/* +|-------------------------------------------------------------------------- +| Create The Application +|-------------------------------------------------------------------------- +| +| Here we will load the environment and create the application instance +| that serves as the central piece of this framework. We'll use this +| application as an "IoC" container and router for this framework. +| +*/ + +$app = new Laravel\Lumen\Application( + dirname(__DIR__) +); + +$app->withFacades(); +$app->withEloquent(); + +/* +|-------------------------------------------------------------------------- +| Register Container Bindings +|-------------------------------------------------------------------------- +| +| Now we will register a few bindings in the service container. We will +| register the exception handler and the console kernel. You may add +| your own bindings here if you like or you can make another file. +| +*/ + +$app->singleton( + Illuminate\Contracts\Debug\ExceptionHandler::class, + App\Exceptions\Handler::class +); + +$app->singleton( + Illuminate\Contracts\Console\Kernel::class, + App\Console\Kernel::class +); + +/* +|-------------------------------------------------------------------------- +| Register Config Files +|-------------------------------------------------------------------------- +| +| Now we will register the "app" configuration file. If the file exists in +| your configuration directory it will be loaded; otherwise, we'll load +| the default version. You may register other files below as needed. +| +*/ + +$app->configure('app'); + +/* +|-------------------------------------------------------------------------- +| Register Middleware +|-------------------------------------------------------------------------- +| +| Next, we will register the middleware with the application. These can +| be global middleware that run before and after each request into a +| route or middleware that'll be assigned to some specific routes. +| +*/ + +// $app->middleware([ +// App\Http\Middleware\ExampleMiddleware::class +// ]); + +// $app->routeMiddleware([ +// 'auth' => App\Http\Middleware\Authenticate::class, +// ]); + +$app->middleware([ + \App\Http\Middleware\CorsMiddleware::class +]); + +$app->routeMiddleware([ + 'auth' => App\Http\Middleware\FirebaseMiddleware::class, +]); + +/* +|-------------------------------------------------------------------------- +| Register Service Providers +|-------------------------------------------------------------------------- +| +| Here we will register all of the application's service providers which +| are used to bind services into the container. Service providers are +| totally optional, so you are not required to uncomment this line. +| +*/ + +// $app->register(App\Providers\AppServiceProvider::class); + $app->register(App\Providers\AuthServiceProvider::class); +// $app->register(App\Providers\EventServiceProvider::class); +$app->register(Kreait\Laravel\Firebase\ServiceProvider::class); + +/* +|-------------------------------------------------------------------------- +| Load The Application Routes +|-------------------------------------------------------------------------- +| +| Next we will include the routes file so that they can all be added to +| the application. This will provide all of the URLs the application +| can respond to, as well as the controllers that may handle them. +| +*/ + +$app->router->group([ + 'namespace' => 'App\Http\Controllers', +], function ($router) { + require __DIR__.'/../routes/web.php'; +}); + +return $app; diff --git a/database/migrations/2020_10_31_230717_create_profiles_table.php b/database/migrations/2020_10_31_230717_create_profiles_table.php new file mode 100644 index 0000000..e1a3a56 --- /dev/null +++ b/database/migrations/2020_10_31_230717_create_profiles_table.php @@ -0,0 +1,24 @@ +bigIncrements('id'); + $table->string('display_name')->default('No Name'); + $table->mediumText('picture'); + $table->string('uid'); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('profiles'); + } +} diff --git a/routes/web.php b/routes/web.php index 66157fc..2ccf3ff 100644 --- a/routes/web.php +++ b/routes/web.php @@ -17,3 +17,5 @@ $router->get('/', function () use ($router) { }); $router->get('/formats', ['uses' => 'FormatsController@index', 'as' => 'formats.list']); + +$router->get('/profiles', ['uses' => 'ProfilesController@current', 'as' => 'profiles.current', 'middleware' => 'auth']);