Today, I try to create custom query for JWTAuth, I know JWTAuth using User model as default model.
The point is:
- I already created new model named as Staff Model
- And retrieve staff information detail by 'username' and 'password' with Raw Query
This is my Login Function in Staff Model:
public function login($params) {
$response = array();
try {
$query = "
SELECT a.id AS user_id,
a.titleid,
b.name AS 'titlename',
a.firstname,
a.lastname,
a.email,
a.phone,
a.identitynumber,
a.country,
a.address,
a.city,
a.state,
a.postalcode,
a.dept,
d.name AS 'division_name',
d.name AS 'role_name'
FROM " .$this->user_table. " a
JOIN " .$this->title_table. " b ON b.id = a.titleid
JOIN " .$this->staff_table. " c ON c.id = a.id
JOIN " .$this->division_table. " d ON d.id = c.divisionid
JOIN " .$this->role_table. " e ON e.id = c.role_id
WHERE c.username=? AND c.password=?
";
$bind = array(
$params['username'],
$params['password']
);
$response = DB::select($query, $bind);
} catch(\Exception $e) {
$message = $e->getMessage();
return $message;
}
return $response;
}
This is my Controller, but not linking yet with Staff Model.
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
class AuthenticateController extends Controller
{
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return response()->json(compact('token'));
}
}
In that controller, JWTAuth do attempt the credentials.
I want to custom the JWTAuth to attempt with my Staff Model. I completely lost with this case, anyone know the best practice to solve this ?
Thanks in advance.
via Code On