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