I have a login screen where I authenticate a user, however when I put a action
to change the page independently whether the user was authenticated or not the page is opened.
My question is to know where I have to check whether the user is authenticated or not to redirect?
Button:
<f:facet name="footer">
<p:commandButton value="Entrar" update=":msgGlobal"
actionListener="#{usuarioBean.entrar}" action="principal?faces-redirect=true"/>
</f:facet>
Authentication method:
public void entrar() {
try {
UsuarioDAO usuarioDAO = new UsuarioDAO();
usuarioLogado = usuarioDAO.autenticar(usuarioLogado.getLogin(),
usuarioLogado.getSenha());
if (usuarioLogado == null) {
FacesUtil.adicionarMsgErro("Login ou Senha inválidos");
} else {
FacesUtil.adicionarMsgInfo("Usuario Autenticado com Sucesso");
}
} catch (RuntimeException ex) {
FacesUtil.adicionarMsgErro("Erro ao tentar entrar no Sistema");
}
}