Hello,

I have a strange problem with StatelessForms in combination with login in:
On my MainPage are two stateless forms. One for registration and one for the
login (registration isn't implemented yet). When I try to log in, Wicket
creates a new User() and saves it in the Session (I made this like it's
explained in Wicket in Action). A System.out.print("user created") in the
constructor of User confirms that. Everything seems to be ok. But when the
main page reloads, I am still logged out. 
And: When I change from StatelessForm to Form, everything works perfectly.
Something else might be important: When I request a page that requries
authorization and I'm not logged in, i'm being redirected to /login. There
is the same LoginPanel (with StatelessForm) as on the main page - and the
login works! But when I directly open /login it's the same as on the main
page and I can't login.
I also noticed that the login works (with StatelessForm) when I open a page
that requires authorization, then press the back button (back to main page)
and there try to log in.

The question is now, what am I doing worng? I can't find the problem in my
code, I have been trying a lot to find out what's worng but everything seems
to be correct. I couldn't find anything in the mailing list / on the web, I
searched a lot.

At the end I attached code from LoginPanel.java, WiaSession.java and
WicketApplication.java.

Thank you very much for your help!
(And sorry if there are one or two missspelled words - I don't speak english
natively.)


René

LoginPanel.java:
public class LoginPanel extends Panel {

    public LoginPanel(String id) {
        super(id);
        add(new LoginForm("login"));
    }

    private class LoginForm extends StatelessForm {

        private String username;
        private String password;

        public LoginForm(String id) {
            super(id);

            setModel(new CompoundPropertyModel(this));
            add(new TextField("username"));
            add(new PasswordTextField("password"));
        }

        @Override
        public final void onSubmit() {
            if (tryToLogIn()) {
                if (!continueToOriginalDestination()) {
                    setResponsePage(getApplication().getHomePage());
                }
            }
        }

        private boolean tryToLogIn() {
            if (username != null && password != null) {
                User user = Database.findUser(username);
                if (user != null) {
                    if (user.comparePasswords(password)) {
                        WiaSession.get().setUser(user);
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

WiaSession.java:
public final class WiaSession extends WebSession {

    private User user;

    public WiaSession(Request request) {
        super(request);
    }

    public static WiaSession get() {
        return (WiaSession) Session.get();
    }
    
    public static boolean isLoggedIn() {
        return WiaSession.get().isAuthenticated();
    }

    public boolean isAuthenticated() {
        return (user != null);
    }

    public final synchronized User getUser() {
        return user;
    }

    public final synchronized void setUser(User user) {
        this.user = user;
    }
}

WicketApplication.java, newSession overwritten:
    @Override
    public final Session newSession(Request request, Response response) {
        return new WiaSession(request);
    }

--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/StatelessForm-Cannot-login-tp4373476p4373476.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to