Authenticating Vaadin-based applications

Hi,

I am trying to implement this authorization in my application, using HttpServletRequestListener instead of TransactionListener, following this Vaadin Wiki Page:
Authenticating Vaadin-based applications

Here is the code for Application class methods authenticate() and loadProtectedResources():

	public void authenticate(String login, String password) throws Exception 
	{	
		//Validation
		if(login.equals("admin") && password.equals("admin")) {
			currentUser = login;
			loadProtectedResources();
			return;
		}
		throw new Exception("Invalid username or password");
	}
  
    private void loadProtectedResources()
    {
    	setMainWindow(new InitialView());
	}

Login button listener:

	public void buttonClick(ClickEvent event) 
	{
		final Button source = event.getButton();

		if(source == btnLogin) {
			
			try {
				App.getInstance().authenticate(login.getValue().toString(), password.getValue().toString());
				open(new ExternalResource(App.getInstance().getURL()));
			}
			catch (Exception e) {
				showNotification("Login error", "<br>" + e.getMessage(), Notification.TYPE_ERROR_MESSAGE);
			}
		}
	}

The problem occurs when the user clicks on the Login button: the page is refreshed with the same login page, instead of loading the InitialView class, which is created but is not shown, remaining in background…

Am I missing something?

Thanks in advance…

Changing the main window on the fly can be a little bit tricky. How about going around the problem and change the main layout instead of the window? Make InitialView extend the layout that the window uses, instead of Window, and use getMainWindow().setContent(new InitialView()) instead of setMainWindow(new InitialView());

Hi Jens, thanks for the answer…

I managed to solve this issue by removing the main window and setting the InitialView window as the new main window.

    private void loadProtectedResources()
    {
    	InitialView initialview = new InitialView();

    	App.getInstance().removeWindow(App.getInstance().getMainWindow());
    	
    	App.getInstance().setMainWindow(initialview);
	}