toby/app/Http/Controllers/GoogleController.php
Adrian Hopek 0869395aab
#2 - authentication via google (#16)
* #2 - wip

* #2 - wip

* #2 - ui fixes to login page

* #2 - fix

* #2 - fix

* #2 - add title to login page

* Apply suggestions from code review

Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>

* #2 - cr fix

* #2 - cr fix

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
Co-authored-by: Krzysztof Rewak <krzysztof.rewak@blumilk.pl>
2022-01-13 14:19:11 +01:00

42 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Http\Controllers;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Socialite\SocialiteManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Toby\Models\User;
class GoogleController extends Controller
{
public function redirect(SocialiteManager $socialiteManager): RedirectResponse
{
return $socialiteManager->driver("google")->redirect();
}
public function callback(AuthFactory $auth, SocialiteManager $socialiteManager): RedirectResponse
{
$socialUser = $socialiteManager->driver("google")->user();
try {
/** @var User $user */
$user = User::query()
->where("email", $socialUser->getEmail())
->firstOrFail();
} catch (ModelNotFoundException) {
return redirect()
->route("login")
->withErrors([
"oauth" => __("User does not exist."),
]);
}
$auth->guard()->login($user, true);
return redirect()->route("dashboard");
}
}