Am trying to build a multi tennant application and switching between the databases in my middleware
So i have the following middleware
public function handle($request, Closure $next)
{
//check if request has the value of header
$passedheader = $request->header('HTTP_X_APP_LOCATION');
$headers = AppConnectionsModel::get()->pluck('header_val')->toArray();
if(!isset($passedheader) || !in_array($passedheader,$headers)){
return response('Invalid Request.', 403);
}
//get the actual header passed
$connection = AppConnectionsModel::where('header_val',$passedheader)->first();
if($connection){
Config::set('database.connections.tennant.database', $connection->database);
Config::set('database.connections.tennant.port', $connection->port);
Config::set('database.connections.tennant.username', $connection->username);
Config::set('database.connections.tennant.password', $connection->password);
Config::set('database.connections.tennant.host', $connection->host);
return $next($request);
}else{
return response("Invalid Request.", 200);
}
return $next($request);
}
So what basically the middleware does is to check the connection settings and set to the config
So i would like to apply this middleware to the passport token route that is oauth/token
so that every time a user sends a request to get a token his or her db is used
In my users model i have
protected $connection = "tennant";
protected $table = "tbl_user";
How do i override the oauth/token passport route in laravel to use this middleware
As suggested below i have also tried changing middleware priority in kernel file like
protected $routeMiddleware = [
'appconnection' => \App\Http\Middleware\AppConnectionMiddleware::class,
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
];
app connection is the middleware io have placed at the top having the handle method shown above But it doesnt get executed before passport oauth/token route
Checking on my configs the passport routes are set in the AuthServiceProvider
like
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
//
}
}