首页 文章

Laravel:当我尝试打开/登录或/注册时,Auth直接重定向到/ home

提问于
浏览
0

我在laravel 5.5中使用Auth脚手架 . 但是当我尝试进入/登录或/注册时,我被重定向到/ home . 这是在我的登录控制器中:

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

我的用户模型:

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

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

    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,
            ...
        ];
    

相关问题