You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.9 KiB
133 lines
3.9 KiB
package api.menu.playa.controller;
|
|
|
|
import java.util.Collections;
|
|
|
|
import javax.enterprise.context.RequestScoped;
|
|
import javax.inject.Inject;
|
|
import javax.ws.rs.Consumes;
|
|
import javax.ws.rs.GET;
|
|
import javax.ws.rs.POST;
|
|
import javax.ws.rs.Path;
|
|
import javax.ws.rs.Produces;
|
|
import javax.ws.rs.core.MediaType;
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import org.eclipse.microprofile.jwt.JsonWebToken;
|
|
import org.jboss.logging.Logger;
|
|
import org.jboss.resteasy.reactive.RestHeader;
|
|
|
|
import api.menu.playa.enums.RolesEnum;
|
|
import api.menu.playa.exceptions.NegocioException;
|
|
import api.menu.playa.helper.LoginHelper;
|
|
import api.menu.playa.vo.RegistroVO;
|
|
import api.menu.playa.vo.ResponseGlobal;
|
|
import api.menu.playa.vo.UsuarioVO;
|
|
|
|
@RequestScoped
|
|
@Path("/user")
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
public class LoginController {
|
|
|
|
private static final String SUCCESS = "SUCCESS";
|
|
private static final String ERROR = "ERROR";
|
|
|
|
@Inject
|
|
Logger logger;
|
|
|
|
@Inject
|
|
JsonWebToken jwt;
|
|
|
|
@Inject
|
|
LoginHelper loginHelper;
|
|
|
|
@POST
|
|
@Path("login")
|
|
public Response login(UsuarioVO request) {
|
|
|
|
try {
|
|
String token = loginHelper.validarUsuario(request);
|
|
return Response.ok(new ResponseGlobal<>(0, SUCCESS, token)).build();
|
|
} catch (NegocioException e) {
|
|
return Response.ok(new ResponseGlobal<>(e.getCode(), e.getMessage())).build();
|
|
} catch (Exception e) {
|
|
logger.error(e);
|
|
return Response.ok(new ResponseGlobal<>(1, ERROR)).build();
|
|
}
|
|
}
|
|
|
|
@POST
|
|
@Path("register")
|
|
public Response register(RegistroVO request) {
|
|
|
|
try {
|
|
validacionPassword(request);
|
|
|
|
loginHelper.registrarUsuario(request);
|
|
|
|
return Response.ok(new ResponseGlobal<>(0, SUCCESS)).build();
|
|
} catch (NegocioException e) {
|
|
return Response.ok(new ResponseGlobal<>(e.getCode(), e.getMessage())).build();
|
|
} catch (Exception e) {
|
|
return Response.ok(new ResponseGlobal<>(1, ERROR)).build();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@GET
|
|
@Path("token")
|
|
public Response token() {
|
|
|
|
try {
|
|
|
|
|
|
String token = loginHelper.tokenGenerator("darroyo", Collections.singleton(RolesEnum.USER), 3600L, "www.vodorod.cl");
|
|
|
|
return Response.ok(new ResponseGlobal<>(0, SUCCESS, token)).build();
|
|
} catch (NegocioException e) {
|
|
return Response.ok(new ResponseGlobal<>(e.getCode(), e.getMessage())).build();
|
|
} catch (Exception e) {
|
|
return Response.ok(new ResponseGlobal<>(1, ERROR)).build();
|
|
}
|
|
|
|
}
|
|
|
|
/* @GET
|
|
@Path("valida")
|
|
public Response validaToken(@RestHeader("Authorization") String token) {
|
|
|
|
try {
|
|
|
|
|
|
String user = loginHelper.validaToken(token);
|
|
|
|
return Response.ok(new ResponseGlobal<>(0, SUCCESS, user)).build();
|
|
|
|
} catch (Exception e) {
|
|
return Response.ok(new ResponseGlobal<>(1, ERROR)).build();
|
|
}
|
|
|
|
} */
|
|
|
|
private void validacionPassword(RegistroVO request) throws NegocioException {
|
|
|
|
if (request.getUser() == null || request.getUser().isEmpty()) {
|
|
throw new NegocioException("el campo usuario debe ser ingresado", 2);
|
|
}
|
|
if (request.getNombre() == null || request.getNombre().isEmpty()) {
|
|
throw new NegocioException("el campo nombre debe ser ingresado", 2);
|
|
}
|
|
if (request.getPass() == null || request.getPass().isEmpty()) {
|
|
throw new NegocioException("el campo contreseña debe ser ingresado", 2);
|
|
}
|
|
if (request.getPass2() == null || request.getPass2().isEmpty()) {
|
|
throw new NegocioException("el campo confirmar contraseña debe ser ingresado", 2);
|
|
}
|
|
if (!request.getPass().equals(request.getPass2())) {
|
|
throw new NegocioException("los campos de contraseña no son iguales", 2);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|