Question

I'm working with the Silex (micro framework based on Symfony2).

When a session expires (like for 2 hours no activity) and I refresh my page I always get the error that he can't find the attribute displayName on a null attribute. Because in my layout view I have:
{{ app.user.displayName }}. (That's logical)

Now I want to redirect to the login page when a user is not logged in. I'm doing this in $app->before like this:

$app->before(function (Request $request) use($app) {
    if ($request->getMethod() === "OPTIONS") {
        $response = new Response();
        $response->headers->set("Access-Control-Allow-Origin","*");
        $response->headers->set("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
        $response->headers->set("Access-Control-Allow-Headers","Content-Type");
        $response->headers->set("Access-Control-Expose-Headers","handshake");
        $response->setStatusCode(200);
        $response->send();
    }

    if ($app['security']->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
        return $app->redirect('/login');
    }
});

But when I do this I always get the error: ERR_TOO_MANY_REDIRECTS . But how can I fix this?

Was it helpful?

Solution

Created a controle to check if you're on the login page. If not -> redirect to the login page.

if ($app['security']->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
    $routeName = $request->get('_route');
    if($routeName != 'user.login')
    {
        return $app->redirect('/');
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top