3
votes

I am using Auth scaffolding in laravel 5.5. But when i try to go to /login or /register,i am redirected to /home. This is in my Login Controller:

public function login(Request $request) { $this->validateLogin($request);

    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    // Check Whether username or email used
    $user = $request->input('user');
    $password = $request->input('password');
    if (filter_var($user, FILTER_VALIDATE_EMAIL)) {
        //email used
        if(Auth::attempt(['email' => $user, 'password' => $password, 'ustatus' => 'active'])){
        // Check and go to home
            $this->sendLoginResponse($request);
            return redirect()->intended('user/home');
        }
        else{
            $this->incrementLoginAttempts($request);
            return redirect()->back()->withErrors('error','User is invalid');
        }

    } else {
        //username used
        if(Auth::attempt(['user_name' => $user, 'password' => $password, 'ustatus' => 'active'])){
        // Check and go to home{
            $this->sendLoginResponse($request);
            return redirect()->intended('user/home');
        } else {
            $this->incrementLoginAttempts($request);
            return redirect()->back()->withErrors('error', 'User is invalid');
        }
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}

My user model:

class User extends Authenticatable
{
    use Notifiable;

    protected $primaryKey = "user_id"; 
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Routes:

Auth::routes();

Can someone help. Thanks in advance

1

1 Answers

1
votes

Make sure you have and use a redirect if authenticated in the middleware

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/');
        }

        return $next($request);
    }
}

and add it to your Kernal.php under the HTTP folder

protected $routeMiddleware = [
        ...
        'auth'          => \Illuminate\Auth\Middleware\Authenticate::class,
        'guest'         => \App\Http\Middleware\RedirectIfAuthenticated::class,
        ...
    ];