From 9eecc6e1527301c8f4f4ebcae8414755fd9c70c0 Mon Sep 17 00:00:00 2001 From: wouerner3 Date: Fri, 26 Jan 2024 18:21:03 -0300 Subject: [PATCH] token for register --- app/Controller/AuthController.php | 19 ---------- app/Controller/UserController.php | 52 +++++++++++++++++++++------- app/Repositories/LoginRepository.php | 21 +++++------ app/Request/UserRegisterRequest.php | 2 ++ config/config.php | 1 + config/routes.php | 3 +- 6 files changed, 51 insertions(+), 47 deletions(-) diff --git a/app/Controller/AuthController.php b/app/Controller/AuthController.php index f17d412..248a213 100644 --- a/app/Controller/AuthController.php +++ b/app/Controller/AuthController.php @@ -18,34 +18,15 @@ class AuthController extends AbstractController { private $loginRepository; - /* protected $response; */ public function __construct( LoginRepository $loginRepository, - /* LoginRepositoryInterface $loginRepository, */ - /* ResponseInterface $response */ ) { $this->loginRepository = $loginRepository; - /* $this->response = $response; */ } public function login(LoginRequest $request) { return $this->loginRepository->login($request); } - - public function register(UserRegisterRequest $request) - { - $result = $this->loginRepository->register($request); - - if ($result) { - return $this->response->json([ - 'message' => 'Usuário cadastrado com sucesso.' - ])->withStatus(201); - } else { - return $this->response->json([ - 'error' => 'Não foi possível realizar o cadastro.' - ])->withStatus(500); - } - } } diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index 4068e5c..73142e2 100644 --- a/app/Controller/UserController.php +++ b/app/Controller/UserController.php @@ -15,33 +15,59 @@ use App\Model\User; use Hyperf\HttpServer\Contract\RequestInterface; use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface; +use App\Request\UserRegisterRequest; +use App\Repositories\LoginRepository; +use Hyperf\Config\Annotation\Value; -class UserController extends AbstractController +final class UserController extends AbstractController { + private $loginRepository; + + #[Value(key: "register_token")] + private $registerToken; + + public function __construct( + LoginRepository $loginRepository, + ) { + $this->loginRepository = $loginRepository; + } + public function index() { return User::select( 'uuid', 'name', 'email', + 'birth_date', + 'document', + 'cellphone', + 'linkedin', 'created_at', 'updated_at' )->get(); } - /** - * - * @Todo - */ - public function create() + public function create(UserRegisterRequest $request) { - return [ - "id" => 1, - "linkedin" => "https://picsum.photos/200/300", - "description" => "Product 1 description", - "image" => "https://picsum.photos/200/300", - "token" => "token" - ]; + $token = $this->request->input('register_token'); + + if ($token !== $this->registerToken) { + return $this->response->json([ + 'error' => 'Token inválido.', + ], 403); + } + + $result = $this->loginRepository->register($request); + + if ($result) { + return $this->response->json([ + 'message' => 'Usuário cadastrado com sucesso.' + ])->withStatus(201); + } else { + return $this->response->json([ + 'error' => 'Não foi possível realizar o cadastro.' + ])->withStatus(500); + } } public function update(RequestInterface $request, $id) diff --git a/app/Repositories/LoginRepository.php b/app/Repositories/LoginRepository.php index 2a4f8d3..16e0b73 100644 --- a/app/Repositories/LoginRepository.php +++ b/app/Repositories/LoginRepository.php @@ -16,12 +16,6 @@ class LoginRepository implements LoginRepositoryInterface #[Value(key: "jwt_secret_key")] protected $jwtSecretKey; - public function __construct() - { - /* $this->jwtSecretKey = env('JWT_SECRET_KEY'); */ - var_dump($this->jwtSecretKey); - } - public function login($request) { $email = $request->input('email'); @@ -57,12 +51,14 @@ public function register($request) { $user = User::create([ 'uuid' => Uuid::uuid4()->toString(), - 'name' => $request->input('name'), - 'email' => $request->input('email'), - 'birth_date' => $request->input('birth_date'), - 'document' => $request->input('document'), - 'cellphone' => $request->input('cellphone'), - 'password' => password_hash($request->input('password'), PASSWORD_BCRYPT), + 'name' => $request->input('name'), + 'email' => $request->input('email'), + 'birth_date' => $request->input('birth_date'), + 'document' => $request->input('document'), + 'cellphone' => $request->input('cellphone'), + 'linkedin' => $request->input('linkedin'), + 'permission' => 'founder', + 'password' => password_hash($request->input('password'), PASSWORD_BCRYPT), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), ]); @@ -72,5 +68,4 @@ public function register($request) } return false; } - } diff --git a/app/Request/UserRegisterRequest.php b/app/Request/UserRegisterRequest.php index ce71fb9..4a72269 100644 --- a/app/Request/UserRegisterRequest.php +++ b/app/Request/UserRegisterRequest.php @@ -21,6 +21,7 @@ public function rules():array 'birth_date' => 'required|date', 'document' => 'required|string|max:20|unique:users', 'cellphone' => 'required|string|max:20|unique:users', + 'register_token' => 'required|string', 'password' => 'required|string|min:8', ]; } @@ -47,6 +48,7 @@ public function messages():array 'password.required' => 'O campo senha é obrigatório.', 'password.string' => 'O campo senha deve ser uma string.', 'password.min' => 'O campo senha deve ter pelo menos :min caracteres.', + 'register_token.required' => 'O campo register_token é obrigatório.', ]; } diff --git a/config/config.php b/config/config.php index c446360..aab9423 100644 --- a/config/config.php +++ b/config/config.php @@ -31,4 +31,5 @@ ], ], 'jwt_secret_key' => env('JWT_SECRET_KEY', 'Hyperf Skeleton'), + 'register_token' => env('REGISTER_TOKEN', 'Hyperf Skeleton'), ]; diff --git a/config/routes.php b/config/routes.php index 39c4f10..5d30bb7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -19,7 +19,6 @@ function () { // user Router::addRoute(['GET'], '/users', 'App\Controller\UserController@index'); - Router::addRoute(['POST'], '/user', 'App\Controller\UserController@create'); Router::addRoute(['PUT'], '/user/{id}', 'App\Controller\UserController@update'); Router::addRoute(['DELETE'], '/user/{id}', 'App\Controller\UserController@del'); @@ -44,5 +43,5 @@ function () { Router::addGroup('/api', function () { Router::post('/login', 'App\Controller\AuthController@login'); - Router::post('/register', 'App\Controller\AuthController@register'); + Router::addRoute(['POST'], '/user', 'App\Controller\UserController@create'); });