Skip to content

Fixes for Laravel Session, reset between requests #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 52 additions & 16 deletions Bootstraps/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,59 @@ public function getApplication()
}

$kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');

$this->app->afterResolving('auth', function($auth) {
$auth->extend('session', function($app, $name, $config) {
$provider = $app['auth']->createUserProvider($config['provider']);
$guard = new \PHPPM\Laravel\SessionGuard($name, $provider, $app['session.store'], null, $app);
$guard->setCookieJar($app['cookie']);
$guard->setDispatcher($app['events']);
$guard->setRequest($app->refresh('request', $guard, 'setRequest'));

return $guard;
});
});

$app = $this->app;
$this->app->extend('session.store', function() use ($app) {
$manager = $app['session'];
return $manager->driver();
});



return $kernel;
}

/**
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function preHandle($app)
{
//reset const LARAVEL_START, to get the correct timing
}

/**
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function postHandle($app)
{
//reset debugbar if available
}

/**
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function preHandle($app)
{
//reset const LARAVEL_START, to get the correct timing
}

/**
* @param \Illuminate\Contracts\Foundation\Application $app
*/
public function postHandle($app)
{
//reset debugbar if available

$this->resetProvider('\Illuminate\Cookie\CookieServiceProvider');
$this->resetProvider('\Illuminate\Session\SessionServiceProvider');
}

/**
* @param string $providerName
*/
protected function resetProvider($providerName)
{
if (!$this->app->getProvider($providerName))
{
return;
}

$this->app->register($providerName, [], true);
}
}
5 changes: 5 additions & 0 deletions Bridges/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse as SymfonyStreamedResponse;
use Symfony\Component\HttpKernel\TerminableInterface;
use Illuminate\Contracts\Http\Kernel;

class HttpKernel implements BridgeInterface
{
Expand Down Expand Up @@ -110,6 +111,10 @@ public function onRequest(ReactRequest $request, HttpResponse $response)
if ($this->application instanceof TerminableInterface) {
$this->application->terminate($syRequest, $syResponse);
}

if ($this->application instanceof Kernel) {
$this->application->terminate($syRequest, $syResponse);
}

if ($this->bootstrap instanceof HooksInterface) {
$this->bootstrap->postHandle($this->application);
Expand Down
73 changes: 73 additions & 0 deletions Laravel/SessionGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace PHPPM\Laravel;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Foundation\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionGuard extends \Illuminate\Auth\SessionGuard
{

/**
* App instance
*
* @var mixed|\Illuminate\Foundation\Application $app
*/
protected $app;

/**
* Create a new authentication guard.
*
* @param string $name
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Symfony\Component\HttpFoundation\Request $request
* @param mixed|\Illuminate\Foundation\Application $app
* @return void
*/
public function __construct($name,
UserProvider $provider,
SessionInterface $session,
Request $request = null,
Application $app)
{
$this->name = $name;
$this->session = $session;
$this->request = $request;
$this->provider = $provider;
$this->app = $app;
}

/**
* Set the current request instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return $this
*/
public function setRequest(Request $request)
{
// reset the current state
$this->reset();

// retrieve a new session from the app
$this->session = $this->app->make('session');

return parent::setRequest($request);
}

/**
* Reset the state of current class instance.
*
* @return void
*/
protected function reset()
{
$this->user = null;
$this->lastAttempted = null;
$this->viaRemember = false;
$this->loggedOut = false;
$this->tokenRetrievalAttempted = false;
}
}