Skip to content

Commit 0aa658b

Browse files
add token based sanctum authentication
1 parent a8964c8 commit 0aa658b

File tree

9 files changed

+170
-6
lines changed

9 files changed

+170
-6
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Api\Auth\LoginRequest;
7+
use App\Models\User;
8+
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\Validation\ValidationException;
10+
11+
class LoginController extends Controller
12+
{
13+
/**
14+
* Handle the incoming login request.
15+
*/
16+
public function store(LoginRequest $request): array
17+
{
18+
$user = User::where('email', $request->email)->first();
19+
20+
if (!$user || !Hash::check($request->password, $user->password)) {
21+
throw ValidationException::withMessages([
22+
'email' => ['The provided credentials are incorrect.'],
23+
]);
24+
}
25+
26+
$token = $user->createToken('auth_token')->plainTextToken;
27+
28+
return [
29+
'user' => $user,
30+
'token' => $token,
31+
];
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
8+
class LogoutController extends Controller
9+
{
10+
/**
11+
* Handle the incoming logout request.
12+
*/
13+
public function destroy(Request $request): array
14+
{
15+
$request->user()->currentAccessToken()->delete();
16+
17+
return [
18+
'message' => 'Successfully logged out'
19+
];
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Api\Auth\RegisterRequest;
7+
use App\Models\User;
8+
use Illuminate\Auth\Events\Registered;
9+
use Illuminate\Support\Facades\Hash;
10+
11+
class RegisterController extends Controller
12+
{
13+
/**
14+
* Handle the incoming registration request.
15+
*/
16+
public function store(RegisterRequest $request): array
17+
{
18+
$user = User::create([
19+
'name' => $request->name,
20+
'email' => $request->email,
21+
'password' => Hash::make($request->password),
22+
]);
23+
24+
event(new Registered($user));
25+
26+
$token = $user->createToken('auth_token')->plainTextToken;
27+
28+
return [
29+
'token' => $token
30+
];
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Api\Auth;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class LoginRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, array<int, string>>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'email' => ['required', 'string', 'email'],
26+
'password' => ['required', 'string'],
27+
];
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Api\Auth;
4+
5+
use App\Models\User;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Validation\Rules;
8+
9+
class RegisterRequest extends FormRequest
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*/
14+
public function authorize(): bool
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array<string, array<int, string>>
23+
*/
24+
public function rules(): array
25+
{
26+
return [
27+
'name' => ['required', 'string', 'max:255'],
28+
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:' . User::class],
29+
'password' => ['required', Rules\Password::defaults()],
30+
];
31+
}
32+
}

app/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Illuminate\Notifications\Notifiable;
1111
use Laravel\Sanctum\HasApiTokens;
1212

13-
class User extends Authenticatable implements MustVerifyEmail
13+
class User extends Authenticatable
1414
{
1515
/** @use HasFactory<\Database\Factories\UserFactory> */
1616
use HasApiTokens, HasFactory, Notifiable;

bootstrap/app.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Foundation\Configuration\Exceptions;
77
use Illuminate\Foundation\Configuration\Middleware;
88
use Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets;
9-
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
109

1110
return Application::configure(basePath: dirname(__DIR__))
1211
->withRouting(
@@ -23,10 +22,6 @@
2322
HandleInertiaRequests::class,
2423
AddLinkHeadersForPreloadedAssets::class,
2524
]);
26-
27-
$middleware->api(prepend: [
28-
EnsureFrontendRequestsAreStateful::class,
29-
]);
3025
})
3126
->withExceptions(function (Exceptions $exceptions) {
3227
//

routes/api.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
Route::apiResource('categories', CategoryController::class)->except(['index','show']);
2929
});
3030
});
31+
32+
33+
// Auth routes for API
34+
require __DIR__.'/api_auth.php';

routes/api_auth.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use App\Http\Controllers\Api\Auth\LoginController;
4+
use App\Http\Controllers\Api\Auth\LogoutController;
5+
use App\Http\Controllers\Api\Auth\RegisterController;
6+
use Illuminate\Support\Facades\Route;
7+
8+
9+
Route::prefix('auth')->group(function () {
10+
Route::middleware('guest')->group(function () {
11+
Route::post('register', [RegisterController::class, 'store']);
12+
Route::post('login', [LoginController::class, 'store']);
13+
});
14+
15+
Route::middleware('auth:sanctum')->group(function () {
16+
Route::post('logout', [LogoutController::class, 'destroy']);
17+
});
18+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy