-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract common logic to ExternalAuthController
- Loading branch information
1 parent
7355996
commit 7eaa8f2
Showing
3 changed files
with
105 additions
and
187 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
app/Http/Controllers/ExternalAuth/ExternalAuthController.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,99 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\ExternalAuth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use App\Notifications\AccountCreatedViaProvider; | ||
use App\Providers\RouteServiceProvider; | ||
use App\View\Components\Notification; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\Lang; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Str; | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Throwable; | ||
|
||
abstract class ExternalAuthController extends Controller | ||
{ | ||
/** | ||
* Redirect user to the provider's authorization page. | ||
*/ | ||
public function redirect(): RedirectResponse | ||
{ | ||
if (!config(Str::lower('services.' . $this->getProviderName() . '.enabled'))) { | ||
Notification::push( | ||
Lang::get('notifications.in-app.external-auth-failed.title'), | ||
Lang::get('notifications.in-app.external-auth-failed.description', ['provider' => $this->getProviderName()]), | ||
Notification::DANGER, | ||
); | ||
|
||
return redirect()->back(); | ||
} | ||
|
||
return Socialite::driver($this->getProviderName())->redirect(); | ||
} | ||
|
||
/** | ||
* Process data retrieved from the provider. | ||
*/ | ||
public function callback(): RedirectResponse | ||
{ | ||
$externalUser = null; | ||
|
||
try { | ||
$externalUser = Socialite::driver($this->getProviderName())->user(); | ||
|
||
$user = User::where('email', '=', $externalUser->getEmail())->first(); | ||
|
||
if ($user === null) { | ||
$password = Str::password(16); | ||
|
||
$user = new User([ | ||
'username' => $externalUser->getNickname() ?? Str::before($externalUser->getEmail(), '@'), | ||
'email' => $externalUser->getEmail(), | ||
'password' => $password, | ||
]); | ||
|
||
$user->email_verified_at = Carbon::now(); | ||
$user->save(); | ||
|
||
$user->notify(new AccountCreatedViaProvider($this->getProviderName(), $user->email, $password)); | ||
} | ||
|
||
Auth::login($user, true); | ||
|
||
$notificationType = $user->wasRecentlyCreated ? 'registered' : 'logged-in'; | ||
|
||
Notification::push( | ||
Lang::get('notifications.in-app.' . $notificationType . '.title'), | ||
Lang::get('notifications.in-app.' . $notificationType . '.description', ['username' => $user->username]), | ||
$notificationType === 'registered' ? Notification::SUCCESS : Notification::INFO, | ||
); | ||
|
||
return redirect(RouteServiceProvider::HOME); | ||
} catch (Throwable $t) { | ||
Notification::push( | ||
Lang::get('notifications.in-app.external-auth-failed.title'), | ||
Lang::get('notifications.in-app.external-auth-failed.description', ['provider' => $this->getProviderName()]), | ||
Notification::DANGER, | ||
); | ||
|
||
Log::info(class_basename($this) . ': Authentication failed for ' . $this->getProviderName() . ' user ' . $externalUser?->getEmail() ?? request()->ip(), [ | ||
'code' => $t->getCode(), | ||
'message' => $t->getMessage(), | ||
'file' => $t->getFile(), | ||
'line' => $t->getLine(), | ||
]); | ||
} | ||
|
||
return redirect('/'); | ||
} | ||
|
||
/** | ||
* Get name of the Socialite provider. | ||
*/ | ||
abstract protected function getProviderName(): string; | ||
} |
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