Skip to content

Commit 050e5d1

Browse files
committed
auth switched to JWT
1 parent eedc553 commit 050e5d1

21 files changed

+756
-111
lines changed

app/Http/Controllers/Api/v1/PropertyController.php

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class PropertyController extends Controller
1616
{
1717
/**
1818
* List properties
19+
*
20+
* @unauthenticated
1921
*/
2022
public function index(GenericListingRequest $request)
2123
{
@@ -106,6 +108,8 @@ public function store(StorePropertyRequest $request)
106108

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

app/Http/Controllers/Auth/AuthenticatedSessionController.php

-41
This file was deleted.

app/Http/Controllers/Auth/AuthenticatedUserController.php

-21
This file was deleted.

app/Http/Controllers/Auth/EmailVerificationNotificationController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use Illuminate\Http\Request;
99

1010
/**
11-
* @tags Auth
11+
* @tags Auth (JWT)
1212
*/
1313
class EmailVerificationNotificationController extends Controller
1414
{
1515
/**
16+
* Email verification link
17+
*
1618
* Send a new email verification notification.
1719
*/
1820
public function store(Request $request): JsonResponse|RedirectResponse

app/Http/Controllers/Auth/NewPasswordController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
use Illuminate\Validation\ValidationException;
1414

1515
/**
16-
* @tags Auth
16+
* @tags Auth (JWT)
1717
*/
1818
class NewPasswordController extends Controller
1919
{
2020
/**
21+
* Password change
22+
*
2123
* Handle an incoming new password request.
2224
*
2325
* @throws \Illuminate\Validation\ValidationException

app/Http/Controllers/Auth/PasswordResetLinkController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
use Illuminate\Validation\ValidationException;
1010

1111
/**
12-
* @tags Auth
12+
* @tags Auth (JWT)
1313
*/
1414
class PasswordResetLinkController extends Controller
1515
{
1616
/**
17+
* Password reset link
18+
*
1719
* Handle an incoming password reset link request.
1820
*
1921
* @throws \Illuminate\Validation\ValidationException

app/Http/Controllers/Auth/RegisteredUserController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@
1212
use Illuminate\Validation\Rules;
1313

1414
/**
15-
* @tags Auth
15+
* @tags Auth (JWT)
1616
*/
1717
class RegisteredUserController extends Controller
1818
{
1919
/**
20+
* Register
21+
*
2022
* Handle an incoming registration request.
2123
*
2224
* @throws \Illuminate\Validation\ValidationException
2325
*/
24-
public function store(Request $request): Response
26+
public function register(Request $request): Response
2527
{
2628
$request->validate([
2729
'name' => ['required', 'string', 'max:255'],
@@ -37,8 +39,6 @@ public function store(Request $request): Response
3739

3840
event(new Registered($user));
3941

40-
Auth::login($user);
41-
4242
return response()->noContent();
4343
}
4444
}

app/Http/Controllers/Controller.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Http\Controllers;
44

5-
abstract class Controller
5+
use Illuminate\Routing\Controller as BaseController;
6+
7+
abstract class Controller extends BaseController
68
{
79
//
810
}

app/Models/User.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace App\Models;
44

5-
// use Illuminate\Contracts\Auth\MustVerifyEmail;
65
use App\Models\Relationships\UserRelationships;
76
use Illuminate\Database\Eloquent\Factories\HasFactory;
87
use Illuminate\Foundation\Auth\User as Authenticatable;
98
use Illuminate\Notifications\Notifiable;
109
use Laravel\Sanctum\HasApiTokens;
10+
use Tymon\JWTAuth\Contracts\JWTSubject;
1111

12-
class User extends Authenticatable
12+
class User extends Authenticatable implements JWTSubject
1313
{
1414
/** @use HasFactory<\Database\Factories\UserFactory> */
1515
use HasApiTokens, HasFactory, Notifiable, UserRelationships;
@@ -47,4 +47,24 @@ protected function casts(): array
4747
'password' => 'hashed',
4848
];
4949
}
50+
51+
/**
52+
* Get the identifier that will be stored in the subject claim of the JWT.
53+
*
54+
* @return mixed
55+
*/
56+
public function getJWTIdentifier()
57+
{
58+
return $this->getKey();
59+
}
60+
61+
/**
62+
* Return a key value array, containing any custom claims to be added to the JWT.
63+
*
64+
* @return array
65+
*/
66+
public function getJWTCustomClaims()
67+
{
68+
return [];
69+
}
5070
}

app/Providers/AppServiceProvider.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Illuminate\Auth\Notifications\ResetPassword;
66
use Illuminate\Support\ServiceProvider;
7+
use Dedoc\Scramble\Scramble;
8+
use Dedoc\Scramble\Support\Generator\OpenApi;
9+
use Dedoc\Scramble\Support\Generator\SecurityScheme;
710

811
class AppServiceProvider extends ServiceProvider
912
{
@@ -23,5 +26,11 @@ public function boot(): void
2326
ResetPassword::createUrlUsing(function (object $notifiable, string $token) {
2427
return config('app.frontend_url')."/password-reset/$token?email={$notifiable->getEmailForPasswordReset()}";
2528
});
29+
30+
Scramble::afterOpenApiGenerated(function (OpenApi $openApi) {
31+
$openApi->secure(
32+
SecurityScheme::http('bearer', 'JWT')
33+
);
34+
});
2635
}
2736
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"laravel/framework": "^11.31",
1515
"laravel/sanctum": "^4.0",
1616
"laravel/tinker": "^2.9",
17-
"spatie/laravel-query-builder": "^6.2"
17+
"spatie/laravel-query-builder": "^6.2",
18+
"tymon/jwt-auth": "^2.1"
1819
},
1920
"require-dev": {
2021
"fakerphp/faker": "^1.23",

0 commit comments

Comments
 (0)