public String loginAction() {
    	String returned = null;
    	FacesContext context = FacesContext.getCurrentInstance();
    
    	User user = userService.getUserByEmail(userName);
    	
    	if (user == null){
    		user = userService.getUserByUserName(userName);
    		if (user == null){
    			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Błąd logowania", "Użytkownik \"" + userName + "\" nie istnieje.")); 
    			logIn = false;
    			return returned;
    		}else{
    			logIn = true;
    		}
    	}else{
    		logIn = true;
    	}
    	
    	if (!user.getPassword().equals(stringUtils.getMD5Sum(password))){
    		context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Błąd logowania", "Podane hasło jest niepoprawne")); 
    		logIn = false;
    	}
    	
    	if (!user.getEnabled()){
    		context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Błąd logowania", "Użytkownik został zablokowany.")); 
    		logIn = false;
    	}
    	
    	
        if (logIn){
        	try {
                Authentication result = null;
                if (rememberMe) {                        
                    UserDetails userDetails = userDetailsService.loadUserByUsername(user.getEmail());
                    RememberMeAuthenticationToken rememberMeAuthenticationToken = new RememberMeAuthenticationToken("jsfspring-sec", userDetails,userDetails.getAuthorities());
                    HttpServletRequest httpServletRequest = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
                    HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                    rememberMeServices.loginSuccess(httpServletRequest, httpServletResponse, rememberMeAuthenticationToken);
                    result = rememberMeAuthenticationToken;
                    returned = "Protected";
                } else {
                    Authentication request = new UsernamePasswordAuthenticationToken(getUserName(), getPassword());
                    result = authenticationManager.authenticate(request);
                    returned = "Protected";
                }
                SecurityContextHolder.getContext().setAuthentication(result);
            } catch (AuthenticationException e) {
            	log.error("AuthenticationException" , e);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Błędne dane logowania.", ""));
            }
        	
            
            
        }else{
        	
        }
        
        return returned;
        
    }