toby/app/Infrastructure/Http/Controllers/GoogleController.php
2022-03-21 13:41:08 +01:00

47 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;
use Laravel\Socialite\SocialiteManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Toby\Eloquent\Models\User;
class GoogleController extends Controller
{
public function redirect(SocialiteManager $socialiteManager): RedirectResponse
{
return $socialiteManager->driver("google")->redirect();
}
public function callback(AuthFactory $auth, SocialiteManager $socialiteManager, Hasher $hash): 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."),
]);
}
$user->update([
"password" => $hash->make(Str::random(40)),
]);
$auth->guard()->login($user, true);
return redirect()->route("dashboard");
}
}