0

I wanna use authenticated() in my login() return type. I wanna try to when user try to login via their phn_number they can login and it also check middleware / user_role. This part of code i write on autheticated() . so my plan is when i login , the login() will return via the authenticated() also.

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

}

So It's returned me Too few arguments to function App\Http\Controllers\Auth\LoginController::authenticated().
I also try to use

  return $this->authenticated(); 

It also give me error.

1
  • 2
    you shold pass that arguments. Ie: $this->authenticated(request(), Auth:user()), but you don't really needs that information in that function, just remove them protected function authenticated () { //... } Commented Feb 22, 2020 at 23:52

1 Answer 1

2

The authenticated() method that overrides method on Illuminate\Foundation\Auth\AuthenticatesUsers class, has 2 arguments you have to pass to, so change your login() method to this one:

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

As you can see, we're calling authenticated($request, $this->guard()->user()) with 2 arguments instead of authenticated() with no arguments.

You can also rewrite your authenticated method and use $user, also no need to use Auth::chcek() because user is already authenticated when reaches this method:

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');
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

There is no requirement for the op to pass two arguments to the authenticated method. You can simply overridde it because AuthenticateUsers is just a trait. So if they just drop the $user argument and use Auth::user() it should be fine. Same with the request, just use the facade like porloscerro said.
@AlexMac you're right, I tried to explain that why the op is getting this error, the authenticated method has been overridden with 2 args and should be call with 2 args. Or be overridden with no args and be called with no args too.
Yeah, sorry on mobile, I didn't see your update. I just wanted to point out op was dealing with a trait. Therefore, you could override everything if op really wanted to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.