Laravel:当我尝试打开/ login或/ register时,Auth直接重定向到/ home

时间:2018-10-01 17:18:21

标签: php laravel

我在laravel 5.5中使用Auth脚手架。但是当我尝试去/ login或/ register时,我被重定向到/ home。 这是在我的登录控制器中:

公共功能登录(请求$ 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);
}

我的用户模型:

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',
    ];
}

路线:

Auth::routes();

有人可以帮忙吗?预先感谢

1 个答案:

答案 0 :(得分:0)

如果在中间件中通过了身份验证,请确保拥有并使用重定向

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);
    }
}

并将其添加到HTTP文件夹下的Kernal.php

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