mirror of
https://github.com/kevin-DL/commander_league_api.git
synced 2026-01-11 18:44:32 +00:00
19
app/Http/Controllers/ProfilesController.php
Normal file
19
app/Http/Controllers/ProfilesController.php
Normal 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'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
23
app/Profile.php
Normal file
23
app/Profile.php
Normal 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'
|
||||
];
|
||||
}
|
||||
@@ -99,7 +99,7 @@ $app->routeMiddleware([
|
||||
*/
|
||||
|
||||
// $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(Kreait\Laravel\Firebase\ServiceProvider::class);
|
||||
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user