Skip to content

Commit

Permalink
Working dingo+jwt auth with example routes
Browse files Browse the repository at this point in the history
  • Loading branch information
0plus1 committed Nov 30, 2015
1 parent c7e609f commit 8aae4be
Show file tree
Hide file tree
Showing 9 changed files with 1,017 additions and 75 deletions.
50 changes: 50 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomKey!!!

APP_LOCALE=en
APP_FALLBACK_LOCALE=en

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=lumendingojwtapi
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=memcached
SESSION_DRIVER=memcached
QUEUE_DRIVER=database

# MAIL_DRIVER=smtp
# MAIL_HOST=mailtrap.io
# MAIL_PORT=2525
# MAIL_USERNAME=null
# MAIL_PASSWORD=null
# MAIL_FROM_ADDRESS=null
# MAIL_FROM_NAME=null

# FILESYSTEM_DRIVER=local
# FILESYSTEM_CLOUD=s3

# S3_KEY=null
# S3_SECRET=null
# S3_REGION=null
# S3_BUCKET=null

# RACKSPACE_USERNAME=null
# RACKSPACE_KEY=null
# RACKSPACE_CONTAINER=null
# RACKSPACE_REGION=null

# DINGO/API
API_STANDARDS_TREE=vnd
API_DOMAIN=lumendingojwtapi.local
API_VERSION=v1
API_NAME=lumendingojwtapi
API_CONDITIONAL_REQUEST=true
API_STRICT=false
API_DEBUG=true

# JWT
JWT_SECRET=GenerateWith:: #php artisan jwt:secret
34 changes: 34 additions & 0 deletions app/Http/Controllers/AuthenticateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace App\Http\Controllers;

use Dingo\Api\Http\Request;
use Tymon\JWTAuth\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller
{
protected $auth;

public function __construct(JWTAuth $auth)
{
$this->auth = $auth;
}

public function backend(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');

try {
// attempt to verify the credentials and create a token for the user
if (! $token = $this->auth->attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}

// all good so return the token
return response()->json(compact('token'));
}
}
22 changes: 22 additions & 0 deletions app/Http/Controllers/BackendController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php namespace App\Http\Controllers;

use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Helpers;

use Symfony;


class BackendController extends Controller
{

/**
* @param Request $request
* @return mixed
*/
public function index (Request $request)
{
$user = app('Dingo\Api\Auth\Auth')->user();

return $user;
}
}
12 changes: 10 additions & 2 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
|
*/

$app->get('/', function () use ($app) {
return $app->welcome();
$api = app('Dingo\Api\Routing\Router');

// JWT Protected routes
$api->version('v1', ['middleware' => 'api.auth', 'providers' => 'jwt'], function ($api) {
$api->get('/index', 'App\Http\Controllers\BackendController@index');
});

// Publicly accessible routes
$api->version('v1', [], function ($api) {
$api->post('/authenticate', 'App\Http\Controllers\AuthenticateController@backend');
});
17 changes: 14 additions & 3 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once __DIR__.'/../vendor/autoload.php';

// Dotenv::load(__DIR__.'/../');
Dotenv::load(__DIR__.'/../');

/*
|--------------------------------------------------------------------------
Expand All @@ -21,7 +21,11 @@

// $app->withFacades();

// $app->withEloquent();
$app->withEloquent();

// Register config files
$app->configure('auth');
$app->configure('jwt');

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -78,9 +82,16 @@
|
*/

// $app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);

$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);

app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});

/*
|--------------------------------------------------------------------------
| Load The Application Routes
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"require": {
"php": ">=5.5.9",
"laravel/lumen-framework": "5.1.*",
"vlucas/phpdotenv": "~1.0"
"vlucas/phpdotenv": "~1.0",
"dingo/api": "1.0.x@dev",
"tymon/jwt-auth": "0.6.*@dev"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
Loading

0 comments on commit 8aae4be

Please sign in to comment.