Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Function of login and logout in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.07k
    Comment on it

    Implementing code of login and logout in cakephp by using auth component in this process appController needs to be modified so that it uses Cake’s Auth component will work at the time of login and logout. This is where we tell the Auth component to redirect users to the index page after a successful login done by the user from the document and the user do login into the page after they logout. Once we do so, we need to update the beforeFilter() function to only allow the login action to be authorized for any controller. Other action will be accessible after the user will logged into the document. After doing this user have to create a file UsersController.php in app/Controller. This function will contain detail of the login and logout file.


    AppController code:

    public $components = array(
        'DebugKit.Toolbar',
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'authError' => 'You must be logged in to view this page.',
            'loginError' => 'Invalid Username or Password entered, please try again.'
     
        ));
     
    // only allow the login controllers only
    public function beforeFilter() {
        $this->Auth->allow('login');
    }
     
    public function isAuthorized($user) {
        // Here is where we should verify the role and give access based on role
         
        return true;
    }

    UsersController code:

    class UsersController extends AppController {
     
        public $paginate = array(
            'limit' => 25,
            'conditions' => array('status' => '1'),
            'order' => array('User.username' => 'asc' ) 
        );
         
        public function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('login','add'); 
        }
         
     
     
        public function login() {
             
            //if already logged-in, redirect
            if($this->Session->check('Auth.User')){
                $this->redirect(array('action' => 'index'));      
            }
             
            // if we get the post information, try to authenticate
            if ($this->request->is('post')) {
                if ($this->Auth->login()) {
                    $this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
                    $this->redirect($this->Auth->redirectUrl());
                } else {
                    $this->Session->setFlash(__('Invalid username or password'));
                }
            } 
        }
     
        public function logout() {
            $this->redirect($this->Auth->logout());
        }
     
        public function index() {
            $this->paginate = array(
                'limit' => 6,
                'order' => array('User.username' => 'asc' )
            );
            $users = $this->paginate('User');
            $this->set(compact('users'));
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: