Hadayat's avatar

Facing issue when Customizing the laravel auth | Created the Custom laravel Login Controller

I Created the custom login auth in my application, and I want to add some other stuff in login query so I can validate the user active users.

I use this function to laravel built-in Login-controller and it successfully works

 public function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        $credentials = Arr::add($credentials, 'id', '2');
        return $credentials;
    }

When I am adding this into my custom Login Controller it is not working

Built-in laravel login (Which is successfully working)

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        $this->middleware('guest')->except(['userlogout']);
    }
    public function userlogout()
    {
         Auth::guard('web')->logout();
         return redirect('/login');
    }
    public function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        $credentials = Arr::add($credentials, 'id', '1');
        return $credentials;
    }
}

My Custom controller Which is not working

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;


class AdminLoginController extends Controller
{
    use AuthenticatesUsers;
    public function __construct()
    {
        $this->middleware('guest:admin')->except(['userlogout','logout']);
    }
   public function showLoginForm()
   {
       return view('auth.admin-login');
   }
   public function login(Request $request)
   {
       $this->validate($request, [
            'email'=>'required|email',
            'password'=>'required|min:6',
       ]);
       if(Auth::guard('admin')->attempt(['email'=>$request->email, 'password'=>$request->password], $request->remember))
       {
        return redirect()->intended(route('admin.dashboard'));
       }
       return redirect()->back()->withInput($request->only('email','remember'));
   }
   public function logout()
   {
        Auth::guard('admin')->logout();
        return redirect()->route('admin.login');
   }


   public function credentials(Request $request)
    {
        $credentials = $request->only($this->username(), 'password');
        $credentials = Arr::add($credentials, 'id', '1');
        return $credentials;
    }
}
0 likes
15 replies
SilenceBringer's avatar

Hi @hadayat

What exactly not working? What is the error you see? Right now I just little confused about this line

$this->middleware('guest:admin')
Hadayat's avatar

This one to looking for a guard whose name is admin if it exists it pass the request.

Hadayat's avatar

@silencebringer I am not able to validate the credentials, because I want to customize this query

$sql = "Select * from users where username='$username' AND password='$password'  ";

to

$sql = "Select * from users where username='$username' AND password='$password' AND status=1  ";

Hope this makes it sence?

MichalOravec's avatar
/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request)
{
    return array_merge($request->only($this->username(), 'password'), ['status' => 1]);
}
Hadayat's avatar

@michaloravec It works in a built-in login controller but it's not working in my custom login controller, I duplicate the controller everything is working fine but this function is not working. what is reason behind this, do you have any idea?

MichalOravec's avatar

In your AdminLoginController just add

/**
 * Get the guard to be used during authentication.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return Auth::guard('admin');
}

For credentials

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function credentials(Request $request)
{
    return array_merge($request->only($this->username(), 'password'), ['status' => 1]);
}

For redirect use

/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    //
}
Hadayat's avatar

Hey @jlrdw Thanks for suggesting the best option, but I already visit this site, I am facing this problem with my custom login, this code is working in built-in Auth LoginController. I am not very sure why it's happening, If you have some other tips please suggest to me, Am waiting for your precious suggestions.

Hadayat's avatar

I think this custom controller is not overriding the authenticate function

public function authenticate(Request $request)
    {
        echo "Is this function working";
    }
jlrdw's avatar

@hadayat I am guessing you are using version 7, you may want to copy the entire AuthenticatesUsers.php trait to your login controller first, so all available methods are there for you.

You can get it here: https://github.com/laravel/ui/blob/2.x/auth-backend/AuthenticatesUsers.php

From there you can look over the methods and hopefully work out what needs done.

Note, I had to work out some custom stuff a while back, it did take a little trial and error working with the methods

SilenceBringer's avatar
Level 55

@hadayat

here

public function login(Request $request)
{
    $this->validate($request, [
        'email'=>'required|email',
        'password'=>'required|min:6',
    ]);
    if(Auth::guard('admin')->attempt(['email'=>$request->email, 'password'=>$request->password], $request->remember))
    {
        return redirect()->intended(route('admin.dashboard'));
    }
    return redirect()->back()->withInput($request->only('email','remember'));
}

you rewrited default login function. So, just try to add you status here

    if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password, 'status' => 1], $request->remember))
       {
        return redirect()->intended(route('admin.dashboard'));
       }
1 like
Hadayat's avatar

Thanks for your brief info, I am using an attempt method already that's why it's not overriding the below method.

MichalOravec's avatar

@hadayat So what I posted before would be work. I said there what you have to change from original LoginController.

Please or to participate in this conversation.