Skip to content

Instantly share code, notes, and snippets.

@hackel
Last active June 24, 2021 14:48
Show Gist options
  • Save hackel/00a2b5a43d5007eceaa1 to your computer and use it in GitHub Desktop.
Save hackel/00a2b5a43d5007eceaa1 to your computer and use it in GitHub Desktop.
SentryAuthAdapter for using Tymon\JWTAuth with Cartalyst\Sentry
<?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();
}
}
@iolson
Copy link

iolson commented May 15, 2015

How did you hook this up to be used in the config file?

@iolson
Copy link

iolson commented May 15, 2015

Figured this out :)

@runjara
Copy link

runjara commented Dec 1, 2016

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