Change Authentication Table, Laravel 5

0

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

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

ObreirosController:

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 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('/');
    }

That is the same in all three, except for $sessAuth

Routes ex:

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

That's also the same in all three ...

views / worker / dashboard:

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

It seems to be fine but when I try to print the username, which is a column that exists in the obreiros table but does not exist in the table users , {{Auth::user()->username}} it does not print anything to me, it seems to be an empty string , but it is not. I believe you are not changing the authentication table, which is always the users , but it does not give any errors, how do I solve this? Any better way to do this?

    
asked by anonymous 20.12.2015 / 20:29

1 answer

0

I discovered why not give. The problem is in the 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 done but we must also change the table / auth model in the corresponding middlewares:

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('/');
}
    
03.01.2016 / 21:17