Addition to Auth Library tutorial

Okay, I’ve decided to write a little addition to my previous tutorial. I finished the last tutorial, my Auth library for CI, with a note:
When you use this library, you’ll have to manually adjust the standard redirects from the library. I just set them to /admin and /admin/login but this could/will be different for you offcourse. A better way would be to add an initialize() method that gets a config array passed to set these URI’s in class vars.

I have adjusted my library a little so you can initialize it, setting redirects as you please. Even more, it now automatically uses any auth.php config files too. This was really easy and I thank xwero for helping me figure it out, over at the CodeIgniter community.

All we need to do is add 2 more class variables, an extra method and let the construct expect an array. Doing this makes our library look like.

Note: I deleted functions that remained the same, so don’t just overwrite everything!

How does this work? When you load a library, the Loader class automatically scans for a config file with the same name as your class. So when we add auth.php to our config folder, we can pass the index_redirect and login_redirect variables to our library when we load it.

On one of my applications, my config file looks like this:

If you don’t like using config files, you could set that array in your method and use it when loading the Auth library, or when your autoloading the library, you could use the initialize() function.

Bookmark:
  • Digg
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Reddit
  • TwitThis
  • Facebook
  • E-mail this story to a friend!

4 comments on “Addition to Auth Library tutorial”

  • Bramme.net
    July 30th, 2008 2:08 pm

    [...] Bramme.net « Els Dufourmont Addition to Auth Library tutorial [...]

  • Benoit
    August 31st, 2008 2:54 pm

    Hi, thanks for the nice tutorial. It is exactly what I was looking for. Where can I find the whole auth.php class?
    ps: Nice background ;-)

  • Jesse
    November 11th, 2008 9:56 pm

    Hey! Thanks for this great tutorial series! Very helpful to me!

    Could you provide some instruction on hashing the password?

    Thanks Much!

    Jesse

  • Bram
    November 11th, 2008 10:25 pm

    I’m writing a new Auth lib (it’s going rather slow), and I’ll be writing a tut about it too. Hashing will be included!

    I’ll let you know when I get there ;) Or you could subscribe to my RSS feed!

Leave a Reply