Quick Tip: Laravel 3.2.0 Auth Driver

I am upgrading a current project to Laravel 3.2 and had a bit of confusion with the (newly implemented) Auth drivers. I’m sure a lot of developers are able to use the Auth class as-is, but because I need some custom stuff returned when logging in/retrieving a user, I needed to figure out how to re-implement this logic.

Previously, the auth.php config file held the logic for logging in and retrieiving a user. Now, that logic has been moved to drivers (which, really, makes much more sense, but that’s just my opinion). With some help from the (awesome) forums, I was able to quickly port my code over.

1. Create a file in /libraries. For example purposes, we’ll call ours nerdy.php
2. The skeleton class for an Auth driver should look like this:

<?php
class Nerdy extends \Laravel\Auth\Drivers\Driver {

public function retrieve($id)
{
}

public function attempt($arguments=array())
{
}
}

2. Add your logic to the 2 methods.
3. Open up your application/start.php file and add the following:
Autoloader::map(array(
// Of course, you would change the class name and path to
// the actual name & path for your app.
'Nerdy' => __DIR__.'/libraries/nerdy.php
));

Auth::extend('nerdy', function() {
// Again, the class name should be the name you chose
return new Nerdy;
});

4. Finally, open your applicatin/auth.php file and change the driver value to (in this example) nerdy and save.

Not so bad once you know what to do! 🙂

(Source)

Advertisements
Leave a comment

1 Comment

  1. kkoala

     /  July 24, 2012

    Useful tip, thanks! I think you can omit step 3, though. By default, start.php maps the model and libraries directories to the autolader. So if your custom class lives in libraries/nerdy.php, it should be picked up automatically.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: