-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from P3D-Legacy/facebook-account-association
Facebook account association
- Loading branch information
Showing
14 changed files
with
396 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace App\Achievements\User; | ||
|
||
use Assada\Achievements\Achievement; | ||
|
||
/** | ||
* Class Registered | ||
* | ||
* @package App\Achievements\User | ||
*/ | ||
class AssociatedFacebook extends Achievement | ||
{ | ||
/* | ||
* The achievement name | ||
*/ | ||
public $name = 'AssociatedFacebook'; | ||
|
||
/* | ||
* A small description for the achievement | ||
*/ | ||
public $description = ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Models\FacebookAccount; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Auth; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use GuzzleHttp\Exception\ClientException; | ||
use App\Achievements\User\AssociatedFacebook; | ||
use Laravel\Socialite\Two\InvalidStateException; | ||
|
||
class FacebookController extends Controller | ||
{ | ||
/** | ||
* Redirect the user to the Facebook authentication page. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function redirectToProvider() | ||
{ | ||
return Socialite::driver('facebook')->redirect(); | ||
} | ||
|
||
/** | ||
* Obtain the user information from Facebook. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handleProviderCallback() | ||
{ | ||
try { | ||
|
||
$facebookUser = Socialite::driver('facebook')->user(); | ||
|
||
$userProfile = [ | ||
'id' => $facebookUser->id, | ||
'name' => $facebookUser->name, | ||
'email' => $facebookUser->email, | ||
'avatar' => $facebookUser->avatar, | ||
]; | ||
|
||
// Check if user exists with email | ||
$facebookAccount = FacebookAccount::where('id', $facebookUser->id)->first(); | ||
if (!$facebookAccount && auth()->guest()) { | ||
return redirect()->route('login')->withError('Facebook account association not found with any P3D account.'); | ||
} | ||
|
||
$user = $facebookAccount ? $facebookAccount->user : null; | ||
if ($user) { | ||
Auth::login($user); | ||
return redirect()->route('dashboard'); | ||
} | ||
|
||
if (auth()->guest() && !$user) { | ||
return redirect()->route('login')->withError('You are not logged in and user was not found.'); | ||
} | ||
|
||
// Create new facebook account | ||
$user = auth()->user(); | ||
$userProfile['user_id'] = $user->id; | ||
$userProfile['verified_at'] = now(); | ||
FacebookAccount::create($userProfile); | ||
$user->unlock(new AssociatedFacebook()); | ||
return redirect()->route('profile.show'); | ||
|
||
} catch (InvalidStateException $e) { | ||
return redirect()->route('home')->withError('Something went wrong with Facebook login. Please try again.'); | ||
} catch (ClientException $e) { | ||
return redirect()->route('home')->withError('Something went wrong with Facebook login. Please try again.'); | ||
} | ||
|
||
|
||
return redirect()->route('dashboard'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Profile; | ||
|
||
use Livewire\Component; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class FacebookAccount extends Component | ||
{ | ||
public $name; | ||
public $avatar; | ||
|
||
public function mount() { | ||
$user = Auth::user(); | ||
$this->name = ($user->facebook ? $user->facebook->name : null); | ||
$this->avatar = ($user->facebook ? $user->facebook->avatar : null); | ||
$this->updated_at = ($user->facebook ? $user->facebook->updated_at->diffForHumans() : null); | ||
$this->verified_at = ($user->facebook ? $user->facebook->verified_at->diffForHumans() : null); | ||
} | ||
|
||
/** | ||
* Update the user's GameJolt Account credentials. | ||
* | ||
* @return void | ||
*/ | ||
public function remove() | ||
{ | ||
$this->resetErrorBag(); | ||
$this->resetValidation(); | ||
|
||
$user = Auth::user(); | ||
|
||
if ($user->facebook) { | ||
$user->facebook->delete(); | ||
$this->name = null; | ||
$this->avatar = null; | ||
$this->updated_at = null; | ||
$this->verified_at = null; | ||
} | ||
|
||
$this->emit('refresh'); | ||
|
||
return; | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire.profile.facebook-account'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid; | ||
|
||
class FacebookAccount extends Model | ||
{ | ||
use HasFactory; | ||
use SoftDeletes; | ||
use Uuid; | ||
|
||
protected $primaryKey = 'uuid'; | ||
|
||
/** | ||
* The "type" of the auto-incrementing ID. | ||
* | ||
* @var string | ||
*/ | ||
protected $keyType = 'string'; | ||
|
||
/** | ||
* Indicates if the IDs are auto-incrementing. | ||
* | ||
* @var bool | ||
*/ | ||
public $incrementing = false; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'id', | ||
'name', | ||
'email', | ||
'avatar', | ||
'verified_at', | ||
'user_id', | ||
]; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'verified_at' => 'datetime', | ||
]; | ||
|
||
/** | ||
* The attributes that should be hidden | ||
* | ||
* @var array | ||
*/ | ||
protected $hidden = [ | ||
'aid', | ||
]; | ||
|
||
public function touchVerify() | ||
{ | ||
$this->verified_at = $this->freshTimestamp(); | ||
return $this->save(); | ||
} | ||
|
||
/** | ||
* Get the user associated with the gamejolt account. | ||
*/ | ||
public function user() | ||
{ | ||
return $this->hasOne(User::class, 'id', 'user_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
database/migrations/2022_01_03_215221_create_facebook_accounts_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateFacebookAccountsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('facebook_accounts', function (Blueprint $table) { | ||
$table->increments('aid'); | ||
$table->uuid('uuid')->unique(); | ||
$table->bigInteger('id')->comment('Facebook ID'); | ||
$table->text('name')->comment('Facebook Name'); | ||
$table->text('email')->comment('Facebook Email'); | ||
$table->text('avatar')->comment('Facebook Avatar URL'); | ||
$table->timestamp('verified_at')->nullable(); | ||
$table->unsignedBigInteger('user_id'); | ||
$table->timestamps(); | ||
$table->softDeletes(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('facebook_accounts'); | ||
} | ||
} |
Oops, something went wrong.