Automatic login after sign up(manual login)

After user registration I want the user to be automatically logged in. This what I got so far:

    private void doLogin(DuneUser user) {
        UserDetails userDetails = UserDetailsAdapter.adapt(user);
        Authentication authentication = UsernamePasswordAuthenticationToken.authenticated(
                userDetails,
                null,
                userDetails.getAuthorities()
        );

        SecurityContext context = SecurityContextHolder.createEmptyContext();
        context.setAuthentication(authentication);
        SecurityContextHolder.setContext(context);
        securityContextRepository.saveContext(context, VaadinServletRequest.getCurrent(), VaadinServletResponse.getCurrent());
        VaadinServletRequest.getCurrent().changeSessionId();
        UI.getCurrent().navigate("/");
    }

I avoided using the AuthenticationManager deliberately.

In my SecurityConfigration I registered the SecurityContextRepository as a bean and configured it in the filter chain.

I’m not sure if this is a proper implementation.

public void registerAndLogin(String username, String rawPassword) {
    // User speichern...
    
    // Login
    VaadinServletRequest request = VaadinServletRequest.getCurrent();
    VaadinServletResponse response = VaadinServletResponse.getCurrent();
    
    UsernamePasswordAuthenticationToken token = 
        new UsernamePasswordAuthenticationToken(username, rawPassword);
    
    Authentication auth = authenticationManager.authenticate(token);
    
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(auth);
    SecurityContextHolder.setContext(context);
    securityContextRepository.saveContext(context, request, response);
    
    // Zur Hauptseite navigieren
    UI.getCurrent().navigate(MainView.class);
}