Symfony 3.4 not deleting the token after use

Multi tool use
Multi tool use












-1















Hey guys I am working on a resset password feature. The user cliks on a url received by mail and provides _password1 and _password2. If they match it saves it to the database. Problem is it does not delete the token from the database ... I tryed a few things but can't seem to get it right. I got no error messages.



Just a skip on the delete. I am new to symfony and this is my first delete attempt lol



/**
* @Route("/password-change")
*/
class ResettingController extends Controller
{

/**
* @Route("/{id}/{token}", name="resetting")
* @param ResettingController $event
*/
public function resetting(User $user, $token, Request $request)
{

// Find the token in the database
$tokendb = $this->getDoctrine()
->getRepository('AppBundle\Entity\PasswordToken'::class)
->findOneBy(array('token'=>$token));

//Redirect if token is not valid
if ($token == "" || !$tokendb || $token!=$tokendb->getToken())
{
$request->getSession()->getFlashBag()->add('error', "Lien d'accès invalide");
return $this->redirectToRoute('security_login');
}

// Building the form for the view
$form = $this->createFormBuilder()
->add('_password1', PasswordType::class, array("label"=>"Entrez un nouveau mot de passe:"))
->add('_password2', PasswordType::class, array("label"=>"Retapez ce même mot de passe:"))
->getForm();
$form->handleRequest($request);

// When form is submited
if($form->isSubmitted() && $form->isValid())
{

// Check if password 1 and 2 matches
if( $request->request->get('form')['_password1'] && $request->request->get('form')['_password1'] == $request->request->get('form')['_password2'])
{
// Save the new password
$user->setplainPassword( $request->request->get('form')['_password1'] );

// Delete the token
$em = $this->getDoctrine()->getEntityManager();
$em->remove($tokendb);

// success message and redirect
$request->getSession()->getFlashBag()->add('success', "Votre mot de passe a été modifié.");
return $this->redirectToRoute('security_login');
}else{
$request->getSession()->getFlashBag()->add('error', "Vos deux mot de passe ne correspondent pas.");
}
}

// Sending the view
return $this->render('Security/change-password.html.twig', [
'form' => $form->createView(),
'title' => "Changement de mot de passe",
'error' => $form->getErrors()
]);

}
}









share|improve this question




















  • 2





    Add $em->flush(); after remove

    – Felippe Duarte
    Dec 28 '18 at 21:29











  • as @FelippeDuarte it doesn't work because of no "flush" but why don't you use standard symfony's security?

    – Robert
    Dec 28 '18 at 21:32











  • I guess i got lost by reading so many tutorials that i probably ended up complicating it. But it works and I am not changing it into something else lol

    – Patrick Simard
    Dec 28 '18 at 21:35
















-1















Hey guys I am working on a resset password feature. The user cliks on a url received by mail and provides _password1 and _password2. If they match it saves it to the database. Problem is it does not delete the token from the database ... I tryed a few things but can't seem to get it right. I got no error messages.



Just a skip on the delete. I am new to symfony and this is my first delete attempt lol



/**
* @Route("/password-change")
*/
class ResettingController extends Controller
{

/**
* @Route("/{id}/{token}", name="resetting")
* @param ResettingController $event
*/
public function resetting(User $user, $token, Request $request)
{

// Find the token in the database
$tokendb = $this->getDoctrine()
->getRepository('AppBundle\Entity\PasswordToken'::class)
->findOneBy(array('token'=>$token));

//Redirect if token is not valid
if ($token == "" || !$tokendb || $token!=$tokendb->getToken())
{
$request->getSession()->getFlashBag()->add('error', "Lien d'accès invalide");
return $this->redirectToRoute('security_login');
}

// Building the form for the view
$form = $this->createFormBuilder()
->add('_password1', PasswordType::class, array("label"=>"Entrez un nouveau mot de passe:"))
->add('_password2', PasswordType::class, array("label"=>"Retapez ce même mot de passe:"))
->getForm();
$form->handleRequest($request);

// When form is submited
if($form->isSubmitted() && $form->isValid())
{

// Check if password 1 and 2 matches
if( $request->request->get('form')['_password1'] && $request->request->get('form')['_password1'] == $request->request->get('form')['_password2'])
{
// Save the new password
$user->setplainPassword( $request->request->get('form')['_password1'] );

// Delete the token
$em = $this->getDoctrine()->getEntityManager();
$em->remove($tokendb);

// success message and redirect
$request->getSession()->getFlashBag()->add('success', "Votre mot de passe a été modifié.");
return $this->redirectToRoute('security_login');
}else{
$request->getSession()->getFlashBag()->add('error', "Vos deux mot de passe ne correspondent pas.");
}
}

// Sending the view
return $this->render('Security/change-password.html.twig', [
'form' => $form->createView(),
'title' => "Changement de mot de passe",
'error' => $form->getErrors()
]);

}
}









