-
Notifications
You must be signed in to change notification settings - Fork 16
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 #92 from P3D-Legacy/twitter-account-association
Twitter account association
- Loading branch information
Showing
16 changed files
with
498 additions
and
4 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,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; | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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
Oops, something went wrong.