2

I have users getting auto-generated when they login via social media. I don't get their email so I want to make it that when they land on /whatever/page/after/login they see a screen that's like "Just enter email to continue on!"

I took a look at http://silex.sensiolabs.org/doc/cookbook/sub_requests.html and I'm either misreading or thinking I'd need to do it within a Silex\ControllerProviderInterface. I want this behavior to be for any request. Meanwhile if I make all my providers extend one, I'm not sure of the right way to cut out of a parent's connect without botching everything.

I also tried re-initializing everything similar to the answer here Unable to overwrite pathInfo in a Symfony 2 Request.

Here is the code I'm working with:

$app
->before(function (Request $request) use ($app) {
      $token = $app['security']->getToken();
      $app['user'] = null;

      if ($token && !$app['security.trust_resolver']->isAnonymous($token)) {
          $app['user'] = $token->getUser();


          if (!$app['user']->isVerified()) {
            $request->server->set('REQUEST_URI', '/signup');
            $request->initialize($request->query->all(), $request->request->all(), $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all(), $request->getContent());
          }
      }
    });
Community
  • 1
  • 1
Dave Stein
  • 8,653
  • 13
  • 56
  • 104

1 Answers1

1

I believe what you want to do is create a new Request object with the correct values and then tell that app to handle it.

If you don't care about preserving the request params from the original request then you can strip a lot of the extra stuff out.

$app
->before(function (Request $request) use ($app) {
      $token = $app['security']->getToken();
      $app['user'] = null;

      if ($token && !$app['security.trust_resolver']->isAnonymous($token)) {
          $app['user'] = $token->getUser();


          if (!$app['user']->isVerified()) {
            $subRequest = Request::create('/signup', 'GET', $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all(), $request->getContent());
            $subRequest->request = $request->request;
            $subRequest->query = $request->query;
            return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
          }
      }
    });
PetersenDidIt
  • 25,562
  • 3
  • 67
  • 72
  • There are some crazy things going on. It infinite loops so I had to add `if (!$app['user']->isVerified() && preg_match( '/signup/', $request->getRequestUri() ) === 0) {` because I couldn't figure out what the requestUri is at that point due to the infiniteness. I now also have a weird 500 but that could be my fault. I'll have to tweak when it's not midnight and then mark yours as good ;) – Dave Stein Aug 17 '15 at 03:55