Multi session tables in Laravel
up vote
0
down vote
favorite
i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.
php database laravel session
New contributor
add a comment |
up vote
0
down vote
favorite
i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.
php database laravel session
New contributor
If you have a look atconfig/session.php
, you'll notice'connection' => null
and'table' => 'sessions'
; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
– Tim Lewis
Nov 21 at 19:15
But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.
php database laravel session
New contributor
i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.
php database laravel session
php database laravel session
New contributor
New contributor
New contributor
asked Nov 21 at 18:51
Hacko
11
11
New contributor
New contributor
If you have a look atconfig/session.php
, you'll notice'connection' => null
and'table' => 'sessions'
; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
– Tim Lewis
Nov 21 at 19:15
But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago
add a comment |
If you have a look atconfig/session.php
, you'll notice'connection' => null
and'table' => 'sessions'
; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
– Tim Lewis
Nov 21 at 19:15
But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago
If you have a look at
config/session.php
, you'll notice 'connection' => null
and 'table' => 'sessions'
; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…– Tim Lewis
Nov 21 at 19:15
If you have a look at
config/session.php
, you'll notice 'connection' => null
and 'table' => 'sessions'
; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…– Tim Lewis
Nov 21 at 19:15
But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago
But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Administrator::class => UserPolicy::class,
];
public function boot()
{
$this->registerPolicies();
Auth::extend('admin_session', function ($app, $name, array $config) {
$provider = Auth::createUserProvider($config['provider']);
$guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
}
return $guard;
}
}
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->app->session->extend('admin_database', function ($app) {
$table = 'administrator_sessions';
// This is not workin, because I don't have the user her.
/*if (auth()->user() instanceof Customer) {
$table = 'customer_sessions';
}
else if (auth()->user() instanceof Administrator){
$table = 'administrator_sessions';
}*/
$lifetime = $app['config']['session.lifetime'];
$connection = $app['db']->connection($app['config']['session.connection']);
return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
});
}
}
New contributor
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Administrator::class => UserPolicy::class,
];
public function boot()
{
$this->registerPolicies();
Auth::extend('admin_session', function ($app, $name, array $config) {
$provider = Auth::createUserProvider($config['provider']);
$guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
}
return $guard;
}
}
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->app->session->extend('admin_database', function ($app) {
$table = 'administrator_sessions';
// This is not workin, because I don't have the user her.
/*if (auth()->user() instanceof Customer) {
$table = 'customer_sessions';
}
else if (auth()->user() instanceof Administrator){
$table = 'administrator_sessions';
}*/
$lifetime = $app['config']['session.lifetime'];
$connection = $app['db']->connection($app['config']['session.connection']);
return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
});
}
}
New contributor
add a comment |
up vote
0
down vote
I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Administrator::class => UserPolicy::class,
];
public function boot()
{
$this->registerPolicies();
Auth::extend('admin_session', function ($app, $name, array $config) {
$provider = Auth::createUserProvider($config['provider']);
$guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
}
return $guard;
}
}
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->app->session->extend('admin_database', function ($app) {
$table = 'administrator_sessions';
// This is not workin, because I don't have the user her.
/*if (auth()->user() instanceof Customer) {
$table = 'customer_sessions';
}
else if (auth()->user() instanceof Administrator){
$table = 'administrator_sessions';
}*/
$lifetime = $app['config']['session.lifetime'];
$connection = $app['db']->connection($app['config']['session.connection']);
return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
});
}
}
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Administrator::class => UserPolicy::class,
];
public function boot()
{
$this->registerPolicies();
Auth::extend('admin_session', function ($app, $name, array $config) {
$provider = Auth::createUserProvider($config['provider']);
$guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
}
return $guard;
}
}
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->app->session->extend('admin_database', function ($app) {
$table = 'administrator_sessions';
// This is not workin, because I don't have the user her.
/*if (auth()->user() instanceof Customer) {
$table = 'customer_sessions';
}
else if (auth()->user() instanceof Administrator){
$table = 'administrator_sessions';
}*/
$lifetime = $app['config']['session.lifetime'];
$connection = $app['db']->connection($app['config']['session.connection']);
return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
});
}
}
New contributor
I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Administrator::class => UserPolicy::class,
];
public function boot()
{
$this->registerPolicies();
Auth::extend('admin_session', function ($app, $name, array $config) {
$provider = Auth::createUserProvider($config['provider']);
$guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));
if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}
if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}
if (method_exists($guard, 'setRequest')) {
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
}
return $guard;
}
}
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->app->session->extend('admin_database', function ($app) {
$table = 'administrator_sessions';
// This is not workin, because I don't have the user her.
/*if (auth()->user() instanceof Customer) {
$table = 'customer_sessions';
}
else if (auth()->user() instanceof Administrator){
$table = 'administrator_sessions';
}*/
$lifetime = $app['config']['session.lifetime'];
$connection = $app['db']->connection($app['config']['session.connection']);
return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
});
}
}
New contributor
New contributor
answered yesterday
Hacko
11
11
New contributor
New contributor
add a comment |
add a comment |
Hacko is a new contributor. Be nice, and check out our Code of Conduct.
Hacko is a new contributor. Be nice, and check out our Code of Conduct.
Hacko is a new contributor. Be nice, and check out our Code of Conduct.
Hacko is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418791%2fmulti-session-tables-in-laravel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you have a look at
config/session.php
, you'll notice'connection' => null
and'table' => 'sessions'
; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…– Tim Lewis
Nov 21 at 19:15
But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago