Try to search your question here, if you can't find : Ask Any Question Now ?

Laravel: Additional Login Conditions not working

HomeCategory: stackoverflowLaravel: Additional Login Conditions not working
willson asked 1 week ago

I’m doing a Laravel authentication. I don’t know if it is important to clarify that I’m using the Auth::routes.
I added an active field to my Users model. But even when is not active, I’m able to login.

This is my user model:

<?php

namespace AppModels;

use IlluminateNotificationsNotifiable;
use IlluminateContractsAuthMustVerifyEmail;
use LaravelPassportHasApiTokens;
use LaratrustTraitsLaratrustUserTrait;
use IlluminateFoundationAuthUser as Authenticatable;
use Auth;

class User extends Authenticatable
{
    use Notifiable;
    use LaratrustUserTrait;
    use HasApiTokens;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'active'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'active' => 'boolean',
        'email_verified_at' => 'datetime',
    ];

}

Then, as the documentation specifies under the Specifying Additional Conditions section, I added inside my LoginController.php the custom validation with the extra parameter to the attempt function.

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;
use Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * Handle an authentication attempt.
     *
     * @param  IlluminateHttpRequest $request
     *
     * @return Response
     */
    public function authenticate(Request $request)
    {
        $email = $request->input('email');
        $password = $request->input('password');

        if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
            return redirect()->intended($redirectTo);
        }
    }

}

What Am I doing wrong?

1 Answers
Best Answer
Jyoti answered 1 week ago
Your Answer

4 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com