Merge pull request #1 from kevin-DL/chore/user_model

Profile
This commit is contained in:
2020-11-01 08:57:29 +00:00
committed by GitHub
7 changed files with 235 additions and 166 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function current(Request $request)
{
$params = $request->query();
$user = $params['user'];
return response()->json([
'id' => $user['id'],
'display_name' => $user['display_name'],
'picture' => $user['picture'],
]);
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Http\Middleware; namespace App\Http\Middleware;
use App\Profile;
use App\User; use App\User;
use Closure; use Closure;
use Kreait\Laravel\Firebase\Facades\FirebaseAuth; use Kreait\Laravel\Firebase\Facades\FirebaseAuth;
@@ -44,23 +45,23 @@ class FirebaseMiddleware
try { try {
$decoded = FirebaseAuth::verifyIdToken($token); $decoded = FirebaseAuth::verifyIdToken($token);
$uid = $decoded->getClaim('sub'); $uid = $decoded->getClaim('sub');
$user = User::where('provider_id', $uid)->get(); $profile = Profile::where('uid', $uid)->get();
$user = $user[0] ?? null; $profile = $profile[0] ?? null;
if (!$user) { if (!$profile) {
/** @var \Kreait\Firebase\Auth\UserRecord $data */ /** @var \Kreait\Firebase\Auth\UserRecord $data */
$data = FirebaseAuth::getUser($uid); $data = FirebaseAuth::getUser($uid);
$user = new User(); $profile = new Profile();
$user->provider_id = $data->uid; $profile->uid = $data->uid;
$user->email = $data->email; $profile->display_name = $data->displayName ?? 'No Name User';
$user->name = $data->displayName ?? 'No Name User'; $profile->picture = $data->photoUrl ?? '';
$user->image = $data->photoUrl ?? ''; $profile->save();
$user->save();
} }
return $user; return $profile;
} catch (\Exception $e) { } catch (\Exception $e) {
\Log::error($e->getMessage());
return null; return null;
}; }
} }
} }

23
app/Profile.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Profile
* @package App
*
* @property integer id
* @property string display_name
* @property string uid
* @property string picture
* @property \DateTime created_at
* @property \DateTime updated_at
*/
class Profile extends Model
{
protected $fillable = [
'display_name', 'uid', 'picture'
];
}

View File

@@ -1,32 +1,32 @@
<?php <?php
namespace App; namespace App;
use Illuminate\Auth\Authenticatable; use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable; use Laravel\Lumen\Auth\Authorizable;
class User extends Model implements AuthenticatableContract, AuthorizableContract class User extends Model implements AuthenticatableContract, AuthorizableContract
{ {
use Authenticatable, Authorizable; use Authenticatable, Authorizable;
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *
* @var array * @var array
*/ */
protected $fillable = [ protected $fillable = [
'name', 'email', 'name', 'email',
]; ];
/** /**
* The attributes excluded from the model's JSON form. * The attributes excluded from the model's JSON form.
* *
* @var array * @var array
*/ */
protected $hidden = [ protected $hidden = [
'password', 'password',
]; ];
} }

View File

@@ -1,123 +1,123 @@
<?php <?php
require_once __DIR__.'/../vendor/autoload.php'; require_once __DIR__.'/../vendor/autoload.php';
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables( (new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__) dirname(__DIR__)
))->bootstrap(); ))->bootstrap();
date_default_timezone_set(env('APP_TIMEZONE', 'UTC')); date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Create The Application | Create The Application
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Here we will load the environment and create the application instance | Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this | that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework. | application as an "IoC" container and router for this framework.
| |
*/ */
$app = new Laravel\Lumen\Application( $app = new Laravel\Lumen\Application(
dirname(__DIR__) dirname(__DIR__)
); );
$app->withFacades(); $app->withFacades();
$app->withEloquent(); $app->withEloquent();
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Register Container Bindings | Register Container Bindings
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Now we will register a few bindings in the service container. We will | Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add | register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file. | your own bindings here if you like or you can make another file.
| |
*/ */
$app->singleton( $app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class, Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class App\Exceptions\Handler::class
); );
$app->singleton( $app->singleton(
Illuminate\Contracts\Console\Kernel::class, Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class App\Console\Kernel::class
); );
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Register Config Files | Register Config Files
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Now we will register the "app" configuration file. If the file exists in | Now we will register the "app" configuration file. If the file exists in
| your configuration directory it will be loaded; otherwise, we'll load | your configuration directory it will be loaded; otherwise, we'll load
| the default version. You may register other files below as needed. | the default version. You may register other files below as needed.
| |
*/ */
$app->configure('app'); $app->configure('app');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Register Middleware | Register Middleware
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Next, we will register the middleware with the application. These can | Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a | be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes. | route or middleware that'll be assigned to some specific routes.
| |
*/ */
// $app->middleware([ // $app->middleware([
// App\Http\Middleware\ExampleMiddleware::class // App\Http\Middleware\ExampleMiddleware::class
// ]); // ]);
// $app->routeMiddleware([ // $app->routeMiddleware([
// 'auth' => App\Http\Middleware\Authenticate::class, // 'auth' => App\Http\Middleware\Authenticate::class,
// ]); // ]);
$app->middleware([ $app->middleware([
\App\Http\Middleware\CorsMiddleware::class \App\Http\Middleware\CorsMiddleware::class
]); ]);
$app->routeMiddleware([ $app->routeMiddleware([
'auth' => App\Http\Middleware\FirebaseMiddleware::class, 'auth' => App\Http\Middleware\FirebaseMiddleware::class,
]); ]);
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Register Service Providers | Register Service Providers
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Here we will register all of the application's service providers which | Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are | are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line. | totally optional, so you are not required to uncomment this line.
| |
*/ */
// $app->register(App\Providers\AppServiceProvider::class); // $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class); $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class); // $app->register(App\Providers\EventServiceProvider::class);
$app->register(Kreait\Laravel\Firebase\ServiceProvider::class); $app->register(Kreait\Laravel\Firebase\ServiceProvider::class);
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Load The Application Routes | Load The Application Routes
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Next we will include the routes file so that they can all be added to | 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 | the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them. | can respond to, as well as the controllers that may handle them.
| |
*/ */
$app->router->group([ $app->router->group([
'namespace' => 'App\Http\Controllers', 'namespace' => 'App\Http\Controllers',
], function ($router) { ], function ($router) {
require __DIR__.'/../routes/web.php'; require __DIR__.'/../routes/web.php';
}); });
return $app; return $app;

View File

@@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProfilesTable extends Migration
{
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('display_name')->default('No Name');
$table->mediumText('picture');
$table->string('uid');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('profiles');
}
}

View File

@@ -17,3 +17,5 @@ $router->get('/', function () use ($router) {
}); });
$router->get('/formats', ['uses' => 'FormatsController@index', 'as' => 'formats.list']); $router->get('/formats', ['uses' => 'FormatsController@index', 'as' => 'formats.list']);
$router->get('/profiles', ['uses' => 'ProfilesController@current', 'as' => 'profiles.current', 'middleware' => 'auth']);