Created
September 17, 2013 11:28
-
-
Save tolu360/6593085 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/* | |
|-------------------------------------------------------------------------- | |
| API Token Filter | |
|-------------------------------------------------------------------------- | |
| | |
| Check for a valid access token or throw an Exception. | |
| | |
| | |
*/ | |
$server = new League\OAuth2\Server\Resource( | |
new League\OAuth2\Server\Storage\Fluent\Session() | |
); | |
Route::filter('token', function () use ($server) | |
{ | |
try { | |
$server->isValid(); | |
//echo 'This is it'; | |
} catch (League\OAuth2\Server\Exception\InvalidAccessTokenException $e) { | |
return Response::json(array('error' => $e->getMessage())); | |
} | |
}); | |
/* | |
|-------------------------------------------------------------------------- | |
| API Resource Controller (app/Controllers/ApiResourceController.php) | |
|-------------------------------------------------------------------------- | |
| | |
| | |
| | |
*/ | |
use \League\OAuth2\Server\Storage\Fluent\Session; | |
class ApiResourceController extends BaseController { | |
protected $server; | |
public function __construct() | |
{ | |
$request = new League\OAuth2\Server\Util\Request(); | |
$this->server = new League\OAuth2\Server\Resource(new Session); | |
$this->server->setRequest($request); | |
} | |
public function getUser () | |
{ | |
//return 'Sup yo?'; | |
if ($this->server->getOwnerType() === 'user') | |
{ | |
// Get the access token owner's ID | |
$userId = $this->server->getOwnerId(); | |
return $user = User::find($userId); | |
} | |
} | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| API Route Filter (app/routes.php) | |
|-------------------------------------------------------------------------- | |
| | |
| | |
| | |
*/ | |
Route::get('api/user', array('before' => 'token', 'uses' => 'ApiResourceController@getUser')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment