Skip to content

Commit

Permalink
Add request validation in UserRequest and UserController
Browse files Browse the repository at this point in the history
Added 'authorize' and 'rules' functions in UserRequest for validation of 'username' and 'password'. Updated 'login' function in UserController to implement this validation. This ensures that the username and password fields are not empty, improving the robustness of the application.
  • Loading branch information
AuroraYolo committed Aug 25, 2023
1 parent fee8029 commit f9cb814
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/Controller/Sys/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace App\Controller\Sys;

use App\Controller\AbstractController;
use App\Request\UserRequest;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Http\Message\ResponseInterface;
use Throwable;

#[Controller(prefix: 'sys/user')]
class UserController extends AbstractController
Expand All @@ -24,8 +27,15 @@ public function register()
}

#[PostMapping(path: 'signIn')]
public function login()
public function login(UserRequest $request): ResponseInterface
{
try {
$this->request->input('username');
$this->request->input('password');
return $this->response->success();
} catch (Throwable $e) {
}
return $this->response->fail();
}

#[PostMapping(path: 'signOut')]
Expand Down
12 changes: 12 additions & 0 deletions app/Request/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@

class UserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

public function rules()
{
return [
'username' => 'required',
'password' => 'required',
];
}
}

0 comments on commit f9cb814

Please sign in to comment.