Redirect to another page only if the user is authenticated

Asked

Viewed 1,173 times

5

I have a login screen where I authenticate a user, but when I put one action to switch page regardless of whether the user was authenticated or not the page opens.

My question is to know where I have to do the verification if the user is authenticated or not to redirect?

Knob:

<f:facet name="footer">
        <p:commandButton value="Entrar" update=":msgGlobal"
            actionListener="#{usuarioBean.entrar}" action="principal?faces-redirect=true"/>
</f:facet>

Method that authenticates:

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");
    }
}

1 answer

3


public void entrar() {
    try {
        UsuarioDAO usuarioDAO = new UsuarioDAO();
        usuarioLogado = usuarioDAO.autenticar(usuarioLogado.getLogin(),    usuarioLogado.getSenha());
        if (usuarioLogado == null) {
            FacesContext.getCurrentInstance().getExternalContext().redirect(/* url que vc quer*/);
        } else {
            FacesUtil.adicionarMsgInfo("Usuario Autenticado com Sucesso");
        }
    } catch (RuntimeException ex) {
        FacesContext.getCurrentInstance().getExternalContext().redirect(/* url que vc quer*/);
    }
}
  • Thank you very much. It worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.