Skip to content

Commit

Permalink
token for register
Browse files Browse the repository at this point in the history
  • Loading branch information
wouerner committed Jan 26, 2024
1 parent 21bd528 commit 9eecc6e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 47 deletions.
19 changes: 0 additions & 19 deletions app/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
52 changes: 39 additions & 13 deletions app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 8 additions & 13 deletions app/Repositories/LoginRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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(),
]);
Expand All @@ -72,5 +68,4 @@ public function register($request)
}
return false;
}

}
2 changes: 2 additions & 0 deletions app/Request/UserRegisterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}
Expand All @@ -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.',
];
}

Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
],
],
'jwt_secret_key' => env('JWT_SECRET_KEY', 'Hyperf Skeleton'),
'register_token' => env('REGISTER_TOKEN', 'Hyperf Skeleton'),
];
3 changes: 1 addition & 2 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});

0 comments on commit 9eecc6e

Please sign in to comment.