Dynamically changing a Laravel application session

The laravel app url will be something like app.laravel.com\{clientName} . All routes will follow client_name , for example app.laravel.com\{clientName}\home , app.laravel.com\{clientName}\profile . Downloading / rendering an application depends on clientName .

routes/web.php

 Route::group(['prefix' => '{clientName}', 'middleware' => 'appclient'], function () { Route::get('/', ' ClientController@index '); Route::post('login', 'Auth\ LoginController@login '); Route::post('logout', 'Auth\ LoginController@logout '); Route::get('home', ' HomeController@index '); }); 

The appclient middleware

 public function handle($request, Closure $next) { $clientName = explode('/', $request->path())[0]; $client = Client::where('clientName', $clientName)->first(); if(!isset($client->id)) { abort(404); } Config::set('session.path', "/$clientName"); return $next($request); } 

What I'm trying to achieve is setting up a session based on the clientName directory. When I log in, I get a TokenMismatchException.

First question

Is it possible to save a session based on a directory app.laravel.com\{clientName} like app.laravel.com\{clientName} ?

Second question

I saw that there is a session.path setting that I tried to use above. If possible, how can I fix this problem? Is it a good idea to update the session path in middleware?

Appreciate any feedback or other approaches.

UPDATE

  • Using Redis as a Session Driver
  • In my further research, the request session token generates a new one each time.
+5
source share
1 answer

What I did is update session.path and session.cookie dynamically.

 Config::set('session.path', "$clientName"); Config::set('session.cookie', $clientName.'_laravel_session'); 

This currently works for me.

0
source

Source: https://habr.com/ru/post/1257834/


All Articles