Skip to content

Commit

Permalink
Merge pull request #92 from P3D-Legacy/twitter-account-association
Browse files Browse the repository at this point in the history
Twitter account association
  • Loading branch information
dsbilling authored Jan 3, 2022
2 parents df978f4 + 28168bc commit 4e9f77c
Show file tree
Hide file tree
Showing 16 changed files with 498 additions and 4 deletions.
10 changes: 9 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ DISCORD_TOKEN=null
DISCORD_SERVER_ID=null
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 @@ -76,4 +80,8 @@ LOGIN_AUTH_BLOG_POST=null

SCRIBE_AUTH_KEY=null

MAIL_TO_ADDRESS="${SUPER_ADMIN_EMAIL}"
MAIL_TO_ADDRESS="${SUPER_ADMIN_EMAIL}"

TWITTER_CLIENT_ID=null
TWITTER_CLIENT_SECRET=null
TWITTER_REDIRECT_URI="/login/twitter/callback"
29 changes: 29 additions & 0 deletions app/Achievements/User/AssociatedTwitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace App\Achievements\User;

use Assada\Achievements\Achievement;

/**
* Class Registered
*
* @package App\Achievements\User
*/
class AssociatedTwitter extends Achievement
{
/*
* The achievement name
*/
public $name = 'AssociatedTwitter';

/*
* A small description for the achievement
*/
public $description = 'User associated their account with a Twitter.';

/*
* The amount of "points" this user need to obtain in order to complete this achievement
*/
public $points = 1;
}
2 changes: 0 additions & 2 deletions app/Http/Controllers/Auth/DiscordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Models\DiscordAccount;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Actions\Fortify\CreateNewUser;
use Laravel\Socialite\Facades\Socialite;
use GuzzleHttp\Exception\ClientException;
use App\Achievements\User\AssociatedDiscord;
Expand Down
81 changes: 81 additions & 0 deletions app/Http/Controllers/Auth/TwitterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Models\TwitterAccount;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use GuzzleHttp\Exception\ClientException;
use App\Achievements\User\AssociatedTwitter;
use Laravel\Socialite\Two\InvalidStateException;

class TwitterController extends Controller
{
/**
* Redirect the user to the Twitter authentication page.
*
* @return \Illuminate\Http\Response
*/
public function redirectToProvider()
{
return Socialite::driver('twitter')->redirect();
}

/**
* Obtain the user information from Twitter.
*
* @return \Illuminate\Http\Response
*/
public function handleProviderCallback()
{
try {

$twitterUser = Socialite::driver('twitter')->user();

if ($twitterUser->user['suspended']) {
return redirect()->route('login')->withError('Twitter user is suspended.');
}

$userProfile = [
'id' => $twitterUser->id,
'username' => $twitterUser->nickname,
'name' => $twitterUser->name,
'email' => $twitterUser->email,
'avatar' => $twitterUser->avatar,
];

// Check if user exists with email
$twitterAccount = TwitterAccount::where('id', $twitterUser->id)->first();
if (!$twitterAccount && auth()->guest()) {
return redirect()->route('login')->withError('Twitter account association not found with any P3D account.');
}

$user = $twitterAccount ? $twitterAccount->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 twitter account
$user = auth()->user();
$userProfile['user_id'] = $user->id;
$userProfile['verified_at'] = now();
TwitterAccount::create($userProfile);
$user->unlock(new AssociatedTwitter());
return redirect()->route('profile.show');

} catch (InvalidStateException $e) {
return redirect()->route('home')->withError('Something went wrong with Twitter login. Please try again.');
} catch (ClientException $e) {
return redirect()->route('home')->withError('Something went wrong with Twitter login. Please try again.');
}


return redirect()->route('dashboard');
}
}
53 changes: 53 additions & 0 deletions app/Http/Livewire/Profile/TwitterAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Http\Livewire\Profile;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;

class TwitterAccount extends Component
{
public $username;
public $name;
public $avatar;

public function mount() {
$user = Auth::user();
$this->username = ($user->twitter ? $user->twitter->username : null);
$this->name = ($user->twitter ? $user->twitter->name : null);
$this->avatar = ($user->twitter ? $user->twitter->avatar : null);
$this->updated_at = ($user->twitter ? $user->twitter->updated_at->diffForHumans() : null);
$this->verified_at = ($user->twitter ? $user->twitter->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->twitter) {
$user->twitter->delete();
$this->username = null;
$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.twitter-account');
}
}
85 changes: 85 additions & 0 deletions app/Models/TwitterAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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 TwitterAccount 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',
'username',
'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 @@ -106,4 +106,12 @@ public function forum()
{
return $this->hasOne(ForumAccount::class);
}

/**
* Get the twitter account associated with the user.
*/
public function twitter()
{
return $this->hasOne(TwitterAccount::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/twitter": "^4.1",
"spatie/cpu-load-health-check": "^1.0",
"spatie/laravel-activitylog": "^3.16",
"spatie/laravel-cookie-consent": "^2.12",
Expand Down
Loading

0 comments on commit 4e9f77c

Please sign in to comment.