You've already forked Atomcms-edit
05fc7b04bc
Added proper return types (View, RedirectResponse, JsonResponse, Collection) to 40+ controller methods across 16 controllers. Also added missing imports for Illuminate response types and tightened parameter types (e.g. InstallationController::showStep now uses int instead of mixed).
142 lines
4.2 KiB
PHP
Executable File
142 lines
4.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\SocialAccount;
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Events\Registered;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
class SocialAuthController extends Controller
|
|
{
|
|
public function redirect(string $provider): RedirectResponse
|
|
{
|
|
$enabled = $this->isProviderEnabled($provider);
|
|
|
|
if (! $enabled) {
|
|
return redirect()->route('login')
|
|
->with('error', ucfirst($provider) . ' login is not enabled.');
|
|
}
|
|
|
|
return Socialite::driver($provider)->redirect();
|
|
}
|
|
|
|
public function callback(string $provider): RedirectResponse
|
|
{
|
|
$enabled = $this->isProviderEnabled($provider);
|
|
|
|
if (! $enabled) {
|
|
return redirect()->route('login')
|
|
->with('error', ucfirst($provider) . ' login is not enabled.');
|
|
}
|
|
|
|
try {
|
|
$socialUser = Socialite::driver($provider)->user();
|
|
} catch (\Exception $e) {
|
|
return redirect()->route('login')
|
|
->with('error', 'Failed to login with ' . ucfirst($provider) . '. Please try again.');
|
|
}
|
|
|
|
$account = SocialAccount::findByProvider($provider, $socialUser->getId());
|
|
|
|
if ($account) {
|
|
Auth::login($account->user);
|
|
|
|
return $this->redirectAfterLogin();
|
|
}
|
|
|
|
$email = $socialUser->getEmail();
|
|
|
|
if (! $email) {
|
|
return redirect()->route('login')
|
|
->with('error', 'Your ' . ucfirst($provider) . ' account does not have an email address linked.');
|
|
}
|
|
|
|
$existingUser = User::where('mail', $email)->first();
|
|
|
|
if ($existingUser && $existingUser->hasSocialAccount($provider)) {
|
|
return redirect()->route('login')
|
|
->with('error', 'This ' . ucfirst($provider) . ' account is already linked to another user.');
|
|
}
|
|
|
|
if ($existingUser && ! $existingUser->hasSocialAccount($provider)) {
|
|
$existingUser->socialAccounts()->create([
|
|
'provider' => $provider,
|
|
'provider_id' => $socialUser->getId(),
|
|
'avatar' => $socialUser->getAvatar(),
|
|
]);
|
|
|
|
Auth::login($existingUser);
|
|
|
|
return $this->redirectAfterLogin();
|
|
}
|
|
|
|
$username = $this->generateUsername($socialUser->getName() ?? $provider . '_user');
|
|
|
|
$user = User::create([
|
|
'username' => $username,
|
|
'mail' => $email,
|
|
'password' => Hash::make(Str::random(16)),
|
|
'account_created' => time(),
|
|
'last_login' => time(),
|
|
'motto' => 'New player',
|
|
'look' => 'hr-100-61.hd-180-1.ch-210-66',
|
|
'ip_register' => request()->ip(),
|
|
'ip_current' => request()->ip(),
|
|
]);
|
|
|
|
$user->socialAccounts()->create([
|
|
'provider' => $provider,
|
|
'provider_id' => $socialUser->getId(),
|
|
'avatar' => $socialUser->getAvatar(),
|
|
]);
|
|
|
|
event(new Registered($user));
|
|
|
|
Auth::login($user);
|
|
|
|
return $this->redirectAfterLogin();
|
|
}
|
|
|
|
protected function isProviderEnabled(string $provider): bool
|
|
{
|
|
$key = 'social_login_' . $provider . '_enabled';
|
|
|
|
return setting($key) === '1';
|
|
}
|
|
|
|
protected function generateUsername(string $name): string
|
|
{
|
|
$baseUsername = Str::slug($name, '');
|
|
$username = $baseUsername;
|
|
$counter = 1;
|
|
|
|
while (User::where('username', $username)->exists()) {
|
|
$username = $baseUsername . $counter;
|
|
$counter++;
|
|
}
|
|
|
|
return $username;
|
|
}
|
|
|
|
protected function redirectAfterLogin(): RedirectResponse
|
|
{
|
|
return redirect()->intended('/');
|
|
}
|
|
|
|
public function unlink(Request $request, string $provider): RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
$user->socialAccounts()->where('provider', $provider)->delete();
|
|
|
|
return back()->with('success', ucfirst($provider) . ' account has been unlinked.');
|
|
}
|
|
}
|