用户处于非活动状态时登录错误消息

时间:2017-03-21 11:07:35

标签: laravel

我有Laravel代码登录auth页面,问题是当我登录非活动用户时,我应该收到错误消息,说明你的用户没有被激活。

我的代码

public function postLogin(Request $request)
{
    $credentials = $request->only(['username', 'password']);

    $validator = Validator::make($credentials, [
        'username' => 'required', 'password' => 'required',
    ]);

    // If the user is activated then value will be 1 and not activated user will 0
    // <--- THIS LINE

    if(!$credentials['active'] = 1)
    {
        return Redirect::back()->withInput()->withErrors($validator);
        return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
    }

    if ($validator->fails()) {
        return Redirect::back()->withInput()->withErrors($validator);
    }

    if (Auth::guard('admin')->attempt($credentials)) {
        admin_toastr(trans('admin::lang.login_successful'));

        return redirect()->intended(config('admin.prefix'));
    }


    return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
}

1 个答案:

答案 0 :(得分:0)

您在if语句中没有使用正确的运算符。变化

if(!$credentials['active'] = 1)
{
    return Redirect::back()->withInput()->withErrors($validator);
    return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
}

if(!$credentials['active'] == 1)
{
   return Redirect::back()->withInput()->withErrors($validator);
   return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
}

OR

if($credentials['active'] != 1)
{
   return Redirect::back()->withInput()->withErrors($validator);
   return Redirect::back()->withInput()->withErrors(['username' => $this->getFailedLoginMessage()]);
}