44 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Http\Controllers;
 | 
						|
 | 
						|
use App\Models\User;
 | 
						|
use Illuminate\Support\Facades\Auth;
 | 
						|
use Symfony\Component\HttpKernel\Exception\HttpException;
 | 
						|
 | 
						|
class OauthController extends Controller
 | 
						|
{
 | 
						|
    public function redirect(string $provider)
 | 
						|
    {
 | 
						|
        $socialite_provider = get_socialite_provider($provider);
 | 
						|
 | 
						|
        return $socialite_provider->redirect();
 | 
						|
    }
 | 
						|
 | 
						|
    public function callback(string $provider)
 | 
						|
    {
 | 
						|
        try {
 | 
						|
            $oauthUser = get_socialite_provider($provider)->user();
 | 
						|
            $user = User::whereEmail($oauthUser->email)->first();
 | 
						|
            if (! $user) {
 | 
						|
                $settings = instanceSettings();
 | 
						|
                if (! $settings->is_registration_enabled) {
 | 
						|
                    abort(403, 'Registration is disabled');
 | 
						|
                }
 | 
						|
 | 
						|
                $user = User::create([
 | 
						|
                    'name' => $oauthUser->name,
 | 
						|
                    'email' => $oauthUser->email,
 | 
						|
                ]);
 | 
						|
            }
 | 
						|
            Auth::login($user);
 | 
						|
 | 
						|
            return redirect('/');
 | 
						|
        } catch (\Exception $e) {
 | 
						|
            $errorCode = $e instanceof HttpException ? 'auth.failed' : 'auth.failed.callback';
 | 
						|
 | 
						|
            return redirect()->route('login')->withErrors([__($errorCode)]);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |