-
-
Save anotherZero/04f7b416f7c72c484bdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace MyApp\Providers; | |
| use Exception; | |
| use Cartalyst\Sentry\Sentry; | |
| use Cartalyst\Sentry\Users\UserInterface; | |
| use Tymon\JWTAuth\Providers\Auth\AuthInterface; | |
| class SentryAuthAdapter implements AuthInterface | |
| { | |
| /** | |
| * @var Sentry | |
| */ | |
| private $sentry; | |
| /** | |
| * @param \Illuminate\Auth\AuthManager $auth Unnecessary dependency injected by JWTAuthServiceProvider | |
| * @param Sentry $sentry | |
| */ | |
| public function __construct($auth, Sentry $sentry) | |
| { | |
| $this->sentry = $sentry; | |
| } | |
| /** | |
| * Check a user's credentials | |
| * | |
| * @param array $credentials | |
| * @return bool | |
| */ | |
| public function byCredentials(array $credentials = []) | |
| { | |
| try { | |
| $user = $this->sentry->authenticate($credentials); | |
| return $user instanceof UserInterface; | |
| } catch (Exception $e) { | |
| return false; | |
| } | |
| } | |
| /** | |
| * Authenticate a user via the id | |
| * | |
| * @param mixed $id | |
| * @return bool | |
| */ | |
| public function byId($id) | |
| { | |
| try { | |
| $user = $this->sentry->findUserById($id); | |
| $this->sentry->login($user); | |
| return $user instanceof UserInterface && $this->sentry->check(); | |
| } catch (Exception $e) { | |
| return false; | |
| } | |
| } | |
| /** | |
| * Get the currently authenticated user | |
| * | |
| * @return mixed | |
| */ | |
| public function user() | |
| { | |
| return $this->sentry->getUser(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment