'I have some problems with the Login (Laravel)
I'm doing an application and I need to do a Login. I already do it, and it was working until yesterday, but today it stops work and I don't know why. I was searching for the problem, but I couldn't find it. The problem is that it doesn't redirect to the page that I need, but when I put other view it works (it's strange).
Here is the controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class LoginController extends Controller
{
public function show()
{
return view('auth.login');
}
public function consult()
{
if (auth()->attempt(request(['email', 'password'])) == false) {
return back()->withErrors([
'message' => 'Email o contraseñas incorrectos, intente de nuevo',
]);
} else {
return redirect()->to('/activities');
}
}
public function destroy()
{
auth()->logout();
return redirect()->to('/');
}
public function activity_view()
{
return view('activities.select_activities');
}
}
I know that the controller is working because when I write a wrong email or password, it shows me the message.
This is the table user:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('email', 50)->unique();
$table->string('password');
$table->bigInteger('rol_id')->unsigned();
$table->timestamps();
$table->foreign('rol_id')->references('id')->on('roles')
->onDelete('cascade')->onUpdate('cascade');
});
}
This is the rol table:
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('rol_name', 50);
$table->timestamps();
});
}
This is the model:
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'user_email',
'user_password',
'rol_id',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
The view login:
@extends('layouts.app')
@section('title','Login')
@section('login')
<form class="border border-2 rounded-3 shadow-lg" method="POST">
@csrf
<img src="{{asset('images/index/header_login.jpg')}}" alt="" class="img-fluid m-0 rounded-top">
<div class="offset-1 col-10 mt-5">
<input type="text" class="form-control border border-dark" placeholder="Email..." id="email" name="email">
</div>
<div class="offset-1 col-10 mt-4">
<input type="password" class="form-control border border-dark" placeholder="Password..." id="password" name="password">
@error('message')
<p class="text-danger">{{$message}}</p>
@enderror
</div>
<div class="offset-8 col-3 mt-4 mb-5">
<button type="submit" class="btn btn-primary rounded-pill border border-dark">Ingresar</button>
</div>
</form>
@endsection
And the routes:
<?php
use App\Http\Controllers\InstitucionController;
use App\Http\Controllers\InstitucionEmpresaController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
// Login Routes
Route::get('/', [LoginController::class, 'show'])->name('login.index');
Route::post('/', [LoginController::class, 'consult'])->name('login.consult');
Route::get('/logout', [LoginController::class, 'destroy'])->name('login.destroy');
Route::get('/activities', [LoginController::class, 'activity_view'])->middleware('auth');
I'll apreciate your help.
Solution 1:[1]
That's because you're trying to redirect the user to a route that is protected by the auth middleware. You should remove that protection or change the destination.
I think you're just missing a ! at the start of your login attempt.
Solution 2:[2]
Goodness. Use the prebuilt auth scaffolding that is in Laravel. The Laravel auth is much more secure than your code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Morice |
| Solution 2 |
