I created a new middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
class CheckSession
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if(Auth::guard($guard)->check() && !empty(session('confirmation'))) {
return new Response('This session is not unlocked yet! <a href="'.route('auth.confirm',session('confirmation')).'">Unlock</a>',402);
}
return $next($request);
}
}
a custom SessionHandler
<?php
namespace App\Extensions;
use Illuminate\Session\DatabaseSessionHandler;
class CustomSessionHandler extends DatabaseSessionHandler
{
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
$session = (object) $this->getQuery()->find($sessionId);
if ($this->expired($session)) {
$this->exists = true;
return;
}
if (isset($session->payload)) {
$this->exists = true;
$s = unserialize(base64_decode($session->payload));
$s['confirmation'] = $session->confirmation;
return serialize($s);
}
}
}
added
$table->string('confirmation')->default('');
to the session migration, registered the custom session handler in my appServiceProvider
<?php
namespace App\Providers;
use App\Extensions\CustomSessionHandler;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Session::extend('mySession', function($app) {
$table = $app['config']['session.table'];
$lifetime = $app['config']['session.lifetime'];
$connection = $app['config']['session.connection'];
return new CustomSessionHandler($app['db']->connection($connection), $table, $lifetime, $app);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
and used it in session.php / .env and added a route to confirm with the token.
But now I have the question whats the best way to create a random confirmation code when a user loggs in?