I am learning laravel and to practice I am carrying out a project that I used a few months ago that I made with PHP without any framework, so I am transferring it little by little. I have encountered some problems and one of them is that I can't add an authentication middleware to be able to access to the dashboard. I had tried doing something like this
// Route::get('/dashboard', function() {
// return view('login/dashboard');
// });
And so I was able to login if the username and password match, but I can login through the URL by adding /dashboard to the end, for that reason I need the middleware but I can't get it to work. If anyone can help me I would be glad. Thanks.
By the way, I have been checking the logic and within:
if ($user && $user->username !== null && Hash::check($credentials['username'], $username- >username)) {
//return redirect()->intended('/dashboard');
return 'test';
}
Which would be when my username and password match then if it displays the message.
This is my Authenticate.php:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*/
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : route('login');
}
}
This is my web.php (routes/web.php)
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
Auth::routes();
Route::get('/', [AuthController::class, 'showLoginForm'])->name('login');
Route::post('/', [AuthController::class, 'loginValidator'])->name('login.validator');
Route::middleware('auth')->group(function () {
Route::get('/dashboard', function() {
return view('dashboard.index');
})->name('dashboard');
});
This is my model Usuario.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Usuario extends Model
{
use HasFactory;
protected $table = 'usuarios';
protected $primaryKey = 'idUsuario';
protected $fillable = [
'nombreUsuario', 'contrasenaUsuario', 'sucursal', 'tipoUsuario'
];
}
This is my AuthController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Usuario;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function showLoginForm()
{
return view('login.index');
}
public function loginValidator(Request $request)
{
$credentials = $request->only('nombreUsuario', 'contrasenaUsuario');
$usuario = Usuario::where('nombreUsuario', $credentials['nombreUsuario'])
->where('contrasenaUsuario', '!=', null)
->first();
if ($usuario && $usuario->contrasenaUsuario !== null && Hash::check($credentials['contrasenaUsuario'], $usuario->contrasenaUsuario)) {
return redirect()->intended('/dashboard');
}
return redirect('/')->withErrors(['nombreUsuario' => 'Credenciales incorrectas']);
}
}
And this is my auth.php with the name of the table as "usuarios":
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'usuarios',
],
],
'providers' => [
'usuarios' => [
'driver' => 'eloquent',
'model' => App\Models\Usuario::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
I modified my file Usuario.php to this:
And added
Between
Apparently that fixed it, thank you very much! @lagbox