Skip to content


CakePHP Auth Component For Dummies Tutorial

First off, I would like to say much thanks to Gwoo for finally helping me to understand this thing.

So I know what you’re thinking; I’m probably the last person to finally figure out the CakePHP’s Auth Component. For the past few months, I’ve been using obAuth because that’s the only authentication I could get to work with CakePHP. I think that I was just making it more difficult than it should have been.

My main resource for learning the Auth Component has been Chris’s tutorial, but even then I still needed help. Also, I’m the type that doesn’t really learn much without code.

Note that I’m running off of the CaekPHP 1.2 beta.

Getting Started

Now you can modify this however you like, but I’m starting out with the basics. You’re going to need the following:

  • A user database with fields username, password. Of course they don’t need to be named that way, but defaults are fun.
  • A User Model with Controller and Views - This can be baked from CakePHP
  • A login view for the user.
  • And a base app_controller.php. That’s it.

The Setup - app_controller and users_controller

So here’s the minimum in app_controller:

var $components = array('Auth');

function beforeFilter(){
	$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
	$this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
	$this->Auth->allow('display');
	$this->Auth->authorize = 'controller';
}
function isAuthorized() {
	return true;
}

You can always visit the API for a better understanding of what’s going on, but right now we’re just trying to get stuff working.

After that there’s the users_controller.php. This you can get straight out of CakePHP’s baking. You do need a small modification:

function login()
{
}

function logout(){
	$this->Session->setFlash('Logout');
	$this->redirect($this->Auth->logout());
}

Brief Explanation

Honestly, it’s magic; automagic to be precise. If you want to know how it works, you can read up in the API. But what I will do, is give you some of the magic words.

$this->Auth->authorize = ‘controller’

There are different types of authorization action (ugh - ACL stuff), CRUD (basically locks up all the editing stuff), and controller (gives you some need control). Hey, sorry I don’t know too much of what it does, just what I need.

$this->Auth->loginAction = array(’controller’ => ‘users’, ‘action’ => ‘login’)

This tells yo what the login page is. It also controls where the user is redirected to if he’s not authorized to view a page.

$this->Auth->loginRedirect = array(’controller’ => ‘pages’, ‘display’ => ‘home’)

Self explanatory: default action to redirect the user to when logged in if they go straight to the login page. If, however, they tried to access a restricted page then this will be ignored and when they login they’ll be redirected to where they wanted to go to.

$this->Auth->allow(array(’display’))

This is one of the magic functions. By default, adding the authentication component locks down all actions, except the login and logout. This is your way of telling the component let me in to the ‘display’ action for every controller. You at least want to see the homepage right?

You can also add to this in the beforeFilter() of each controller you you need (don’t forget the parent::beforeFilter() to make sure the Auth stuff is still called). Likewise there’s a $this->Auth->deny(), which does the reverse. One small tip: you can also use allow(array(’*')) to allow everything.

User Controller

For right now, the login() action can be left as is. The Auth Component handles all that foot work beautifully. You just need to make sure you call $this->Auth->logout() in your logout() action. It has the added benefit of returning the Auth’s logoutRedirect, so $this->redirect($this->Auth->logout() works great.

There you have it, I hope that helps. Now if you’re still having a hard time, I got a present for you:

Download: CakePHP Auth 1  CakePHP Auth 1 (4.3 KiB, 7,693 hits)

There you’ll find some code, to get you up and running. It’s slightly different from what I got above (some extra stuff), but it’s heavily documented.

Enjoy and Happy Baking!

If you're new here, you may want to subscribe to my Full RSS feed. Thanks for visiting!

Posted in CakePHP.

Tagged with , , , .

Related Posts


25 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Baz L says

    This isn’t a full example. You cut and paste where necessary.

  2. Johannes Fahrenkrug says

    Nice article, thanks. For anyone interested, I wrote a custom cakephp shell for creating users with authcomponent: http://blog.springenwerk.com/2008/07/cakephp-creating-custom-shell-for.html

    - Johannes

  3. David says

    I think this component has problems in Apache sometimes, as it will create a session in the file system if you set session type to ‘cake’ in the /app/config/core.php, but it does seem to just submit the form and then jump back to the login page.

    I have set,
    $this->Auth->loginRedirect = array(’controller’ => ‘pages’, ‘action’ => ‘display’, ‘loggedin’);

    In order to see if the login works correctly, but it doesn’t seem to. I think a second or even third instalment of this fantastic article would be great as doing login’s in cake atm is a real pain in the ass and getting a component to just work would be fabulous!

  4. Ben says

    Thanks for the tutorial… It’s helped me out. I have a question though regarding this:

    “You can also add to this in the beforeFilter() of each controller you you need (don’t forget the parent::beforeFilter() to make sure the Auth stuff is still called)”

    I added the allow method to the beforeFilter in a controller and added “parent::BeforeFilter()”, but it the allow method in app_controller.php is overriding the individual controller. Any ideas why? Thanks.

  5. Ben says

    I suppose the more appropriate question is, not why is it overriding app_controller.php, but is there a way around this so I can allow entire controllers?

  6. Vitaly says

    I’m just beginning with Cake and maybe the question seems stupid, but… I have copied the files from archive to the Cake’s ‘app’ folder, created the database by executing the SQL script in the ‘auth.sql’ file and configured ‘database.php’ and ‘core.php’ files just like I did for the Cake’s blog tutorial. Then I pointed Firefox to ‘localhost/cake/users/login’, entered some data into the username and password fields and hit ‘Sign in’, but nothing happened (I also tried to add some users to the ‘users’ table - same story). Only query in the log table below the login form changes…
    Is it supposed to be that way?

    P.S. I use Cake 1.2 RC2

  7. bakephp says

    i tried downloading the file it gives
    “File does not exist.” =(

  8. Mohammad Arif Hossen says

    Thanks for Auth Tutorial.

    • Baz L says

      Not a problem. I’ve been dragging my feet about doing an appropriate followup

1 2

Continuing the Discussion

  1. links for 2008-01-10 « Richard@Home linked to this post on January 9, 2008

    [...] CakePHP Authentication Component - Tutorial One | Web Development 2.0: Web Design, CakePHP, Javascri… (tags: cakephp authentication acl tutorial) [...]

  2. CakePHP Auth Component - Tutorial Two | Web Development 2.0: Web Design, CakePHP, Javascript linked to this post on January 11, 2008

    [...] is the second installment of my Auth Component Tutorial. I included a link to download a file for during the first [...]

  3. CakePHP Tutorials :: PseudoCoder.com linked to this post on February 10, 2008

    [...] - Simple User Registration in CakePHP 1.2 http://www.webdevelopment2.com - CakePHP Auth Component For Dummies Tutorial http://www.webdevelopment2.com - CakePHP Auth Component - Tutorial [...]

  4. CakePHP Auth Component - Tutorial Three: Remember Me Cookie | Web Development 2.0: Web Design, CakePHP, Javascript linked to this post on April 9, 2008

    [...] is the third installment of my Auth Component Tutorial. This tutorial builds on the first installment, so make sure you grab the download [...]



Some HTML is OK

or, reply to this post via trackback.