Skip to content

Commit 2b1bfbf

Browse files
committed
JWT installation & setup.
1 parent aaa8c78 commit 2b1bfbf

File tree

9 files changed

+716
-7
lines changed

9 files changed

+716
-7
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Support\Facades\Auth;
6+
use App\Http\Controllers\Controller;
7+
8+
class AuthController extends Controller
9+
{
10+
/**
11+
* Create a new AuthController instance.
12+
*
13+
* @return void
14+
*/
15+
public function __construct()
16+
{
17+
$this->middleware('auth:api', ['except' => ['login']]);
18+
}
19+
20+
/**
21+
* Get a JWT via given credentials.
22+
*
23+
* @return \Illuminate\Http\JsonResponse
24+
*/
25+
public function login()
26+
{
27+
$credentials = request(['email', 'password']);
28+
29+
if (! $token = auth()->attempt($credentials)) {
30+
return response()->json(['error' => 'Unauthorized'], 401);
31+
}
32+
33+
return $this->respondWithToken($token);
34+
}
35+
36+
/**
37+
* Get the authenticated User.
38+
*
39+
* @return \Illuminate\Http\JsonResponse
40+
*/
41+
public function me()
42+
{
43+
return response()->json(auth()->user());
44+
}
45+
46+
/**
47+
* Log the user out (Invalidate the token).
48+
*
49+
* @return \Illuminate\Http\JsonResponse
50+
*/
51+
public function logout()
52+
{
53+
auth()->logout();
54+
55+
return response()->json(['message' => 'Successfully logged out']);
56+
}
57+
58+
/**
59+
* Refresh a token.
60+
*
61+
* @return \Illuminate\Http\JsonResponse
62+
*/
63+
public function refresh()
64+
{
65+
return $this->respondWithToken(auth()->refresh());
66+
}
67+
68+
/**
69+
* Get the token array structure.
70+
*
71+
* @param string $token
72+
*
73+
* @return \Illuminate\Http\JsonResponse
74+
*/
75+
protected function respondWithToken($token)
76+
{
77+
return response()->json([
78+
'access_token' => $token,
79+
'token_type' => 'bearer',
80+
'expires_in' => auth()->factory()->getTTL() * 60
81+
]);
82+
}
83+
}

app/Http/Controllers/PostController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class PostController extends Controller
1616
*/
1717
public function index()
1818
{
19-
$post = Post::latest()->take(5)->get();;
19+
$post = Post::latest()->take(3)->get();;
2020
return PostResource::collection($post);
2121
}
2222

app/User.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace App;
44

5+
use Tymon\JWTAuth\Contracts\JWTSubject;
56
use Illuminate\Notifications\Notifiable;
67
use Illuminate\Contracts\Auth\MustVerifyEmail;
78
use Illuminate\Foundation\Auth\User as Authenticatable;
89

9-
class User extends Authenticatable
10-
{
10+
class User extends Authenticatable implements JWTSubject{
1111
use Notifiable;
1212

1313
/**
@@ -36,4 +36,26 @@ class User extends Authenticatable
3636
protected $casts = [
3737
'email_verified_at' => 'datetime',
3838
];
39+
40+
// Rest omitted for brevity
41+
42+
/**
43+
* Get the identifier that will be stored in the subject claim of the JWT.
44+
*
45+
* @return mixed
46+
*/
47+
public function getJWTIdentifier()
48+
{
49+
return $this->getKey();
50+
}
51+
52+
/**
53+
* Return a key value array, containing any custom claims to be added to the JWT.
54+
*
55+
* @return array
56+
*/
57+
public function getJWTCustomClaims()
58+
{
59+
return [];
60+
}
3961
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"php": "^7.1.3",
1212
"fideloper/proxy": "^4.0",
1313
"laravel/framework": "5.8.*",
14-
"laravel/tinker": "^1.0"
14+
"laravel/tinker": "^1.0",
15+
"tymon/jwt-auth": "^1.0"
1516
},
1617
"require-dev": {
1718
"beyondcode/laravel-dump-server": "^1.0",

0 commit comments

Comments
 (0)