Laravel 5 以用户名或邮箱登录,登录错误多次显示验证码怎么做?

Laravel的Auth默认是以邮箱来登录的,怎样改成用户名OR邮箱登录?

多次登录错误显示验证码又怎样实现?(这是YII2的方式

登录时能以用户名或邮箱登录,当用户名/邮箱或密码错误3次之后要求输入验证码,怎么实现?

我试图重写postLogin方法:

public function postLogin(Request $request)
{
    $username = $request->get('username');
    $email = $request->get('email');
    $password = $request->get('password');
    $remeber = $request->get('remember');

    //登录表单验证
    $validator = Validator::make($request->all(), [
            'username' => 'bail|required|min:5|max:30|unique:users',
            'password' => 'bail|required|min:8|max:50',
        ]);
    //表单验证失败提示
    if ($validator->fails()) {
        $errors = $validator->errors()->all();
        if (count($errors) > 0) {
            Flash::error(implode('<br></br>', $errors));
        }
        return redirect('/login')
                   ->withInput();
    }
    //登录验证
    if (Auth::attempt(['email' => $username, 'password' => $password])
    || Auth::attempt(['username' => $username, 'password' => $password])) {
        Flash('登录成功');
        return redirect()->intended($this->redirectPath());
    } else {
        Flash(Lang::get('auth.failed'), 'error');
        return redirect('/login')
                    ->withInput();
    }
}<br></br>

结果使用邮箱登录正常,以用户名登录时居然提示:用户名已经存在!

这不是注册用户函数,怎么会有这样的提示?

iVanilla

@JellyBool 谢谢,另外问下我这个登录函数里面的逻辑是否有问题,为什么以用户名登录会出现“用户名已经存在”?这应该是注册时出现的提示。
解决了,把前面的$validator = Validator::make里面的|unique:users删掉就好了。
顺便问下,怎样延长多次登录失败之后的间隔时间?默认的60秒太短了。

JellyBool

看看你的用户名是否唯一?

iVanilla

@JellyBool 登录和注册的问题我解决了,但现在卡在“throttles”(登录锁定)上。

//尝试登录次数过多处理
		if ($this->hasTooManyLoginAttempts($request)) {
			if ($request->ajax()) {
				Flash::error(Lang::get('auth.throttles'));
				return response()->json(['lockout_time' => $this->lockoutTime()]);
			} else {
				Flash::error(Lang::get('auth.throttles'));
				return $this->sendLockoutResponse($request);
			}
		}

这样写并没有返回throttles,而是继续运行,这样就无法阻止恶意用户不断登录了。
另外这个属性protected $lockoutTime是不是设置登录锁定间隔时间的?

iVanilla

还有,这个路由https://laravist.com/discuss/comment/update出错了,导致无法编辑。

snail
//加入ThrottlesLogins
use Illuminate\Foundation\Auth\ThrottlesLogins;

//控制器方法类
try {

    /*
    * 检测用户是否尝试登录次数过多
    */
    if ($throttles && $this->hasTooManyLoginAttempts($request)) {
        return $this->sendLockoutResponse($request);
    }


    if (!Auth::attempt(['phone_mobile' => Input::get('username'), 'password' => Input::get('password')], false)) {
        throw new \Exception('您输入的帐号或者密码不正确!');
    }

    if ($throttles) {
        $this->clearLoginAttempts($request);
    }

} catch (\Exception $e) {

    if ($throttles) {
        $this->incrementLoginAttempts($request);
    }
}
iVanilla

目前并没有完全解决,继续求助。