Chron
342
17
Laravel

Bug when I manually login users

Posted 3 weeks ago by Chron
if (Auth::attempt(['email'=>request('email'), 'password'=>request('password'), 'account_type'=>"Admin"], request('remember'))) { 
    return redirect()->intended('/dashboard')->with('message', 'Welcome back');
}

It gets redirected to home as expected but only if they're not logged in but it gets redirected there even if I feed it with correct credentials and its not logged in when I do it the first time then then when I login again it redirects now to dashboard and its logged in. What could be the problem here? It's always the problem

*/-----------------------------------------------------------------/*

Here's what I changed in Middleware

RedirectIfAuthenticated.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            
            return redirect('/dashboard');
        }

        return $next($request);
    }
}

If the guest user tries to access /dashboard, it will be redirected to home route.

Authenticate.php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('home');
        }
    }
}

Please sign in or create an account to participate in this conversation.