Home > Back-end >  I have some problems with the Login (Laravel)
I have some problems with the Login (Laravel)

Time:04-30

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  method="POST">
        @csrf
        <img src="{{asset('images/index/header_login.jpg')}}" alt="" >
        <div >
            <input type="text"  placeholder="Email..." id="email" name="email">
        </div>
        <div >
            <input type="password"  placeholder="Password..." id="password" name="password">
            @error('message')
                <p >{{$message}}</p>
            @enderror
        </div>
        <div >
            <button type="submit" >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.

CodePudding user response:

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.

CodePudding user response:

Goodness. Use the prebuilt auth scaffolding that is in Laravel. The Laravel auth is much more secure than your code.

  • Related