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);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.