I have login metod in Laravel api. When I try to send request i have code 200 without content in Postman
AuthController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\LoginRequest;
use App\Services\AuthService;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
class AuthController extends Controller
{
protected $authService;
public function __construct(AuthService $authService)
{
$this->authService = $authService;
}
public function login(LoginRequest $request)
{
try
{
$res = $this->authService->loginUser($request);
return response($res, 202);
}
catch(Exception $e)
{
if($e instanceof AuthenticationException)
return response(['message' => 'Nieprawidłowy adres email lub hasło!'], 401);
}
}
public function logout(Request $request)
{
try
{
$res = $this->authService->logoutUser($request);
return response($res, 200);
}
catch(Exception $e)
{
throw $e;
}
}
}
api.php
<?php
use App\Http\Controllers\AuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::fallback(function () {
return abort(404);
});
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/auth/login', [AuthController::class, 'login']);
Route::post('/auth/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum');
AuthService.php
<?php
namespace App\Services;
use App\Http\Requests\LoginRequest;
use App\Http\Resources\UserResource;
use App\Repositories\UserRepository;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
class AuthService {
protected $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function loginUser(LoginRequest $request)
{
$user = $this->userRepository->findByEmail($request['email']);
if(!$user) throw new AuthenticationException();
$isCorrectPassword = $this->userRepository->comparePassword($request['hasło'], $user);
$this->validateUser($user, $isCorrectPassword);
$token = $this->createToken($user);
return $this->returnUserWithToken($user, $token);
}
public function createToken($user)
{
return $this->userRepository->createToken($user);
}
public function validateUser($user, $isCorrectPassword)
{
if (!$user || !$isCorrectPassword) throw new AuthenticationException();
}
public function returnUserWithToken($user, $token)
{
$res = [
'data' => new UserResource($user),
'token' => $token
];
return $res;
}
public function logoutUser(Request $request)
{
try
{
$this->userRepository->deleteToken($request);
return $res = ['message' => 'Wylogowanie przebiegło pomyślnie!'];
}
catch(Exception $e)
{
throw $e;
}
}
}
UserRepository.php
<?php
namespace App\Repositories;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class UserRepository {
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function findByEmail(string $email)
{
return $this->user::where('email', $email)->first();
}
public function comparePassword(string $password, User $user)
{
return Hash::check($password, $user->password);
}
public function createToken(User $user)
{
return $user->createToken('token')->plainTextToken;
}
public function deleteToken(Request $request)
{
$request->user()->tokens()->delete();
}
}
User.php (model)
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function product()
{
return $this->hasMany(Product::class);
}
}
When i write wrong password or email i have this Postman screen2
This message is in Polish "Wrong email or password"
I dont know when i have mistakes... In another project i have similar method and it's works. I use sanctum.