mirror of
https://github.com/kevin-DL/commander_league_api.git
synced 2026-01-18 21:35:08 +00:00
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
This commit is contained in:
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'
|
||||
];
|
||||
}
|
||||
64
app/User.php
64
app/User.php
@@ -1,32 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
||||
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Laravel\Lumen\Auth\Authorizable;
|
||||
|
||||
class User extends Model implements AuthenticatableContract, AuthorizableContract
|
||||
{
|
||||
use Authenticatable, Authorizable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'email',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes excluded from the model's JSON form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
];
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
||||
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Laravel\Lumen\Auth\Authorizable;
|
||||
|
||||
class User extends Model implements AuthenticatableContract, AuthorizableContract
|
||||
{
|
||||
use Authenticatable, Authorizable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'email',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes excluded from the model's JSON form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user