vaadin / vaadin/flow-crm-tutorial

Not able to redirect to desired View

Open
#264 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting for author
Dominant language
Java
Stars
204
Forks
194
Avg merge
55m
Merged PRs (30d)
2

Description

I tried as per the tutorial instructions on how to embed LoginForm in custom LoginView, despite all the configurations I set, but I am not able to redirect to the desired view , in my case it was serving on route => "home", instead it returns back to the login view.

My Security Config file -

@EnableWebSecurity`
@Configuration
public class SecurityConfig extends VaadinWebSecurity {
   
    @Autowired
    private GetUserUseCase userUseCase;

    @Bean
    public UserDetailsService users() {
        List<Users> validUsers = userUseCase.getUsers();
        Collection<UserDetails> userDetails = new ArrayList<>();
        validUsers.forEach((posUser) -> {
            String username = String.valueOf(posUser.getUserId());
            String password = posUser.getUserPass();
            String role = posUser.getUserType().getPName();

            UserDetails user = User.builder()
                    .username(username)
                    .password(password)
                    .roles(role)
                    .build();
            userDetails.add(user);
        });
        return new InMemoryUserDetailsManager(userDetails);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        setLoginView(http, LoginView.class);
    }

    @Override
    protected void configure(WebSecurity web) throws Exception {
        super.configure(web);
        web.ignoring()
                .requestMatchers("/**")
                .requestMatchers("/VAADIN")
                .requestMatchers("/VAADIN/**");
    }
}

My LoginView

@Route("login")
@PageTitle("CloudPOS")
@AnonymousAllowed
public class LoginView extends VerticalLayout implements BeforeEnterObserver {

    private final LoginForm login = new LoginForm();
    private final AuthService authService;

    public LoginView(@Autowired AuthService authService) {
        this.authService = authService;
        addClassName("login-view");
        setSizeFull();
        setAlignItems(Alignment.CENTER);
        setJustifyContentMode(JustifyContentMode.CENTER);

        login.setAction("login");
        login.addLoginListener(this::authenticateAndRedirect);

        add(new H1("CloudPOS"), login);
    }

    private void authenticateAndRedirect(AbstractLogin.LoginEvent event) {
        boolean isAuthenticated = authService.isAuthenticated(event.getUsername(), event.getPassword());
        if (isAuthenticated) {
            UI.getCurrent().navigate(PageRoutes.HOME_VIEW_ROUTE); // should navigate to "/home" after login success
        } else {
            login.setError(true);
        }
    }

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        if (
                beforeEnterEvent.getLocation()
                        .getQueryParameters()
                        .getParameters()
                        .containsKey("error")) {
            login.setError(true);
        }
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with SecurityConfig and LoginView, especially configure(HttpSecurity), setLoginView, and authenticateAndRedirect. Reproduce a successful login and trace whether Spring Security or the custom authentication flow sends the user back to login. Done means valid credentials reach the /home route without returning to the login view.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
authentication
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.