Files
backend/app/Http/Requests/Auth/LoginRequest.php

52 lines
1.4 KiB
PHP

<?php
namespace App\Http\Requests\Auth;
use App\Models\User;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return !User::query()->where('username', '=', $this->username)->value('is_online');
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'username' => 'required|exists:users,username',
'password' => 'required|string',
'telephone_id' => 'required|string'
];
}
public function after(): array
{
return [
function (Validator $validator) {
$user = User::query()->where('telephone_id', '=', $this->telephone_id);
if ($user->exists()) {
if ($user->first()->is_online) {
$validator->errors()->add(
'telephone_id',
__('messages.telephone_id_is_in_use')
);
}
$user->first()->update(['telephone_id' => null]);
}
}
];
}
}