函数App \ Http \ Controllers \ Auth \ LoginController :: authenticated()的参数太少

问题描述 投票:0回答:1

我想在我的login()返回类型中使用authenticated()。我想尝试当用户尝试通过其phn_number登录时,他们可以登录,并且它还会检查中间件/ user_role。我将这部分代码写在autheticated()上。所以我的计划是当我登录时,login()也将通过authenticated()返回。

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\SiteSettings;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
use User;

class LoginController extends Controller
{


    use AuthenticatesUsers;

    protected function authenticated (Request $request, $user)
    {
        if (Auth::check() && Auth::user()->role->id == 1 ) {
            $this->redirectTo = route('admin.dashboard');

        } elseif(Auth::check() && Auth::user()->role->id == 2 ) {
            $this->redirectTo = route('doctor.dashboard');

        } elseif(Auth::check() && Auth::user()->role->id == 3 ) {
            $this->redirectTo = route('nurse.dashboard');

        } else {
            $this->redirectTo = route('search.doctor');
        }
    }

   // protected $redirectTo = $this->authenticated();

    public function __construct()
    {
         $this->middleware('guest')->except('logout');
    }


    public function field()
    {
        if (filter_var(request()->phn_number,FILTER_VALIDATE_EMAIL)){
            return 'email';
        }else{
            return 'phn_number';
        }
    }
    public function login()
    {
        // Check Right Position
      //  return $this->field();
        if (Auth::attempt([$this->field()=>request()->phn_number, 'password'=>request()->password ])){
           // Wanna Return Authenticated function
            //return redirect()->intended('/');
            return redirect()->intended($this->authenticated());
        }else{
            return redirect()->back()->withInput('phn_number');
        }
    }

}

所以它返回给我的函数App \ Http \ Controllers \ Auth \ LoginController :: authenticated()的参数太少。我也尝试使用

  return $this->authenticated(); 

这也给我错误。

php laravel
1个回答
1
投票

authenticated()方法将覆盖Illuminate\Foundation\Auth\AuthenticatesUsers类上的方法,您必须将其传递给2个参数,因此请将login()方法更改为该参数:

public function login(Request $request)
{
    // Check Right Position
    //  return $this->field();
    if (Auth::attempt([$this->field() => $request->phn_number, 'password' => $request->password])) {
        // Wanna Return Authenticated function
        //return redirect()->intended('/');
        return redirect()->intended(authenticated($request, $this->guard()->user()));
    } else {
        return redirect()->back()->withInput('phn_number');
    }
}

如您所见,我们正在调用带有两个参数的authenticated($request, $this->guard()->user())而不是不带参数的authenticated()

您也可以重写您的authenticated方法并使用$user,也不需要使用Auth::chcek(),因为到达此方法时用户已经通过身份验证:

protected function authenticated (Request $request, $user)
{
    if ($user->role->id == 1) {
        $this->redirectTo = route('admin.dashboard');
    } else if($user->role->id == 2) {
        $this->redirectTo = route('doctor.dashboard');
    } else if($user->role->id == 3) {
        $this->redirectTo = route('nurse.dashboard');
    } else {
        $this->redirectTo = route('search.doctor');
    }
}

0
投票

通常,您应该传递该参数。即:

$this->authenticated(request(), Auth:user())

但是您实际上并不需要该函数上的信息,因为您不会使用它们,因此只需删除参数即可:

protected function authenticated () { 
    //... 
}

还有另一个问题...

redirect()->intended($arg):

将期望使用uri或路由作为参数。因此,如果您想使用

return redirect()->intended($this->authenticated());

返回您在authenticated()函数中的期望值,而不是为$redirectTo属性分配一个新值

protected function authenticated ()
{
    if (Auth::check() && Auth::user()->role->id == 1 ) {
        return route('admin.dashboard');

    } elseif(Auth::check() && Auth::user()->role->id == 2 ) {
        return route('doctor.dashboard');

    } elseif(Auth::check() && Auth::user()->role->id == 3 ) {
        return route('nurse.dashboard');

    } else {
        return route('search.doctor');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.