share|improve this question




















  • 2





    Add $em->flush(); after remove

    – Felippe Duarte
    Dec 28 '18 at 21:29











  • as @FelippeDuarte it doesn't work because of no "flush" but why don't you use standard symfony's security?

    – Robert
    Dec 28 '18 at 21:32











  • I guess i got lost by reading so many tutorials that i probably ended up complicating it. But it works and I am not changing it into something else lol

    – Patrick Simard
    Dec 28 '18 at 21:35














-1












-1








-1








Hey guys I am working on a resset password feature. The user cliks on a url received by mail and provides _password1 and _password2. If they match it saves it to the database. Problem is it does not delete the token from the database ... I tryed a few things but can't seem to get it right. I got no error messages.



Just a skip on the delete. I am new to symfony and this is my first delete attempt lol



/**
* @Route("/password-change")
*/
class ResettingController extends Controller
{

/**
* @Route("/{id}/{token}", name="resetting")
* @param ResettingController $event
*/
public function resetting(User $user, $token, Request $request)
{

// Find the token in the database
$tokendb = $this->getDoctrine()
->getRepository('AppBundle\Entity\PasswordToken'::class)
->findOneBy(array('token'=>$token));

//Redirect if token is not valid
if ($token == "" || !$tokendb || $token!=$tokendb->getToken())
{
$request->getSession()->getFlashBag()->add('error', "Lien d'accès invalide");
return $this->redirectToRoute('security_login');
}

// Building the form for the view
$form = $this->createFormBuilder()
->add('_password1', PasswordType::class, array("label"=>"Entrez un nouveau mot de passe:"))
->add('_password2', PasswordType::class, array("label"=>"Retapez ce même mot de passe:"))
->getForm();
$form->handleRequest($request);

// When form is submited
if($form->isSubmitted() && $form->isValid())
{

// Check if password 1 and 2 matches
if( $request->request->get('form')['_password1'] && $request->request->get('form')['_password1'] == $request->request->get('form')['_password2'])
{
// Save the new password
$user->setplainPassword( $request->request->get('form')['_password1'] );

// Delete the token
$em = $this->getDoctrine()->getEntityManager();
$em->remove($tokendb);

// success message and redirect
$request->getSession()->getFlashBag()->add('success', "Votre mot de passe a été modifié.");
return $this->redirectToRoute('security_login');
}else{
$request->getSession()->getFlashBag()->add('error', "Vos deux mot de passe ne correspondent pas.");
}
}

// Sending the view
return $this->render('Security/change-password.html.twig', [
'form' => $form->createView(),
'title' => "Changement de mot de passe",
'error' => $form->getErrors()
]);

}
}









share|improve this question
















Hey guys I am working on a resset password feature. The user cliks on a url received by mail and provides _password1 and _password2. If they match it saves it to the database. Problem is it does not delete the token from the database ... I tryed a few things but can't seem to get it right. I got no error messages.



Just a skip on the delete. I am new to symfony and this is my first delete attempt lol



/**
* @Route("/password-change")
*/
class ResettingController extends Controller
{

/**
* @Route("/{id}/{token}", name="resetting")
* @param ResettingController $event
*/
public function resetting(User $user, $token, Request $request)
{

// Find the token in the database
$tokendb = $this->getDoctrine()
->getRepository('AppBundle\Entity\PasswordToken'::class)
->findOneBy(array('token'=>$token));

//Redirect if token is not valid
if ($token == "" || !$tokendb || $token!=$tokendb->getToken())
{
$request->getSession()->getFlashBag()->add('error', "Lien d'accès invalide");
return $this->redirectToRoute('security_login');
}

// Building the form for the view
$form = $this->createFormBuilder()
->add('_password1', PasswordType::class, array("label"=>"Entrez un nouveau mot de passe:"))
->add('_password2', PasswordType::class, array("label"=>"Retapez ce même mot de passe:"))
->getForm();
$form->handleRequest($request);

// When form is submited
if($form->isSubmitted() && $form->isValid())
{

// Check if password 1 and 2 matches
if( $request->request->get('form')['_password1'] && $request->request->get('form')['_password1'] == $request->request->get('form')['_password2'])
{
// Save the new password
$user->setplainPassword( $request->request->get('form')['_password1'] );

// Delete the token
$em = $this->getDoctrine()->getEntityManager();
$em->remove($tokendb);

// success message and redirect
$request->getSession()->getFlashBag()->add('success', "Votre mot de passe a été modifié.");
return $this->redirectToRoute('security_login');
}else{
$request->getSession()->getFlashBag()->add('error', "Vos deux mot de passe ne correspondent pas.");
}
}

// Sending the view
return $this->render('Security/change-password.html.twig', [
'form' => $form->createView(),
'title' => "Changement de mot de passe",
'error' => $form->getErrors()
]);

}
}






php symfony






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 21:26







Patrick Simard

















