Login automatically after registration












-1















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);










share|improve this question

























  • How to programmatically login/authenticate a user? or this 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















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);










share|improve this question

























  • How to programmatically login/authenticate a user? or this 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












-1








-1


1






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);










share|improve this question
















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);







php symfony security symfony4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 16:56







whitebear

















asked Nov 25 '18 at 15:02









whitebearwhitebear

2,828114390




2,828114390













  • How to programmatically login/authenticate a user? or this 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





















  • How to programmatically login/authenticate a user? or this 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



















How to programmatically login/authenticate a user? or this 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







How to programmatically login/authenticate a user? or this 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 Answer
1






active

oldest

votes


















2














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.






share|improve this answer
























  • Thank you very much. I made my code from your suggestion. I updated my article.

    – whitebear
    Nov 25 '18 at 16:54













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468782%2flogin-automatically-after-registration%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.






share|improve this answer
























  • Thank you very much. I made my code from your suggestion. I updated my article.

    – whitebear
    Nov 25 '18 at 16:54


















2














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.






share|improve this answer
























  • Thank you very much. I made my code from your suggestion. I updated my article.

    – whitebear
    Nov 25 '18 at 16:54
















2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 15:30









CeradCerad

35.6k76370




35.6k76370













  • Thank you very much. I made my code from your suggestion. I updated my article.

    – whitebear
    Nov 25 '18 at 16:54





















  • Thank you very much. I made my code from your suggestion. I updated my article.

    – whitebear
    Nov 25 '18 at 16:54



















Thank you very much. I made my code from your suggestion. I updated my article.

– whitebear
Nov 25 '18 at 16:54







Thank you very much. I made my code from your suggestion. I updated my article.

– whitebear
Nov 25 '18 at 16:54




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468782%2flogin-automatically-after-registration%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks