{"status":false,"message":"Invalid email or passwordt."}
{"email": "user@example.com", "password": "your_password", "device_name": "your_device_name"}{"status":false,"message":"Invalid email or password"}
Route::prefix('auth')->group(function () {
Route::post('/register', RegisterController::class);
Route::post('/login', LoginController::class);
Route::post('/logout', LogoutController::class);
});
class LoginController extends Controller
{
/**
* Login user by credentials
* @param LoginRequest $request
* @param AuthService $authService
* @return JsonResponse
* @throws LoginException
*/
public function __invoke(LoginRequest $request, AuthService $authService)
{
$user = $authService->login(
$request->validated('email'),
$request->validated('password'),
$request->validated('fcm_token')
);
return response()->json([
'status' => true,
'token' => $user->createToken($request->validated('device_name'))->plainTextToken,
'user' => new UserResource($user)
])->setStatusCode(Response::HTTP_OK);
}
/**
* Login user
* @param string $email
* @param string $password
* @param string|null $fcmToken
* @return User
* @throws LoginException
*/
public function login(string $email, string $password, ?string $fcmToken): User
{
/** @var User $user */
$user = User::query()->where('email', $email)->first();
if (!$user || !Hash::check($password, $user->password)) {
throw new LoginException(
'The provided credentials are incorrect.',
Response::HTTP_UNAUTHORIZED
);
}
$user->updateFcmToken($fcmToken);
return $user;
}