Home > Back-end >  Laravel- Auth::check returns false in the middleware for successfull login
Laravel- Auth::check returns false in the middleware for successfull login

Time:09-22

My login controller

public function authenticate(Request $request)
    {
        $credentials = $request->validate([
            'username' => ['required'],
            'password' => ['required'],
        ]);

        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();
            
            return redirect()->intended('/');
        }

        return back()->withErrors([
            'error' => 'The provided credentials do not match our records.',
        ]);
    }

middleware that i check the auth

class AuthorizeUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (Auth::check()) {
            return $next($request);
        } else {
            return view('application');
        }
    }
}

Auth::check() in this middleware returns false event the Auth::attempt was successful. How do I find the cause of this issue?

CodePudding user response:

Check the primary key of your users database. If you set your table's primary key other than id, you need to set it in your User model.

The Laravel Documentation states the following:

Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention.

For example, if you have a user_id column set as your primary key in your users database, you need to place the code below in the User model:

protected $primaryKey = 'user_id';

CodePudding user response:

Have you use any guard in auth.php? Is this you default guard?

'defaults' => [
    'guard' => 'web',
     'passwords' => 'users',
],

CodePudding user response:

Put Your routes in Middleware of Auth or Web then it will works.

You can use this code in routes/api.php

 Route::post('your-route-here', [ControllerNameHere::class,'FunctionNameHere'])->name('Route-Name-Here')->middleware('api');

Or You can use in routes/web.php

 Route::post('your-route-here', [ControllerNameHere::class,'FunctionNameHere'])->name('Route-Name-Here')->middleware('web');

CodePudding user response:

Use RedirectIfAuthenticated middleware instead:

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  ...$guards
     * @return mixed
     */
    public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                return redirect(RouteServiceProvider::HOME);
            }
        }

        return $next($request);
    }
  • Related