#84 - fix remembering user

This commit is contained in:
Adrian Hopek 2022-03-21 13:38:28 +01:00
parent a0e60a3160
commit b4e20b79fb
2 changed files with 27 additions and 1 deletions

View File

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

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class() extends Migration {
public function up(): void
{
Schema::table("users", function (Blueprint $table): void {
$table->string("password")->nullable();
});
}
public function down(): void
{
Schema::table("users", function (Blueprint $table): void {
$table->dropColumn("password");
});
}
};