I'm using Tymon's JWT in lumen with this version > "tymon/jwt-auth": "^1.0@dev"
on my composer.
How can I pass parameters on the attempt
but will not actually use it for on the sql query?
Currently, I am able to filter by username and password and will return the token:
LoginController.php
$token = $jwt->attempt([
'username' => $request->get('username'),
'password' => md5($request->get('password'))
]);
But since I am using multiple database per site, I want the application to determine what site_code it is using and set the database base on the site_code given.
Example:
If I am using site A, it should use database_a
, if site B then database_b
and so forth. Currently, I can manually set what database connection and table to use on the User
model.
Example:
User.php
public function __construct()
{
$this->setConnection('database_a');
$this->setTable('users');
}
But since the application handles different sites, it passes a site_code
on post (aside from the username and password) and determines which database to use base on the site_code
.
How could I set the database connection and table base on the site code given?
What I tried is to pass the site code on the attempt
like this:
$token = $jwt->attempt([
'username' => $request->get('username'),
'password' => md5($request->get('password')),
'siteCode => $request->get('siteCode')
]);
So I could do like so on the User
model:
public function __construct()
{
switch($this->siteCode) {
case 'A':
$this->setConnection('database_a');
break;
default:
$this->setConnection('database_b');
}
$this->setTable('users');
}
But what is happening is that it actually uses the parameters passed on the attempt
method for sql. So it gives me an error like this:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'siteCode' in 'where clause' (SQL: select * from
userswhere
username= test and password = test and
siteCode= A limit 1)
Is there a way to get the siteCode
request and pass it on to the User
model to determine the database connection but not use it for the sql query?
I have found a way by doing like so. But would like to know if this is the right way to do it or are there any other "proper way" to do it? Having one API to handle multiple sites and databases is applicable if the database structure of those sites are the same. An example of this are blog sites that have the same database structure across the board that shares the same services.
User.php (model)
class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject
{
//some codes
public function __construct()
{
$siteCode = app('Illuminate\Http\Request')->get('siteCode');
switch ($siteCode) {
case 'A':
$this->setConnection('database_a');
break;
default:
$this->setConnection('database_b');
break;
}
$this->setTable('users');
}
//some codes
}