Hi,
I am using FOSUserBundle in my Synfony 2.7 web app. Everything works fine but I have one question:
Is it possible to re-use the confirmation link the users get via email to activate their account. When the link is used for the first time, the user account is created/activated and the user is logged in.
In the logs of my app I can observe several 404 error, that result in calls to /register/confirm/xyz
MESSAGE: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "The user with confirmation token "xyz" does not exist" at /.../my/page/root/vendor/friendsofsymfony/user-bundle/Controller/RegistrationController.php line 107
This is not suprising, since the confirmation token is removed from the users DB entry, once the account has been activated. However it seems, that some users try to re-use the link as shortcut to user profile. As for know the only get to the 404 error page. *_It would be great, if they would be redirected to login instead. *_
Is there some kind of build in function to achieve this, or do have to do it manually?
We have a workaround in our project where we have overwritten the FOS RegistrationController (via Bundle inheritance):
class RegistrationController extends \FOS\UserBundle\Controller\RegistrationController
{
/**
* Overwrite default FOS action with difference that we redirect to login page
* in case the token is expired/invalid.
*
* => if users click the confirm link twice...
*/
public function confirmAction(Request $request, $token)
{
try {
return parent::confirmAction($request, $token);
} catch (NotFoundHttpException $e) {
return $this->redirectToRoute('fos_user_security_login');
}
}
}
@dmaicher May you help me ? I have the same problem so I have created a file named RegistrationController.php with the content above, located in /src/AppBundle/Controller (using Symfony3.1.4) but it keeps running the FOS User Controller one and not mine what am I missing ?
@Laurent3170 Did you use _Bundle inheritance_ as suggested by dmaicher? This is probably a reference to this method: http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
I have also tried with the implementation at http://symfony.com/doc/current/bundles/inheritance.html with file /src/AppBundle/User/UserBundle.php
<?php
namespace AppBundle\User;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
and file /src/AppBundle/User/RegistrationController.php
<?php
namespace AppBundle\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Controller\RegistrationController as BaseController;
class RegistrationController extends BaseController
public function confirmAction(Request $request, $token)
{
try {
return parent::confirmAction($request, $token);
} catch (NotFoundHttpException $e) {
return $this->redirectToRoute('fos_user_security_login');
}
}
And I keep receiving the err404 message with token not found
Does it even get into your action? Btw there is an import missing for class NotFoundHttpException so it will never catch it :wink: And also there are some brackets missing? Copy&paste error I guess?
No it doesn't go to my action. I have added use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; Thanks
As I can't manage to override FOSUser Controller, my only way is to write directly inside FOSUSer Controller... :-(
@Laurent3170 I guess something is weird with your namespaces & naming :wink:
Maybe check http://symfony.com/doc/current/bundles/best_practices.html
The Controller should for example live inside YourBundle/Controller.
And if it does not work then I would suggest to post it on stackoverflow as its not really the scope of this issue here :wink:
As the answer is short (and may help people coming here) the problem was that the Overriding Bundle has to be declared in AppKernel.php. As it's a Bundle I should have think of it, but it may also be mentioned in the docs... As we follow them like a recipe.
Good stuff, this helped me. One slight correction to make to @Laurent3170 code above. In the file "src/AppBundle/User/UserBundle.php":
namespace AppBundle\User;
should be
namespace AppBundle\User\Controller;
Cheers
:+1:
Most helpful comment
We have a workaround in our project where we have overwritten the FOS RegistrationController (via Bundle inheritance):