Skip to content

Commit ae3b876

Browse files
author
Connor Leech
committed
add jwt quickstart
1 parent d482a0f commit ae3b876

File tree

8 files changed

+1022
-338
lines changed

8 files changed

+1022
-338
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ APP_DEBUG=true
55
APP_LOG_LEVEL=debug
66
APP_URL=http://localhost
77

8+
JWT_SECRET=XXXXXXXXXXXX
9+
810
DB_CONNECTION=mysql
911
DB_HOST=127.0.0.1
1012
DB_PORT=3306
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/User.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace App;
44

55
use Illuminate\Notifications\Notifiable;
6+
use Tymon\JWTAuth\Contracts\JWTSubject;
67
use Illuminate\Foundation\Auth\User as Authenticatable;
78

8-
class User extends Authenticatable
9+
class User extends Authenticatable implements JWTSubject
910
{
1011
use Notifiable;
1112

@@ -27,10 +28,23 @@ class User extends Authenticatable
2728
'password', 'remember_token',
2829
];
2930

30-
public function generateToken()
31+
/**
32+
* Get the identifier that will be stored in the subject claim of the JWT.
33+
*
34+
* @return mixed
35+
*/
36+
public function getJWTIdentifier()
37+
{
38+
return $this->getKey();
39+
}
40+
41+
/**
42+
* Return a key value array, containing any custom claims to be added to the JWT.
43+
*
44+
* @return array
45+
*/
46+
public function getJWTCustomClaims()
3147
{
32-
$this->api_token = str_random(60);
33-
$this->save();
34-
return $this->api_token;
48+
return [];
3549
}
3650
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": ">=7.0.0",
99
"fideloper/proxy": "~3.3",
1010
"laravel/framework": "5.5.*",
11-
"laravel/tinker": "~1.0"
11+
"laravel/tinker": "~1.0",
12+
"tymon/jwt-auth": "1.0.0-rc.1"
1213
},
1314
"require-dev": {
1415
"filp/whoops": "~2.0",

0 commit comments

Comments
 (0)