Last active
June 24, 2021 14:48
-
-
Save hackel/00a2b5a43d5007eceaa1 to your computer and use it in GitHub Desktop.
SentryAuthAdapter for using Tymon\JWTAuth with Cartalyst\Sentry
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(); | |
} | |
} |
Figured this out :)
Hey @iolson can I see an example of a config file? I'm having trouble integrating JWT with Multiauth on Laravel 5.1.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How did you hook this up to be used in the config file?