-1

So as the title dictates, I'm trying to redirect the user based on his status but I'm facing difficulty in doing so. Here is what i'm doing:


In the submit form:


public  function submitForm(array &$form, FormStateInterface $form_state){
   .......
   $logged_user = json_decode($response);
   $_SESSION['loggedin'] = true;
   $_SESSION["jwt"] = $logged_user->token;
   $form_state->setRedirect('entity.node.canonical', ['node' => 1]);//After submit i redirect the  user to /node/1 which is intercepterd by the route defined in module.routing.yml
   ........
}

In my routing file:


user.home:
path: '/node/1'
defaults:
  _controller: '\Drupal\smeno\Controller\SmenoHomeController::index'
requirements:
  _permission: 'access content'

In my controller file:


 function index(){
  if (!(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true)) {
     return $this->redirect('<front>');
  }
 }

Now if the user is not authenticated and attempt to access /node/1 he is redirected to the page, it works fine, however, if the user is authenticated and tries to access /node/1 it gives me an error that there is no error in my controller which is logical because I haven't defined an else in the index() function where the user is already authenticated, then the user should get the page he is demanding. So i did the following:

 function index(){
  if (!(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true)) {
   return $this->redirect('<front>');
  }else{
   return $this->redirect('entity.node.canonical', ['node' => 1]);
  }
 }

but with this it puts me in an infinite loop of redirection which is comprehensible. when an authenticated user request /node/1 the route is activated and it sends him to the index function and the latter redirect him back to /node/1 and the route is activated again and so on.

Now the question is how can i let the authenticated user gets the page he is demanding??

  • What page is the authenticated user trying to get to? From reading your question, I can only assume there is one page node/1 and the authenticated user wants to get there and you are redirecting infinitely? What are you trying to accomplish? You have a form. On submit, you redirect to theoretically a node, but you've hijacked the node/1 route. If you've hijacked the node/1 route, why? Are anonymous user allowed to go to the actual node/1 at all? If not, then make an event subscriber to redirect anonymous users and get rid of the hijacked route. – mradcliffe Oct 24 '17 at 20:30
  • Yes, see drupal.stackexchange.com/a/223109/47547 – 4k4 Oct 24 '17 at 20:38
  • Actually i created a seperate login page that authenticate users to the site and i'm using sessions. so for the login i created a form and in its submit function i redirect the user ot his home page which is /node/1 and using the route i defined earlier i intercept users trying to access /node/1 in order to see which type of user is asking the home page, if its an anonymous i redirect him back to the login page which is working and if its authenticated user i let him pass (P.S. indeed i'm being redirected infinitely and there is only one page). – Abouhassane Abdelhamid Oct 24 '17 at 23:18
  • so as you assumed i hijacked the node/1 route in order to disallow anonymous users from accessing it. i'm not familiar with event subscribers! would this concept work for me knowing that i'm not using drupal users? for me when a user is authenticated i create a session for him where i store 2 variable: boolean(loggedin) && string(token.) – Abouhassane Abdelhamid Oct 24 '17 at 23:18
0

So, in the end, i wasn't able to accomplish this using the route and the controller, all my attempts have failed, but an idea of a friend helped me in solving this. well, the idea has nothing to do with routes and controllers, in fact, i removed the route as well as the controller. the idea is to create two blocks: "AuthSessionCheckBlock.php" is where i validate an anonymous user and "AnonymCheckBlock.php" is where i validate an authenticated user, as follow:

AuthSessionCheckBlock.php:

use Drupal\Core\Block\BlockBase;
use Symfony\Component\HttpFoundation\RedirectResponse;

class AuthSessionCheckBlock extends BlockBase {
    public function build() {
        if (!(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true)) {
             $response = new RedirectResponse(\Drupal::url('<front>'));
             $response->send();
             return;
        }
     }
}

AnonymCheckBlock.php

use Drupal\Core\Block\BlockBase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
class AnonymCheckBlock extends BlockBase {
   public function build() {
      if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
         $destination = Url::fromUserInput('/mon-profil/obtenir_document')->toString();
         $response = new RedirectResponse($destination);
         $response->send();
         return;
      }
   }
}

Then, i put these blocks in the top of my header region using drupal's BackOffice and i specified that the AnonymCheckBlock to be displayed only in the front page and the AuthSessionBlock to be displayed in all the pages an admin has access to, for me it was "/my-profile/*" (This step is very important, if neglected it will result in an infinite redirection looping). So now if an anonymous user tries to access a page that requires authentication such as "/my-profile/something" the AuthSessionCheckBlock will be activated and redirect the anonymous user back to the front page which is in my case the login page. on the other hand, if an authenticated user tries to access pages that he is not allowed to enter (such as login page, registration page...), for me i display the AnonymCheckBlock only in the front (login page, i should display it also in the registration page); so if the authenticated user tries to access the front page the AnonymCheckBlock will be activated and therefore redirect him back to him home (/mon-profil/obtenir_document).

  • Later on, it'll try to use the event subscriber as mentioned by @mradcliffe and 4k4 (Thank you By the way) as I have a feeling it will bear fruitful results in the long run. – Abouhassane Abdelhamid Oct 25 '17 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.