Skip to content
Alex Gisby edited this page Jul 20, 2014 · 3 revisions

Solution10\Auth allows you to have multiple, completely separate login systems running in parallel with each other.

You might be asking; why the hell would you want to?

I counter that with; why the hell not? Way too many Auth systems work to a singleton pattern, which is totally disingenuous. Maybe I need to have customer details in one table, and Admin users in another; I need two login systems to keep them separate (and it means that customers can never accidentally access admin systems).

But with many auth systems, this is impossible without horrible hacks. Well not with Solution10\Auth.

Using Multiple Instances

When you create your Auth instance, the first param is the name of this instance. We'll use this later, but here's how I can have 'customer' and 'backroom' login systems.

$sessionDelegate = new SessionDelegateInstance();

$customerStorageDelegate = new CustomerStorageDelegate();
$customerAuth = new Auth('customers', $sessionDelegate, $customerStorageDelegate);

$backroomStorageDelegate = new BackroomStorageDelegate();
$backroomAuth = new Auth('backroom', $sessionDelegate, $backroomStorageDelegate);

Wow, that was hard ;)

This example highlights something interesting, we can have different delegates for different instances!

Mix and Match Delegates

Because each instance is completely isolated to itself, we can pass in totally different Delegates for each one. Say our 'customer' system is powered by MySQL, then we could use a PDOStorageDelegate to access it. But the 'backroom' system is a REST webservice, so we'd need a RESTStorageDelegate to access it.

We can do this for both the SessionDelegate and StorageDelegate requirements. The built-in Solution10\Auth\Driver\Session is already multi-instance aware, so you're safe to use that on multiple instances and your sessions won't clash.

Retrieving Instances

It's kind of cumbersome to have to pass around the $auth variables to every class / controller.

Not to worry, Auth automatically keeps track of all the instances created, so you can access them easily:

$customerInstance = Auth::instance('customers');
$backroomInstance = Auth::instance('backroom');

You can also fetch all registered instances in one go like so:

$authInstances = Auth::instances();

If you want Auth to forget about all the registered instances (handy in Unit Tests), you can do the following:

Auth::clearInstances();

NOTE: This does not log out any users on those instances, it simply tells Auth to forget about them.