PROBLEM
I'm busy with my first Laravel app and although I see the benefits of the way things are written, I'm having a hard time understanding some of the behaviour.
When I try to login I get redirected to the login page. It seems like the user authenticates correctly, but it redirects to the login page regardless.
WHAT I HAVE
My users table looks like this:
,---------,---------------,---------------,----------------,---------------,-----------,
| user_id | user_username | user_password | user_firtsname | user_lastname | user_type |
|---------|---------------|---------------|----------------|---------------|-----------|
| 1 | me@domain.com | encrypted | Foo | Bar | farmer |
'---------'---------------'---------------'----------------'---------------'-----------'
This is my routes file:
<?php
Route::get('/login', 'UsersController@login');
Auth::routes();
Route::get('/dashboard', 'HomeController@dashboard')->name('dashboard');
Route::get('/users/grid', 'UsersController@grid');
Route::resource('/users', 'UsersController');
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* @var string
*/
protected $redirectTo = '/dashboard';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* @return string
*/
public function username()
{
return 'user_username';
}
/**
* @param Request $request
* @param $user
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function authenticated(Request $request, $user)
{
return $user->user_type === 'farmer' ? redirect('/dashboard') : redirect('/admin');
}
}
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* @var array
*/
protected $fillable = [
'user_username', 'user_firstname', 'user_lastname', 'user_password',
];
/**
* @var array
*/
protected $hidden = [
'user_password', 'remember_token',
];
/**
* @var bool
*/
public $timestamps = false;
/**
* @return mixed|string
*/
public function getAuthPassword()
{
return $this->user_password;
}
/**
* @return string
*/
public function getKey()
{
return $this->user_id;
}
}
WHAT I'VE DONE
I've read various questions on stackoverflow but for some reason I can't get login to work.
I created the auth using php artisan make:auth. I've tried reading the documentation too, but still no luck.
QUESTION
How do I get it to redirect to the dashboard after login? What am I missing?