Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Auth sessions in Laravel 4

The project I'm currently working on is split up in an admin console and the normal frontend. Both front and backend are in the same Laravel instance.

In the frontend I'm trying to create a user login system that works exclusively for the frontend. It uses a different table and model and it has different relations as oposed to the User model for the admin.

What I can't figure out is a way to use the Laravel Auth class for both systems. Logically Auth uses one single config file, and more to the point, one session name.

One solution that has been brought forward is not to use a different table and model and use some form of acl for the distinction. But I don't like the idea of mixing frontend and backend in this way. Especially because it would mean I'd suddenly have to give the admin User model all the fields and relations previously unique to the frontend user.

It just doesn't seem the right way to do things. I could switch to a different authentication system or seperate the admin into a package with its own configs but the scope of the project doesn't allow for such timeconsuming changes.

I'd welcome any idea's you could provide.

like image 323
Jochem Fuchs Avatar asked Sep 24 '13 10:09

Jochem Fuchs


People also ask

What is Auth :: attempt in Laravel?

The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column.


1 Answers

This is a problem that I encountered recently too. The whole separate environment wasn't very easy, especially if you already have development and production environments.

I did however spend some time creating a package to solve this problem, which you can find at https://github.com/ollieread/multiauth. The package itself is essentially a factory class for Auth, that allows you to use multiple instances of it, so you access it like so:

Auth::admin()->check();
Auth::user()->check();
Auth::whatever()->check();

I hope the package helps you or anyone else looking for this approach.

like image 79
ollieread Avatar answered Feb 10 '23 07:02

ollieread