asked Dec 28 '18 at 21:20









Patrick SimardPatrick Simard

1,05911024




1,05911024








  • 2





    Add $em->flush(); after remove

    – Felippe Duarte
    Dec 28 '18 at 21:29











  • as @FelippeDuarte it doesn't work because of no "flush" but why don't you use standard symfony's security?

    – Robert
    Dec 28 '18 at 21:32











  • I guess i got lost by reading so many tutorials that i probably ended up complicating it. But it works and I am not changing it into something else lol

    – Patrick Simard
    Dec 28 '18 at 21:35














  • 2





    Add $em->flush(); after remove

    – Felippe Duarte
    Dec 28 '18 at 21:29











  • as @FelippeDuarte it doesn't work because of no "flush" but why don't you use standard symfony's security?

    – Robert
    Dec 28 '18 at 21:32











  • I guess i got lost by reading so many tutorials that i probably ended up complicating it. But it works and I am not changing it into something else lol

    – Patrick Simard
    Dec 28 '18 at 21:35








2




2





Add $em->flush(); after remove

– Felippe Duarte
Dec 28 '18 at 21:29





Add $em->flush(); after remove

– Felippe Duarte
Dec 28 '18 at 21:29













as @FelippeDuarte it doesn't work because of no "flush" but why don't you use standard symfony's security?

– Robert
Dec 28 '18 at 21:32





as @FelippeDuarte it doesn't work because of no "flush" but why don't you use standard symfony's security?

– Robert
Dec 28 '18 at 21:32













I guess i got lost by reading so many tutorials that i probably ended up complicating it. But it works and I am not changing it into something else lol

– Patrick Simard
Dec 28 '18 at 21:35





I guess i got lost by reading so many tutorials that i probably ended up complicating it. But it works and I am not changing it into something else lol

– Patrick Simard
Dec 28 '18 at 21:35












2 Answers
2






active

oldest

votes


















1














You need to add $em->flush(); after $em->remove($tokendb); in order to perform the operation in database.




flush()



Flushes all changes to objects that have been queued up to now to the database.



This effectively synchronizes the in-memory state of managed objects with the database.







share|improve this answer
























  • That did the trick! Thx man!

    – Patrick Simard
    Dec 28 '18 at 21:33



















0














Just a little remarks :



Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically






share|improve this answer























    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%2f53964399%2fsymfony-3-4-not-deleting-the-token-after-use%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to add $em->flush(); after $em->remove($tokendb); in order to perform the operation in database.




    flush()



    Flushes all changes to objects that have been queued up to now to the database.



    This effectively synchronizes the in-memory state of managed objects with the database.







    share|improve this answer
























    • That did the trick! Thx man!

      – Patrick Simard
      Dec 28 '18 at 21:33
















    1














    You need to add $em->flush(); after $em->remove($tokendb); in order to perform the operation in database.




    flush()



    Flushes all changes to objects that have been queued up to now to the database.



    This effectively synchronizes the in-memory state of managed objects with the database.







    share|improve this answer
























    • That did the trick! Thx man!

      – Patrick Simard
      Dec 28 '18 at 21:33














    1












    1








    1







    You need to add $em->flush(); after $em->remove($tokendb); in order to perform the operation in database.




    flush()



    Flushes all changes to objects that have been queued up to now to the database.



    This effectively synchronizes the in-memory state of managed objects with the database.







    share|improve this answer













    You need to add $em->flush(); after $em->remove($tokendb); in order to perform the operation in database.




    flush()



    Flushes all changes to objects that have been queued up to now to the database.



    This effectively synchronizes the in-memory state of managed objects with the database.








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 28 '18 at 21:32









    Felippe DuarteFelippe Duarte

    10.5k21524




    10.5k21524













    • That did the trick! Thx man!

      – Patrick Simard
      Dec 28 '18 at 21:33



















    • That did the trick! Thx man!

      – Patrick Simard
      Dec 28 '18 at 21:33

















    That did the trick! Thx man!

    – Patrick Simard
    Dec 28 '18 at 21:33





    That did the trick! Thx man!

    – Patrick Simard
    Dec 28 '18 at 21:33













    0














    Just a little remarks :



    Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically






    share|improve this answer




























      0














      Just a little remarks :



      Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically






      share|improve this answer


























        0












        0








        0







        Just a little remarks :



        Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically






        share|improve this answer













        Just a little remarks :



        Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 29 '18 at 19:36









        JessGabrielJessGabriel

        1194




        1194






























            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%2f53964399%2fsymfony-3-4-not-deleting-the-token-after-use%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







            LC2 Tj4hFtnm61qCSWi 0SeGFADub
            smXrJgNubVa,92cQZ 7AeC0 0SN jnVkQV,0HV6jJIMcG0wDPgT0JbEEG XUC P0pekDj1tJ0Y3GRasp IrTc,IiCwhYN,3NQa CT UUnc

            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas