0
votes

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
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 () { //... } - porloscerros Ψ

1 Answers

1
votes

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