Login /Logout user sessin example, use of vaadinServlet if needed

Hi

sory, I don’t get it. How to manage vaadin session.(login, logout, ect)

I want to login with window pop form. I set the session user, close the form and read the it again in my main form.
But the problem is that vaadin says my UI not to be atached to any session!?.
I have implemented SessionInitListener, SessionDestroyListener in my UI.

I don’t know when or how to use VaadinServlet. How or when VaadinServlet calls UIs Init method?

When I logout, nothing happends. 2nd click to the Logout Buton cause vaadin “session expire message”.
I want to know how to cause this event on my self and be able to redirect the application flow back to login/star form.
In this proyect I don’t use Navigator.

Thanks!

Aleksander

Not sure if it’s the correct way but in my logout, I do

               Page.getCurrent()
                  .setLocation(VaadinServlet.getCurrent().getServletConfig().getServletContext().getContextPath());
               VaadinSession.getCurrent().close();

and in my main UI I have a

      getNavigator().addViewChangeListener(new ViewChangeListener()
      {
         @Override
         public boolean beforeViewChange(ViewChangeEvent event)
         {
            boolean loggedIn= // check for session login info
            boolean loginView = event.getNewView() instanceof LoginViewView;

            if (!loggedIn && !loginView)
            {
               getNavigator().navigateTo(LoginView.NAME);
               return false;
            } else if (loggedIn && loginView)
            {
               return true;
            }
            return true;
         }

         @Override
         public void afterViewChange(ViewChangeEvent event)
         {
         }
      });

Hi Nicklas,

thank you for the answer.
I don’t use navigator in this aplication because is too small, but I will try your version of logout.

Aleksander

Hi Nicklas,

your peace of code worked for me. Now I would lile to know how do you do the login?

I use for this a Window type of form, and in my main UI init method I ask for a session or user.
I ther are null I invoke the login form. I don’t know if this is the correct way, but for now works.

Thanks again
Aleksander

Well, the line

            boolean loggedIn= // check for session login info

is actually a check in the VaadinSession for precense of my “Session” object under a known key. This object is placed in the login after a successful login. This way, every time a navigation is done, the login validation is checked (unless you are navigating to the login page) and you are tossed to the login page if you’re not logged in.

The login itself is a LoginRequest object with username and password which i Binder and connect to a FormLayout. The object is JAX-RS jsonified and passed on the service layer etc