Skip to content

Commit

Permalink
Merge pull request #150 from tuandm/feature/laravel-sanctum
Browse files Browse the repository at this point in the history
Laravel/sanctum integration
  • Loading branch information
tuandm authored Mar 25, 2020
2 parents 6083080 + 547ae65 commit 5e8f6de
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 924 deletions.
8 changes: 3 additions & 5 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ public function login(Request $request)
}

$user = $request->user();
$tokenResult = $user->createToken('Personal Access Token');
$token = $tokenResult->token;
$token->save();
$token = $user->createToken('laravue');

return response()->json(new UserResource($user), Response::HTTP_OK)->header('Authorization', $tokenResult->accessToken);
return response()->json(new UserResource($user), Response::HTTP_OK)->header('Authorization', $token->plainTextToken);
}

public function logout(Request $request)
{
$request->user()->token()->revoke();
$request->user()->tokens()->delete();
return response()->json((new JsonResponse())->success([]), Response::HTTP_OK);
}

Expand Down
5 changes: 4 additions & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;


class Kernel extends HttpKernel
{
Expand Down Expand Up @@ -38,8 +40,9 @@ class Kernel extends HttpKernel
],

'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:60,1',
'bindings',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

Expand Down
2 changes: 1 addition & 1 deletion app/Laravue/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Laravel\Sanctum\HasApiTokens;

/**
* Class User
Expand Down
4 changes: 0 additions & 4 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -25,7 +23,5 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
$this->registerPolicies();

Passport::routes();
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "^7.2",
"fideloper/proxy": "^4.0",
"laravel/framework": "^7.0",
"laravel/passport": "^8.4",
"laravel/sanctum": "^2.1",
"laravel/tinker": "^2.0",
"league/flysystem-aws-s3-v3": "^1.0",
"spatie/laravel-permission": "^3.0"
Expand Down
Loading

0 comments on commit 5e8f6de

Please sign in to comment.