You've already forked Atomcms-edit
Initial commit
This commit is contained in:
+140
@@ -0,0 +1,140 @@
|
||||
<?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\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)
|
||||
{
|
||||
$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)
|
||||
{
|
||||
$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()
|
||||
{
|
||||
return redirect()->intended('/');
|
||||
}
|
||||
|
||||
public function unlink(Request $request, string $provider)
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
$user->socialAccounts()->where('provider', $provider)->delete();
|
||||
|
||||
return back()->with('success', ucfirst($provider) . ' account has been unlinked.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user