-
composer.json
Open in GitHub{ // "require": { "php": "^7.3", // "laravel/sanctum": "^2.6" }, // }
-
routes/api.php
Open in GitHubuse App\Http\Controllers\AuthController; use Illuminate\Support\Facades\Route; Route::post('token', [AuthController::class, 'requestToken']);
-
app/Http/Controllers/AuthController.php
Open in GitHubuse App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\ValidationException; class AuthController extends Controller { public function requestToken(Request $request): string { $request->validate([ 'email' => 'required|email', 'password' => 'required', 'device_name' => 'required', ]); $user = User::where('email', $request->email)->first(); if (! $user || ! Hash::check($request->password, $user->password)) { throw ValidationException::withMessages([ 'email' => ['The provided credentials are incorrect.'], ]); } return $user->createToken($request->device_name)->plainTextToken; } }