Change authentication table, Laravel 5

Asked

Viewed 1,771 times

0

I’m making a platform that will have 3 authentication systems, one for ordinary users, one for admins, and one for workers.

I thought changing the model and table in config->auth would be enough but apparently it is not.

Workers:

class ObreiroController extends Controller
{
    public function __construct() {
        Config::set('auth.model', 'App\Obreiros');
        Config::set('auth.table', 'obreiros');
    }

    public function doLogin(Request $request) {

        $credentials = array(
            'username' => $request->input('username'),
            'password' => $request->input('password'),
        );

        if(!Auth::attempt($credentials)) {
            Session::flash('flash_error', 'Something went wrong with your login chef');
            return redirect('/');
        }

        Session::set('auth', 'obra');
        return redirect('/obra/dashboard');
    }

    public function goDashboard() {
        return view('/obreiro/dashboard');
    }
}

Usercontroller:

class UserController extends Controller
{
    public function __construct() {
        Config::set('auth.model', 'App\User');
        Config::set('auth.table', 'users');
    }

    public function doLogin(Request $request) {

        $credentials = array(
            'email' => $request->input('email'),
            'password' => $request->input('password'),
        );

        if(!Auth::attempt($credentials)) {
            Session::flash('flash_error', 'Something went wrong with your login chef');
            return redirect('/');
        }

        Session::set('auth', 'user');
        return redirect('/user/dashboard');
    }

    public function goDashboard() {
        return view('/user/dashboard');
    }
}

And the same for the Admincontroller...

The Middleware:

class UserAuth
{

    public function handle($request, Closure $next)
    {
        $sessAuth = 'user';

        if (Auth::user() && Session::get('auth') == $sessAuth) {
            return $next($request);
        }
        else if (Auth::user() && Session::get('auth') != $sessAuth) {
            Session::flash('flash_session_off', 'YOU SHALL NOT PASS' .$sessAuth);
            return redirect('/');
        }
        Session::flash('flash_session_off', 'you are off chef, please login again');
        return redirect('/');
    }

Which is the same in all three except $sessAuth

Routes ex:

Route::group(['middleware' => 'userAuth'], function() {
    Route::get('/user/dashboard', 'UserController@goDashboard');
});
Route::post('/auth/user', 'UserController@doLogin');

Which is also the same in all three...

views/worker/Dashboard:

Hello {{Auth::user()->username}}

It seems to be all right but when I try in the view print the username, which is a column that exists in the table obreiros but does not exist in the table users, {{Auth::user()->username}} it doesn’t print anything to me, it looks like an empty string, but it’s not. I don’t think it’s changing the authentication table, which is always the users, but there’s no mistake, how do I fix it? Some better way to do it?

  • But does authentication actually happen? Have you checked if he may be redirecting to the view you sent without authentication happening? So there’s nothing to show when you have it printed.

  • Yes authentication happens, but the Auth::user() ends up being table users, despite logging in with the details (email, password) of the right table... Auth::user() is always from the table users... It’s very strange

  • Boy... take a look at Laravel 5.2!

  • http://learninglaravel.net/what-are-laravel-52s-new-features

  • I think "php Artisan make:auth" will help you

1 answer

0


I figured out why not. The problem is in middleware should also be the dynamism of changing the table for authentication, IE, in the controller we have to change the auth.table which by default is users, this was already being done but we also have to change the table/model of auth in the corresponding middleware:

EX middlewares:

class ObraAuth
{

// acrescentar em cada middleware as tabelas/models correspondentes
public function __construct() {
    Config::set('auth.model', 'App\Obra');
    Config::set('auth.table', 'obreiros');
}

public function handle($request, Closure $next)
{
    $sessAuth = 'obreiro';

    if (Auth::user() && Session::get('auth') == $sessAuth) {
        return $next($request);
    }
    else if (Auth::user() && Session::get('auth') != $sessAuth) {
        Session::flash('flash_session_off', 'YOU SHALL NOT PASS' .$sessAuth);
        return redirect('/');
    }
    Session::flash('flash_session_off', 'you are off chef, please login again');
    return redirect('/');
}
  • I don’t quite understand, what has changed? Could you provide an answer with details? This can help other users.

  • I clarified it better

  • I just don’t understand where ObraAuth fits and relationship to the question code.

  • I have three authentication systems, workers, users, and admins. My problem is that the default table for authentication on routes that required someone to login ex: /obreiros/dashboard needed to have the default table corresponding, if admin would be the table admins, users would be users and workers would be obreiros. And I was able to do that by setting the right table in the middleware builder. Otherwise the auth table that was on these restricted routes was always users which is the default in config->auth

Browser other questions tagged

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