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.
– Raylan Soares
Yes authentication happens, but the
Auth::user()
ends up being tableusers
, despite logging in with the details (email, password) of the right table... Auth::user() is always from the tableusers
... It’s very strange– Miguel
Boy... take a look at Laravel 5.2!
– Raylan Soares
http://learninglaravel.net/what-are-laravel-52s-new-features
– Raylan Soares
I think "php Artisan make:auth" will help you
– Raylan Soares