Create a helper trait to find a user by the given Bearer token

parent 176352ca
<?php
namespace Modules\User;
use Modules\User\Entities\UserInterface;
use Modules\User\Repositories\UserTokenRepository;
trait CanFindUserWithBearerToken
{
/**
* @param string $token
* @return UserInterface|null
*/
public function findUserWithBearerToken($token)
{
$token = app(UserTokenRepository::class)->findByAttributes(['access_token' => $this->parseToken($token)]);
if ($token === null) {
return null;
}
return $token->user;
}
private function parseToken($token)
{
return str_replace('Bearer ', '', $token);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment