0

I am making security system using symfony4 built-in security system.

I integerate registration form on index page.

And handle registration in function index

However after finishing registration, user needs to type username and passwrd again.

I want to skip this process.

After registration, User don't need to type username and password again.

How can I solve this??

public function index(Request $request, UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->commonFunc = $commonFunc;

        $this->data['user'] = $this->getUser();

        if (!$this->data['user']){// make registration form when no login
            $user = new User();
            $form = $this->createForm(UserType::class, $user);

            $form->handleRequest($request);

            if ($form->isSubmitted() && $form->isValid()) {

                $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
                $user->setPassword($password);

                // save the User!
                $entityManager = $this->getDoctrine()->getManager();
                $entityManager->persist($user);
                $entityManager->flush();
                $this->data['user'] = $user;

                // After registration process. the user must input id and pass again.
                // I want to skip this.


                return $this->redirectToRoute('index');

            }
            $this->data['form'] = $form->createView();
        }

        return $this->render('default/index.html.twig', [
            'controller_name' => 'DefaultController',
            'data' => $this->data
        ]);
    }

These are my final code from @Cerad suggestion.

I changed the way to get tokenStorage(my environment is 4.1) Somehow, I don't need to do event dispatch... However it works.

    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());

// $this->tokenStorage->setToken($token); $this->get('security.token_storage')->setToken($token);

whitebear
  • 11,200
  • 24
  • 114
  • 237
  • 1
    [How to programmatically login/authenticate a user?](https://stackoverflow.com/questions/9550079/how-to-programmatically-login-authenticate-a-user?noredirect=1&lq=1) or this [Automatic post-registration user authentication](https://stackoverflow.com/questions/5886713/automatic-post-registration-user-authentication) but there are also many other recent duplicate questions (_and less voted_) aroud here, just make a search. – gp_sflover Nov 25 '18 at 17:01

1 Answers1

3

Here is what I use:

private function loginUser(Request $request, UserInterface $user) : void
{
    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $this->tokenStorage->setToken($token);

    $event = new InteractiveLoginEvent($request, $token);
    $this->eventDispatcher->dispatch(SecurityEvents::INTERACTIVE_LOGIN, $event);
}

You can either inject the token storage and event dispatcher or pull them from the container.

Cerad
  • 48,157
  • 8
  • 90
  • 92