Skip to content

Commit f8832f8

Browse files
committed
added OAuth2 authentication
1 parent f922000 commit f8832f8

File tree

11 files changed

+901
-14
lines changed

11 files changed

+901
-14
lines changed

app/Http/Middleware/Authenticate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(Auth $auth)
3636
public function handle($request, Closure $next, $guard = null)
3737
{
3838
if ($this->auth->guard($guard)->guest()) {
39-
return response('Unauthorized.', 401);
39+
return response()->json((['status' => 401, 'message' => 'Unauthorized']), 401);
4040
}
4141

4242
return $next($request);

app/Models/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
99
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
1010
use Illuminate\Database\Eloquent\SoftDeletes;
11+
use Laravel\Passport\HasApiTokens;
1112

1213
class User extends Model implements AuthenticatableContract, AuthorizableContract
1314
{
14-
use Authenticatable, Authorizable, SoftDeletes;
15+
use Authenticatable, Authorizable, SoftDeletes, HasApiTokens;
1516

1617
/**
1718
* The database table used by the model.

app/Providers/AuthServiceProvider.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace App\Providers;
44

5-
use App\User;
5+
use App\Models\User;
6+
use Laravel\Passport\Passport;
67
use Illuminate\Support\Facades\Gate;
78
use Illuminate\Support\ServiceProvider;
89

@@ -35,5 +36,10 @@ public function boot()
3536
return User::where('api_token', $request->input('api_token'))->first();
3637
}
3738
});
39+
40+
Passport::tokensCan([
41+
'admin' => 'Admin user scope',
42+
'basic' => 'Basic user scope',
43+
]);
3844
}
3945
}

app/Repositories/EloquentUserRepository.php

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Repositories\Contracts\UserRepository;
66
use App\Models\User;
77
use Illuminate\Support\Facades\Hash;
8+
use Illuminate\Database\Eloquent\Model;
89

910
class EloquentUserRepository extends AbstractEloquentRepository implements UserRepository
1011
{
@@ -30,4 +31,18 @@ public function save(array $data)
3031

3132
return $user;
3233
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function update(Model $model, array $data)
39+
{
40+
if (isset($data['password'])) {
41+
$data['password'] = Hash::make($data['password']);
42+
}
43+
44+
$updatedUser = parent::update($model, $data);
45+
46+
return $updatedUser;
47+
}
3348
}

bootstrap/app.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363
// App\Http\Middleware\ExampleMiddleware::class
6464
// ]);
6565

66-
// $app->routeMiddleware([
67-
// 'auth' => App\Http\Middleware\Authenticate::class,
68-
// ]);
66+
$app->routeMiddleware([
67+
'auth' => App\Http\Middleware\Authenticate::class,
68+
]);
6969

7070
/*
7171
|--------------------------------------------------------------------------
@@ -79,9 +79,11 @@
7979
*/
8080

8181
// $app->register(App\Providers\AppServiceProvider::class);
82-
// $app->register(App\Providers\AuthServiceProvider::class);
82+
$app->register(App\Providers\AuthServiceProvider::class);
8383
// $app->register(App\Providers\EventServiceProvider::class);
8484
$app->register(App\Providers\RepositoriesServiceProvider::class);
85+
$app->register(Laravel\Passport\PassportServiceProvider::class);
86+
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
8587

8688
/*
8789
|--------------------------------------------------------------------------

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"laravel/lumen-framework": "5.4.*",
1010
"vlucas/phpdotenv": "~2.2",
1111
"ramsey/uuid": "^3.5",
12-
"league/fractal": "^0.15.0"
12+
"league/fractal": "^0.15.0",
13+
"dusterio/lumen-passport": "^0.1.9"
1314
},
1415
"require-dev": {
1516
"fzaninotto/faker": "~1.4",

0 commit comments

Comments
 (0)