Skip to content

Commit 25d12ef

Browse files
committed
Projeto - Primeiras Autenticações
1 parent fe2eb5b commit 25d12ef

30 files changed

+526
-8
lines changed

.env.example

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ PUSHER_KEY=
3232
PUSHER_SECRET=
3333

3434
URL_ADMIN_LOGIN=/admin/login
35-
URL_ADMIN_LOGOUT=/admin/logout
35+
URL_ADMIN_LOGOUT=/admin/logout
36+
37+
JWT_SECRET=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace CodeFin\Http\Controllers\Api;
4+
5+
use CodeFin\Http\Controllers\Controller;
6+
use Illuminate\Foundation\Auth\AuthenticatesUsers;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Support\Facades\Lang;
10+
11+
class AuthController extends Controller
12+
{
13+
use AuthenticatesUsers;
14+
15+
public function accessToken(Request $request)
16+
{
17+
$this->validateLogin($request);
18+
19+
if ($this->hasTooManyLoginAttempts($request)) {
20+
$this->fireLockoutEvent($request);
21+
22+
return $this->sendLockoutResponse($request);
23+
}
24+
25+
$credentials = $this->credentials($request);
26+
27+
if($token = Auth::guard('api')->attempt($credentials)){
28+
return $this->sendLoginResponse($request,$token);
29+
}
30+
31+
$this->incrementLoginAttempts($request);
32+
33+
return $this->sendFailedLoginResponse($request);
34+
}
35+
36+
public function refreshToken(Request $request)
37+
{
38+
$token = Auth::guard('api')->refresh();
39+
return $this->sendLoginResponse($request,$token);
40+
}
41+
42+
protected function sendLoginResponse(Request $request, $token)
43+
{
44+
$this->clearLoginAttempts($request);
45+
46+
return response()->json([
47+
'token' => $token
48+
]);
49+
}
50+
51+
protected function sendLockoutResponse(Request $request)
52+
{
53+
$seconds = $this->limiter()->availableIn(
54+
$this->throttleKey($request)
55+
);
56+
57+
$message = Lang::get('auth.throttle', ['seconds' => $seconds]);
58+
59+
return response()->json([
60+
'message' => $message
61+
],403);
62+
}
63+
64+
public function sendFailedLoginResponse(Request $request)
65+
{
66+
return response()->json([
67+
'message' => Lang::get('auth.failed')
68+
],401);
69+
}
70+
71+
public function logout()
72+
{
73+
Auth::guard('api')->logout();
74+
75+
return response()->json([],204);
76+
}
77+
78+
79+
80+
}

app/Jwt/Manager.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace CodeFin\Jwt;
4+
5+
use \Tymon\JWTAuth\Manager as JwtManager;
6+
use Tymon\JWTAuth\Token;
7+
8+
class Manager extends JwtManager{
9+
10+
public function refresh(Token $token, $forceForever = false, $resetClaims = false)
11+
{
12+
$this->setRefreshFlow();
13+
return parent::refresh($token, $forceForever, $resetClaims);
14+
}
15+
}

app/Providers/AppServiceProvider.php

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

33
namespace CodeFin\Providers;
44

5+
use CodeFin\Jwt\Manager;
56
use Illuminate\Support\ServiceProvider;
67

78
class AppServiceProvider extends ServiceProvider
@@ -23,6 +24,15 @@ public function boot()
2324
*/
2425
public function register()
2526
{
26-
//
27+
$this->app->singleton('tymon.jwt.manager', function ($app) {
28+
$instance = new Manager(
29+
$app['tymon.jwt.provider.jwt'],
30+
$app['tymon.jwt.blacklist'],
31+
$app['tymon.jwt.payload.factory']
32+
);
33+
34+
return $instance->setBlacklistEnabled((bool) config('jwt.blacklist_enabled'))
35+
->setPersistentClaims(config('jwt.persistent_claims'));
36+
});
2737
}
2838
}

app/User.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

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

8-
class User extends Authenticatable
9+
class User extends Authenticatable implements JWTSubject
910
{
1011
use Notifiable;
1112
const ROLE_ADMIN = "admin";
@@ -26,4 +27,30 @@ class User extends Authenticatable
2627
protected $hidden = [
2728
'password', 'remember_token',
2829
];
30+
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->id;
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()
47+
{
48+
return [
49+
'user'=>[
50+
'id'=> $this->id,
51+
'name'=> $this->name,
52+
'email'=> $this->email,
53+
]
54+
];
55+
}
2956
}

composer.lock

+134-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
/*
167167
* Package Service Providers...
168168
*/
169-
169+
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
170170
//
171171

172172
/*

config/auth.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
],
4343

4444
'api' => [
45-
'driver' => 'token',
45+
'driver' => 'jwt',
4646
'provider' => 'users',
4747
],
4848
],

0 commit comments

Comments
 (0)