lavel login error

Asked 2 years ago, Updated 2 years ago, 37 views

function login(Request $request)
    {
        $user = $request->only('email', 'password');

        if (Auth::attempt($user)) {
            // dd(Auth::check()); true check completed
            return redirect()->route('home');
        }

        return redirect()->route('login');
    }
@if(Auth::check())
    <div class="buttons">
        <a href="{{ route('register') }}" class="button is-primary is-outlined">
            <strong>{{ $name }}</strong>
        </a>
        <a href="{{ route('api.logout') }}" class="button is-primary is-outlined">
            <strong>Logout</strong>
        </a>
    </div>
 @else

If you create and execute a login function like this, it seems that you succeeded in logging in when you see it go to home When you go to redirect, the template displays Auth::check() with false and acts as if you are not logged in. Once I log in, I want to log in even if I go to another page. What should I do?

laravel php

2022-09-20 22:06

1 Answers

Wow! PHP developers! In response, the concept of middleware was introduced in the modern PHP web development paradigm to do exactly that.

If you look at the official manual , you can find this source.

public function __construct()
{
    $this->middleware('auth'); // where $this is the instance that inherited the Illuminate\Routing\Controller
}

Actually, you don't have to put it in the constructor. The key is to apply auth middleware to the method responsible for /home if the /home route is a route that "must go through authentication first ('intermediate') before coming out."

Then, each time you connect to /home, the auth middleware runs before drawing the screen. Whether there's a proper login cookie or if it matches the server session or if it hasn't expired yet, and if something fails, it goes to the login screen, and if it succeeds, it does the next move - drawing the screen.

In this way, if you put auth middleware for each route/method that you want authentication to be processed first in the middle, the login will be processed. I wonder if that's the answer

+ I think you're renovating the included certification implementation, but don't do that. "Don't worry because it's very easy" Although the manual is tempting, it's quite cumbersome to actually touch everything like finding a password and resetting it.


2022-09-20 22:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.