Skip to content

Commit dc75136

Browse files
committed
jwt
1 parent 0046e8a commit dc75136

File tree

4 files changed

+64
-27
lines changed

4 files changed

+64
-27
lines changed

Diff for: app/Http/Controllers/Auth/LoginController.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ public function __construct()
3838
$this->middleware('guest')->except('logout');
3939
}
4040

41+
/**
42+
* Get a JWT via given credentials.
43+
*
44+
* @return \Illuminate\Http\JsonResponse
45+
*/
46+
public function login()
47+
{
48+
$credentials = request(['email', 'password']);
49+
50+
if (! $token = auth()->attempt($credentials)) {
51+
return response()->json(['error' => 'Unauthorized'], 401);
52+
}
53+
54+
return $this->respondWithToken($token);
55+
}
56+
57+
/**
58+
* Refresh a token.
59+
*
60+
* @return \Illuminate\Http\JsonResponse
61+
*/
62+
public function refresh()
63+
{
64+
return $this->respondWithToken(auth()->refresh());
65+
}
66+
4167
/**
4268
* Get the token array structure.
4369
*
@@ -50,7 +76,19 @@ protected function respondWithToken($token)
5076
return response()->json([
5177
'access_token' => $token,
5278
'token_type' => 'bearer',
53-
'expires_in' => auth()->factory()->getTTL() * 60
79+
'expires_in' => auth('api')->factory()->getTTL() * 60
5480
]);
5581
}
82+
83+
/**
84+
* Log the user out (Invalidate the token).
85+
*
86+
* @return \Illuminate\Http\JsonResponse
87+
*/
88+
public function logout()
89+
{
90+
auth()->logout();
91+
92+
return response()->json(['message' => 'Successfully logged out']);
93+
}
5694
}

Diff for: app/Http/Controllers/Auth/RegisterController.php

+23-25
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,16 @@
33
namespace App\Http\Controllers\Auth;
44

55
use App\Http\Controllers\Controller;
6-
use App\Providers\RouteServiceProvider;
7-
use App\User;
6+
use App\Models\User;
7+
use Illuminate\Contracts\Auth\MustVerifyEmail;
88
use Illuminate\Foundation\Auth\RegistersUsers;
9-
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\Http\Request;
1010
use Illuminate\Support\Facades\Validator;
1111

1212
class RegisterController extends Controller
1313
{
14-
/*
15-
|--------------------------------------------------------------------------
16-
| Register Controller
17-
|--------------------------------------------------------------------------
18-
|
19-
| This controller handles the registration of new users as well as their
20-
| validation and creation. By default this controller uses a trait to
21-
| provide this functionality without requiring any additional code.
22-
|
23-
*/
24-
2514
use RegistersUsers;
2615

27-
/**
28-
* Where to redirect users after registration.
29-
*
30-
* @var string
31-
*/
32-
protected $redirectTo = RouteServiceProvider::HOME;
33-
3416
/**
3517
* Create a new controller instance.
3618
*
@@ -41,6 +23,22 @@ public function __construct()
4123
$this->middleware('guest');
4224
}
4325

26+
/**
27+
* The user has been registered.
28+
*
29+
* @param \Illuminate\Http\Request $request
30+
* @param \App\User $user
31+
* @return \Illuminate\Http\JsonResponse
32+
*/
33+
protected function registered(Request $request, User $user)
34+
{
35+
if ($user instanceof MustVerifyEmail) {
36+
return response()->json(['status' => trans('verification.sent')]);
37+
}
38+
39+
return response()->json($user);
40+
}
41+
4442
/**
4543
* Get a validator for an incoming registration request.
4644
*
@@ -50,9 +48,9 @@ public function __construct()
5048
protected function validator(array $data)
5149
{
5250
return Validator::make($data, [
53-
'name' => ['required', 'string', 'max:255'],
54-
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
55-
'password' => ['required', 'string', 'min:8', 'confirmed'],
51+
'name' => 'required|max:255',
52+
'email' => 'required|email:filter|max:255|unique:users',
53+
'password' => 'required|min:6|confirmed',
5654
]);
5755
}
5856

@@ -67,7 +65,7 @@ protected function create(array $data)
6765
return User::create([
6866
'name' => $data['name'],
6967
'email' => $data['email'],
70-
'password' => Hash::make($data['password']),
68+
'password' => bcrypt($data['password']),
7169
]);
7270
}
7371
}

Diff for: config/auth.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
'providers' => [
6969
'users' => [
7070
'driver' => 'eloquent',
71-
'model' => App\User::class,
71+
'model' => App\Models\User::class,
7272
],
7373

7474
// 'users' => [

Diff for: routes/api.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222

2323
Route::group(['middleware' => 'guest:api'], function () {
2424
Route::post('login', [LoginController::class, 'login']);
25+
Route::post('logout', [LoginController::class, 'logout']);
2526
Route::post('register', [RegisterController::class, 'register']);
2627
});

0 commit comments

Comments
 (0)