Skip to content

Commit

Permalink
Merge pull request #93 from P3D-Legacy/facebook-account-association
Browse files Browse the repository at this point in the history
Facebook account association
  • Loading branch information
dsbilling authored Jan 3, 2022
2 parents e8dcd07 + 70fcc3c commit 75f21a1
Show file tree
Hide file tree
Showing 14 changed files with 396 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ DISCORD_INVITE_URL="https://discordapp.com/invite/EUhwdrq"

DISCORD_CLIENT_ID=null
DISCORD_CLIENT_SECRET=null
DISCORD_REDIRECT_URI="/login/discord/callback"

SENTRY_LARAVEL_DSN=null
SENTRY_TRACES_SAMPLE_RATE=null
Expand All @@ -84,4 +83,6 @@ MAIL_TO_ADDRESS="${SUPER_ADMIN_EMAIL}"

TWITTER_CLIENT_ID=null
TWITTER_CLIENT_SECRET=null
TWITTER_REDIRECT_URI="/login/twitter/callback"

FACEBOOK_CLIENT_ID=null
FACEBOOK_CLIENT_SECRET=null
24 changes: 24 additions & 0 deletions app/Achievements/User/AssociatedFacebook.php
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 = '';
}
76 changes: 76 additions & 0 deletions app/Http/Controllers/Auth/FacebookController.php
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');
}
}
50 changes: 50 additions & 0 deletions app/Http/Livewire/Profile/FacebookAccount.php
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');
}
}
84 changes: 84 additions & 0 deletions app/Models/FacebookAccount.php
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');
}
}
8 changes: 8 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,12 @@ public function twitter()
{
return $this->hasOne(TwitterAccount::class);
}

/**
* Get the facebook account associated with the user.
*/
public function facebook()
{
return $this->hasOne(FacebookAccount::class);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"power-components/livewire-powergrid": "^1.5",
"restcord/restcord": "dev-develop",
"sentry/sentry-laravel": "^2.5",
"socialiteproviders/facebook": "^4.1",
"socialiteproviders/twitter": "^4.1",
"spatie/cpu-load-health-check": "^1.0",
"spatie/laravel-activitylog": "^3.16",
Expand Down
43 changes: 42 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
'redirect' => env('TWITTER_REDIRECT_URI', '/login/twitter/callback'),
],

'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT_URI', '/login/facebook/callback')
],

];
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');
}
}
Loading

0 comments on commit 75f21a1

Please sign in to comment.