How to deal with multiple login areas in Laravel?

Asked

Viewed 517 times

-2

In my application I have 3 distinct areas:

Admin

Where everything is managed, currently the authentication is done by Sentry (very good by the way). For this area I have the Users model that communicates with the database.

Students

Where the student has access to a wide range of data and tools.

Officials

Where the student’s guardian enters to manage their payments and data.

Each of them has its own model/table and format

I really can’t figure out how to solve this problem.

  • 1

    What exactly is the problem? Authentication itself with multiple models? In this case, the question seems even duplicated, as Ernandes pointed out.

  • That’s really it, I had not found this question before, so I decided to post it.

1 answer

4


The most practical method is to configure the authentication on the route where needed:

Config::set('auth.model', 'Admin');

or set to a URI standard

if ($request->is('admin*'))
{
    Config::set('auth.model', 'Admin');
}

Thus, the Model Admin shall be responsible for authentication.

P.S. responded with the same response from Authentication with two different tables and I’m voting as a duplicate.

  • With this method you change the authentication mode the moment you authenticate, so in the following requests, because it returns to the normal model, this should be kept somehow (I suggest Session)

  • I like the @Hernandes alternative I use a filter before my protected routes. I think I can put this command right there.

  • Hernandes, have you used this method? Because after Logging in config::get('auth.model') is back to "USER" so how much the user gives Auth::user() will give error not?

  • @Flávioh.Ferreira the configuration returns to the default with each request, you must set in the same request before using Auth:user

  • 1

    yes... but for each new request you must set again Config::set('auth.model', 'Admin'); so that it is not a problem.

  • However, "on-the-fly" settings do not persist, so it would be a good practice to set it in the Controller’s Construct or create a config method and call it in each controller method

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.