The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How to add authentication class to a router with params

Erdem's Avatar

Erdem

24 Feb, 2016 02:23 PM

Hi there I have auth class which is middleware.

    class Auth
    {

      protected $userType = array( '1' => 'user',
                                   '2' => 'author',
                                   '3' => 'editor',
                                   '4' => 'admin' );
      public $givenAccess;
        /**
         * Example middleware invokable class
         *
         * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
         * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
         * @param  callable                                 $next     Next middleware
         *
         * @return \Psr\Http\Message\ResponseInterface
         */

        public function __construct( $container ) {
          $this->flash = $container->get('flash');

        }

        public function __invoke($request, $response, $next)
        {
          //Do Something before we execute logic
          if ( $this->checkPermission() === true ) {
              $this->flash->addMessage('login', 'You need to login to access this page');
              return $response->withStatus(301)->withHeader('Location', '/login');
          } else {
            $response =  $next($request, $response);
            return $response;
          }

        }

        public function givenAccess($access) {
          $this->givenAccess = $access;
        }

        protected function checkPermission() {
          if ( isset( $_SESSION['userLevel'] ) ) {
            if ( array_search( $givenAccess, $userType ) == $_SESSION['userLevel'] ) {
              return true;
            }
            return false;
          }
          return false;
        }
    }

for example I want to authenticate admin route but this doesn't work.

    $app->get('/admin', function ($request, $response, $args) {
        // Sample log message
      echo 'test';

    })->add( new Auth()->givenAccess('admin')  );

or if I try via container like this

    //Authentication
    $container['auth'] = function($c) {
      return new entity\Auth($c);
    };

I'm not sure how to add into router because I have to pass $c to use flash or other things.

With slim framework 2 I used to do like this:

authentication function:

    $authenticate = function ( $app ) {
        return function () use ( $app ) {
            if ( !isset( $_SESSION['userid'] ) ) {
                $_SESSION['urlRedirect'] = $app->request()->getPathInfo();
                $app->flash('danger', 'Giriş yapmak zorundasın');
                $app->redirect('/giris');
            }
        };
    };

and this is how I add into a router

    $app->get( '/admin', $authenticate($app), function() use( $app ){
          echo 'hello world';
     });

can you please explain and help me how I can authenticate a route? thanks.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac