3 Replies Latest reply on Dec 27, 2008 8:27 AM by kingsob.derek.protal.com

    login after registration

    kingsob.derek.protal.com

      in my EJB after creating a new used, i have the following code


      identity.setUsername(user.getUsername());
      identity.setPassword(user.getPassword());
      identity.login();


      System.out.println(identity.isLoggedIn());



      in the logs it is prining false, and i get a Login failed message


      i'm not sure if this is suposto call my authenticator, but it is not...


      im delpoying on glassfish if this matters



      Cheers,
          Derek Knapp

        • 1. Re: login after registration
          shane.bryzak

          Where are you getting the identity instance from? Could you post all your code?

          • 2. Re: login after registration
            dstas

            did you tried
            identity.getCredentials().setUsername()   ?

            • 3. Re: login after registration
              kingsob.derek.protal.com

              here is all my code




              package wyd.beans;
              
              import java.util.List;
              import javax.annotation.Resource;
              import javax.ejb.Remove;
              import javax.ejb.SessionContext;
              import javax.ejb.Stateful;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
              import org.hibernate.validator.InvalidStateException;
              import org.hibernate.validator.InvalidValue;
              import org.jboss.seam.annotations.Begin;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Out;
              import org.jboss.seam.bpm.Actor;
              import org.jboss.seam.contexts.Context;
              import org.jboss.seam.faces.FacesMessages;
              import org.jboss.seam.security.Identity;
              import wyd.entity.User;
              
              /**
               *
               * @author Derek Knapp
               */
              @Stateful
              @Name("userAction")
              public class UserFacade implements UserFacadeLocal
              {
                  @PersistenceContext
                  EntityManager em;
              
                  @Resource
                  SessionContext ctx;
              
                  @In
                  Context sessionContext;
              
                  @In(create=true)
                  @Out
                  User user;
              
                  @In
                  FacesMessages facesMessages;
              
                  @In Identity identity;
              
                  String password = null;
              
                  public void setPasswordVerify(String password)
                  {
                      this.password = password;
                  }
              
                  public String getPasswordVerify()
                  {
                      return password;
                  }
              
                  @Begin(nested=true, pageflow="newuser")
                  public void startEdit()
                  {
                  }
              
                  public boolean isValidNamePassword()
                  {
                      boolean ok = true;
                      if (!isUniqueName())
                      {
                          facesMessages.add("userName", "This name is already in use");
                          ok = false;
                      }
                      if (!isPasswordsMatch())
                      {
                          facesMessages.add("passwordVerify", "Must match password field");
                          ok = false;
                      }
                      return ok;
                  }
              
                  @SuppressWarnings("unchecked")
                  private boolean isUniqueName()
                  {
                      String username = user.getUsername();
                      if (username == null) return true;
              
                      List<User> results = em.createQuery("select u from User u where u.username = :username")
                          .setParameter("username", username)
                          .getResultList();
              
                      return results.size() == 0;
                  }
              
                  private boolean isPasswordsMatch()
                  {
                      String userpassword = user.getPassword();
                      return (password != null) && (userpassword != null) && (userpassword.equals(password));
                  }
              
                  public String saveUser()
                  {
                      if (!isValidNamePassword())
                      {
                          facesMessages.add("User name #{user.username} is not unique");
                          return null;
                      }
              
                      try
                      {
                          em.persist(user);
                          sessionContext.set("currentUser", user);
                          Actor.instance().setId(user.getUsername());
              
                          identity.setUsername(user.getUsername());
                          identity.setPassword(user.getPassword());
                          identity.login();
              
              
                          System.out.println(identity.getUsername());
                          System.out.println(identity.isLoggedIn());
                          System.out.println(identity);
                          
                          facesMessages.addFromResourceBundle("createUserSuccess");
                          return "success";
                      }
                      catch (InvalidStateException e)
                      {e.printStackTrace();
                          InvalidValue[] vals = e.getInvalidValues();
                          for (InvalidValue val: vals)
                          {
                              facesMessages.add(val);
                          }
              
                          return null;
                      }
                      catch (RuntimeException e)
                      {e.printStackTrace();
                          ctx.setRollbackOnly();
              
                          facesMessages.addFromResourceBundle("createUserError");
              
                          return null;
                      }
                  }
              
                  @Remove
                  public void destroy() {}
              }