Published: Wednesday, April 9th, 2008

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

Download: CakePHP Auth 1  CakePHP Auth 1 (4.3 KB, 1,641 hits)

You'll find it on almost every popular web site's login page; a nice lil' checkbox that says "Remember Me". It seems to have become a basic addition to any page with a login form. Generally, when a user logs into a web site, he's logged in until his session expires. Which usually translates to when they close the browser window or when they allow the session to timeout.

With a Remember Me cookie, you hit the lil' checkbox and it stores a lil' cookie on the user's computer. This cookie can contain various information (we'll get to that later) but it essentially enables the user to bypass the login process the next time he comes to the page requiring authentication.

So, the simplest way to proceed is to list changes by file, so strap in your seat belts, here goes.

The Obvious, The Login Form - login.ctp

Now of course you need to add the checkbox and a label for said checkbox. Simple enough:

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



Published: Friday, January 11th, 2008

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

Download: CakePHP Auth 1  CakePHP Auth 1 (4.3 KB, 1,641 hits)

I just think that some of the stuff in there warrants some explanation.

isAuthorized()

This function is needed when $this-Auth->authorize = 'controller'. Theory has it, you can do something similar in app_model if $this-Auth->authorize = 'model', but I haven't looked into this.

The thing that confused me about this is that I thought you were required to perform your own validation. But oh no, this is additional authorization. Sort of like what beforeSave() does, where you can cancel the save after the validation. isAuthorized() is performed after the user has been logged in. If after that, you need some additional stuff, then you can put it in there. I'm not sure why it doesn't default to return true like beforeSave() (if everything goes well), but if it's not present, it errors out.

$this->Auth->user('group_id')

In my User Model I have a field called group_id. So you guessed it, this just returns the group_id of the user that's logged in. Things couldn't be simpler.



Published: Tuesday, January 8th, 2008

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: