Laravel身份验证登录解决方案

3

我将创建postSignIn方法并进行验证: 电子邮件,密码,验证标志 首先,创建postSignIn方法时没有任何问题,例如:

public function postSignIn(){
    if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){
            return Redirect::route('home-view');
    }
    else{
        return "Email/Password wrong or Your Account not verified by Admin";
    }
}

现在我尝试通过分别针对以下两种情况提供警报,使其更加用户友好:

  • 账户未验证
  • 电子邮件/密码错误

现在我尝试将其改为以下形式:

    if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password')))){
        Auth::logout();
        if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password'),'verified' => 1))){
            return Redirect::route('home-view');
        }
        else{
            return "Your Account not verfied. Please wait until admin verified your account or contact your admin";

        }
    }
    else{
        return "NIM/Password wrong";
    }

目前没有问题,但我认为需要其他解决方案,这样就不需要进行两次身份验证登录。

2个回答

3
您可以使用“validate”方法。这样做就可以了:

您可以使用 validate 方法。这样做就可以了:

public function postSignIn(){
    if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){
            return Redirect::route('home-view');
    }
    elseif(Auth::validate(array('email' => Input::get('email'),'password' => Input::get('password')))){
           return "Your Account not verified by Admin";
    }
    else
    {
        return "Email/Password wrong";
    }
}

我刚意识到我在放置IF语句时出错了。无论如何,谢谢 :) - GandhyOnly

0

过滤器是解决问题的好方法。使用它简单而干净,看下面的示例。

如果用户在任何时候处于非活动状态,系统将注销用户, 您可以通过会话闪存消息重定向用户,您的登录代码仍然有效。

Route::filter('auth', function()
{
    if (Auth::guest())
    {
         if (Request::ajax())
         {
             return Response::make('Unauthorized', 401);
         }
         else
         {
            return Redirect::guest('login');
         }
 }
else
{
    // If the user is not active any more, immidiately log out.
    if(Auth::check() && !Auth::user()->verifiedFlag)
    {
        Auth::logout();
        Session::flash('message','Your account is not active, please contact your administrator             to active your account');

        // redirect to login page
        return Redirect::to('/');
    }
}
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接