Symfony 3.4 not deleting the token after use
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
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
add a comment |
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
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
add a comment |
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
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
php symfony
edited Dec 28 '18 at 21:26
Patrick Simard
asked Dec 28 '18 at 21:20
![](https://i.stack.imgur.com/fI9HN.jpg?s=32&g=1)
![](https://i.stack.imgur.com/fI9HN.jpg?s=32&g=1)
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
That did the trick! Thx man!
– Patrick Simard
Dec 28 '18 at 21:33
add a comment |
Just a little remarks :
Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
That did the trick! Thx man!
– Patrick Simard
Dec 28 '18 at 21:33
add a comment |
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.
That did the trick! Thx man!
– Patrick Simard
Dec 28 '18 at 21:33
add a comment |
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.
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.
answered Dec 28 '18 at 21:32
![](https://i.stack.imgur.com/Y2S7p.jpg?s=32&g=1)
![](https://i.stack.imgur.com/Y2S7p.jpg?s=32&g=1)
Felippe DuarteFelippe Duarte
10.5k21524
10.5k21524
That did the trick! Thx man!
– Patrick Simard
Dec 28 '18 at 21:33
add a comment |
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
add a comment |
Just a little remarks :
Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically
add a comment |
Just a little remarks :
Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically
add a comment |
Just a little remarks :
Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically
Just a little remarks :
Use RepeatedType instead of adding two fields to the formbuilder. It did the matching check automatically
answered Dec 29 '18 at 19:36
![](https://i.stack.imgur.com/bjKNH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/bjKNH.jpg?s=32&g=1)
JessGabrielJessGabriel
1194
1194
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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