I am new to Laravel. I am using Laravel 6 to develop a medical application. Everything works well on local. Now I tried to test my app on a server (Heroku). The login screen is loading well but when I insert the username and password, the app reloads the login page and nothing happens.
My database is stored on db4free.net and if I go to the url: myapp.herokuapp.com/register and I register a new user, the new user is added to the database so I think it is not a database connection problem. I am going to add some code if it can help you understanding what's happening:
web.php:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('auth.login');
});
Auth::routes();
Route::get('profile', 'ProfileController@index')->name('profile');
Route::put('profile-update', 'ProfileController@update')->name('profile.update');
Route::put('profile-update-password', 'ProfileController@updatePassword')->name('profile.update.password');
Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'admin', 'middleware'=>['auth','admin']], function ()
{
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
Route::resource('user', 'UserController');
Route::resource('dosimeter', 'DosimeterController');
Route::resource('dosimeterref', 'DosimeterRefController');
Route::resource('room', 'RoomController');
Route::put('password-update/{id}', 'UserController@updatePassword')->name('password.update');
Route::put('password-reset/{id}', 'UserController@resetPassword')->name('password.reset');
});
Route::group(['as'=>'mphysicist.','prefix'=>'mphysicist','namespace'=>'mphysicist', 'middleware'=>['auth','mphysicist']], function ()
{
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});
Route::group(['as'=>'professional.','prefix'=>'professional','namespace'=>'professional', 'middleware'=>['auth','professional']], function ()
{
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
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;
/**
* Where to redirect users after login.
*
* @var string
*/
//protected $redirectTo = '/';
protected $redirectTo;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
if(Auth::check() && Auth::user()->role->id == 1)
{
$this->redirectTo = route('admin.dashboard');
}
elseif (Auth::check() && Auth::user()->role->id == 2)
{
$this->redirectTo = route('mphysicist.dashboard');
}
else
{
$this->redirectTo = route('professional.dashboard');
}
$this->middleware('guest')->except('logout');
}
public function dni(){
return 'dni';
}
}
AdminMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class AdminMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check() && Auth::user()->role->id == 1)
{
return $next($request);
}
else
{
return redirect()->route('login');
}
}
}
I found the problem. I changed my login to use the DNI (national id document) instead of the email, but I forget to override the login method in login controller. I don't understand why it worked on local without this override.
For if anyone helps this is my override login method:
public function login(Request $request)
{
$this->validate($request, [
'dni' => 'required',
'password' => 'required',
]);
if(Auth::attempt(['dni' => $request->dni, 'password' => $request->password]))
{
if(Auth::check() && Auth::user()->role->id == 1)
{
return redirect()->route('admin.dashboard');
}
elseif (Auth::check() && Auth::user()->role->id == 2)
{
return redirect()->route('mphysicist.dashboard');
}
else
{
return redirect()->route('professional.dashboard');
}
}else{
return redirect()->route('login')
->with('error','DNI and Password are wrong.');
}
}