Skip to content

Commit

Permalink
Merge pull request #186 from ECFMP/socialite-invalid-state
Browse files Browse the repository at this point in the history
feat: notify user if login fails for whatever reason
  • Loading branch information
AndyTWF authored Jul 23, 2022
2 parents 92f2d29 + 623312a commit 3cbc774
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
39 changes: 23 additions & 16 deletions app/Http/Controllers/Auth/VatsimConnectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@
use App\Enums\RoleKey;
use App\Models\Role;
use App\Models\User;
use Exception;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

class VatsimConnectController
{
public function callback()
{
/** @var \SocialiteProviders\Manager\OAuth2\User $vatsimUser */
$vatsimUser = Socialite::driver('vatsimconnect')->user();
try {
/** @var \SocialiteProviders\Manager\OAuth2\User $vatsimUser */
$vatsimUser = Socialite::driver('vatsimconnect')->user();

$user = User::firstWhere('id', $vatsimUser->id);
if (!$user) {
$user = new User([
'id' => $vatsimUser->getId(),
'role_id' => Role::firstWhere('key', RoleKey::USER)->id
]);
}
$user = User::firstWhere('id', $vatsimUser->id);
if (!$user) {
$user = new User([
'id' => $vatsimUser->getId(),
'role_id' => Role::firstWhere('key', RoleKey::USER)->id
]);
}

$user->name = $vatsimUser->name;
$user->token = $vatsimUser->token;
$user->refresh_token = $vatsimUser->refreshToken;
$user->refresh_token_expires_at = now()->addSeconds($vatsimUser->expiresIn);
$user->saveQuietly();
$user->name = $vatsimUser->name;
$user->token = $vatsimUser->token;
$user->refresh_token = $vatsimUser->refreshToken;
$user->refresh_token_expires_at = now()->addSeconds($vatsimUser->expiresIn);
$user->saveQuietly();

Auth::login($user);
Auth::login($user);

return to_route('filament.pages.dashboard');
return to_route('filament.pages.dashboard');
} catch (Exception $e) {
Filament::notify('danger', __('Something went wrong, please try again'));
return to_route('filament.auth.login');
}
}
}
48 changes: 25 additions & 23 deletions resources/views/login.blade.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<div @class([
'flex items-center justify-center min-h-screen filament-login-page bg-gray-100 text-gray-900',
'dark:bg-gray-900 dark:text-white' => config('filament.dark_mode'),
])>
<div class="w-screen max-w-md px-6 -mt-16 space-y-8 md:mt-0 md:px-2">
<form wire:submit.prevent="authenticate" @class([
'p-8 space-y-8 bg-white/50 backdrop-blur-xl border border-gray-200 shadow-2xl rounded-2xl relative',
'dark:bg-gray-900/50 dark:border-gray-700' => config('filament.dark_mode'),
])>
<div class="flex justify-center w-full">
<x-filament::brand/>
</div>

<h2 class="text-2xl font-bold tracking-tight text-center">
{{ __('filament::login.heading') }}
</h2>

<x-filament::button type="submit" form="authenticate" class="w-full">
Login via VATSIM Connect
</x-filament::button>
</form>
</div>
</div>
<div @class([
'flex items-center justify-center min-h-screen filament-login-page bg-gray-100 text-gray-900',
'dark:bg-gray-900 dark:text-white' => config('filament.dark_mode'),
])>
<div class="w-screen max-w-md px-6 -mt-16 space-y-8 md:mt-0 md:px-2">
<form wire:submit.prevent="authenticate" @class([
'p-8 space-y-8 bg-white/50 backdrop-blur-xl border border-gray-200 shadow-2xl rounded-2xl relative',
'dark:bg-gray-900/50 dark:border-gray-700' => config('filament.dark_mode'),
])>
<div class="flex justify-center w-full">
<x-filament::brand />
</div>

<h2 class="text-2xl font-bold tracking-tight text-center">
{{ __('filament::login.heading') }}
</h2>

<x-filament::button type="submit" form="authenticate" class="w-full">
Login via VATSIM Connect
</x-filament::button>
</form>
</div>

<x-filament::notification-manager />
</div>

0 comments on commit 3cbc774

Please sign in to comment.