Skip to content

Instantly share code, notes, and snippets.

@anotherZero
Forked from hackel/SentryAuthAdapter.php
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save anotherZero/04f7b416f7c72c484bdf to your computer and use it in GitHub Desktop.

Select an option

Save anotherZero/04f7b416f7c72c484bdf to your computer and use it in GitHub Desktop.
<?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