I am using Yii2 Filsh Oauth Server which is working fine,however when I login it generates AccessToken with its default fields i.e
{
"access_token": "f3389e81c234276967079b2293795fc9104a2fac",
"expires_in": 86400,
"token_type": "Bearer",
"user_id": 9,
"scope": null,
"refresh_token": "851464a210f56bb831da378a43e1016bd3e765d7",
}
But I needed to add User info in its response something like
{
"access_token": "f3389e81c234276967079b2293795fc9104a2fac",
"expires_in": 86400,
"token_type": "Bearer",
"scope": null,
"refresh_token": "851464a210f56bb831da378a43e1016bd3e765d7",
"user": {
"id": 9,
"first_name": "Test",
"last_name": "Test2",
"username": "test",
"email": "test@gmail.com",
"status": 1,
"dob": "20-08-1990",
"gender": "Male",
}
}
I came up with a weird workaround I customized bshaffer core library file (which is not a good approach) to meet my requirements, what I did, I changed this line in User model:
return ['user_id' => $user->getId()];
TO THIS
return ['user_id' => [$user->getId(), $userObject]]; //I get all user info in $userObject and passed an array with two fields
since I am passing an array instead of single $user->getId()
hence I needed to modify bshaffer library file AccessToken.php which is available at this path: vendor/bshaffer/oauth2-server-php/src/OAuth2/ResponseType/AccessToken.php
on line 76
I CHANGED THIS:
public function createAccessToken($client_id, $user_id, $scope = null, $includeRefreshToken = true)
{
$token = array(
"access_token" => $this->generateAccessToken(),
"expires_in" => $this->config['access_lifetime'],
"token_type" => $this->config['token_type'],
"user_id" => $user_id,
"scope" => $scope
);
$this->tokenStorage->setAccessToken($token["access_token"], $client_id, $user_id, $this->config['access_lifetime'] ? time() + $this->config['access_lifetime'] : null, $scope);
if ($includeRefreshToken && $this->refreshStorage) {
$token["refresh_token"] = $this->generateRefreshToken();
$expires = 0;
if ($this->config['refresh_token_lifetime'] > 0) {
$expires = time() + $this->config['refresh_token_lifetime'];
}
$this->refreshStorage->setRefreshToken($token['refresh_token'], $client_id, $user_id, $expires, $scope);
}
return $token;
}
TO THIS:
public function createAccessToken($client_id, $user_id, $scope = null, $includeRefreshToken = true)
{
$token = array(
"access_token" => $this->generateAccessToken(),
"expires_in" => $this->config['access_lifetime'],
"token_type" => $this->config['token_type'],
"scope" => $scope,
"user" => $user_id[1] //NOTE: I added new user field and passed second index of array which is user node
);
//NOTE: Here I passed $user_id[0] since $user_id is array hence I am using its 0 index here which has id
$this->tokenStorage->setAccessToken($token["access_token"], $client_id, $user_id[0], $this->config['access_lifetime'] ? time() + $this->config['access_lifetime'] : null, $scope);
if ($includeRefreshToken && $this->refreshStorage) {
$token["refresh_token"] = $this->generateRefreshToken();
$expires = 0;
if ($this->config['refresh_token_lifetime'] > 0) {
$expires = time() + $this->config['refresh_token_lifetime'];
}
//NOTE: Same goes here passing $user_id[0]
$this->refreshStorage->setRefreshToken($token['refresh_token'], $client_id, $user_id[0], $expires, $scope);
}
return $token;
}
Everything works pefect now the PROBLEM is since I modified bshaffer core file when I run composer it again overrites its default code and my changes just wash out each time after running composer I again need to modify same file. I need a proper workarount may be any component
where I override this calss/method and put my changes so that it ramians same after running composer.