Skip to content

Commit 1d542af

Browse files
committed
berhasil menerapkan jwt di laravel
1 parent a5f0af2 commit 1d542af

File tree

26 files changed

+2281
-11852
lines changed

26 files changed

+2281
-11852
lines changed

app/Http/Controllers/Auth/LoginController.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use Illuminate\Http\Request;
56
use App\Http\Controllers\Controller;
67
use Illuminate\Foundation\Auth\AuthenticatesUsers;
78

9+
use Auth;
10+
use JWTAuth;
11+
use Tymon\JWTAuth\Exceptions\JWTException;
12+
813
class LoginController extends Controller
914
{
1015
/*
@@ -34,6 +39,43 @@ class LoginController extends Controller
3439
*/
3540
public function __construct()
3641
{
37-
$this->middleware('guest')->except('logout');
42+
// $this->middleware('guest')->except('logout');
43+
}
44+
45+
public function login(Request $request)
46+
{
47+
$credentials = $request->only('name', 'password');
48+
49+
if ($token = $this->guard()->attempt($credentials)) {
50+
return $this->respondWithToken($token);
51+
}
52+
53+
return response()->json(['error' => 'Unauthorized'], 401);
54+
}
55+
56+
/**
57+
* Get the token array structure.
58+
*
59+
* @param string $token
60+
*
61+
* @return \Illuminate\Http\JsonResponse
62+
*/
63+
protected function respondWithToken($token)
64+
{
65+
return response()->json([
66+
'access_token' => $token,
67+
'token_type' => 'bearer',
68+
'expires_in' => $this->guard()->factory()->getTTL() * 60
69+
]);
70+
}
71+
72+
/**
73+
* Get the guard to be used during authentication.
74+
*
75+
* @return \Illuminate\Contracts\Auth\Guard
76+
*/
77+
public function guard()
78+
{
79+
return Auth::guard('api');
3880
}
3981
}

app/Models/User.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use Illuminate\Notifications\Notifiable;
66
use Illuminate\Contracts\Auth\MustVerifyEmail;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
8+
use Tymon\JWTAuth\Contracts\JWTSubject;
89

9-
class User extends Authenticatable
10+
class User extends Authenticatable implements JWTSubject
1011
{
1112
use Notifiable;
1213

@@ -15,7 +16,7 @@ class User extends Authenticatable
1516
/**
1617
* The attributes that are mass assignable.
1718
*
18-
* @var array
19+
* @var mixed
1920
*/
2021
protected $fillable = [
2122
'name', 'email', 'password', 'address'
@@ -30,6 +31,15 @@ class User extends Authenticatable
3031
'password', 'remember_token',
3132
];
3233

34+
public function getJWTIdentifier()
35+
{
36+
return $this->getKey();
37+
}
38+
public function getJWTCustomClaims()
39+
{
40+
return [];
41+
}
42+
3343
public function user_group()
3444
{
3545
return $this->belongsTo('App\Models\UserGroup');

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"laravel/framework": "5.8.*",
1414
"laravel/tinker": "^1.0",
1515
"phpoffice/phpword": "v0.17.*",
16-
"predis/predis": "^1.1"
16+
"predis/predis": "^1.1",
17+
"tymon/jwt-auth": "1.0.0-rc.4.1"
1718
},
1819
"require-dev": {
1920
"beyondcode/laravel-dump-server": "^1.0",

0 commit comments

Comments
 (0)