【问题标题】:How to do custom auth::attempt message on laravel 4如何在 laravel 4 上做自定义 auth::attempt 消息
【发布时间】:2015-12-04 13:45:48
【问题描述】:

我需要帮助,所以当他登录时会出现不同的错误,例如这是我的代码,但仍然不知道如何返回不同的错误。

public function postLogin() 
    {
        $user = array(
            'email'     => Input::get('email'),
            'password'  => Input::get('password'),
            'active'    => 1
            );


        if (Auth::attempt($user, (Input::get('remember') == 'on') ? true : false)) {
            return Redirect::route('home');
        }

        // authentication failure! lets go back to the login page
        return Redirect::route('login')
            ->with('flash', ['message'  =>  '<strong>Error!</strong> Account is not registered.', 
                'type'      =>  'danger']);
    }

我的问题是如何添加这两个自定义重定向

如果用户有正确的用户名/密码但active = 0,它将返回

return Redirect::route('login')
            ->with('flash', ['message'  =>  '<strong>Error!</strong> Account is not verified.', 
                'type'      =>  'danger']);

如果用户的用户名/密码错误,它将像这样返回

return Redirect::route('login')
                ->with('flash', ['message'  =>  '<strong>Error!</strong> Wrong password.', 
                    'type'      =>  'danger']);

EDIT1

public function postLogin() 
    {
        $user = array(
            'email'     => Input::get('email'),
            'password'  => Input::get('password'),
            );


        if(Auth::attempt($user, (Input::get('remember') == 'on') ? true : false)) {
            if(Auth::user()->active == 1) {
                if(Auth::user()->isBanned == 0 ) {
                    return Redirect::route('home');
                }

                Auth::logout();
                $message = '<strong>Error!</strong> Your account was banned for misconduct.';
            }

            Auth::logout();
            $message = '<strong>Error!</strong> Account is not verified.';
        } else {
            $message = '<strong>Error!</strong> Wrong password.';
        }

        // authentication failure! lets go back to the login page
        return Redirect::route('login')
            ->with('flash', ['message'  =>  $message, 
                'type'      =>  'danger']);
    }

EDIT2

我的答案

public function postLogin() 
{
    $user = array(
        'email'     => Input::get('email'),
        'password'  => Input::get('password'),
        );


    if(Auth::attempt($user, (Input::get('remember') == 'on') ? true : false)) {
        if(Auth::user()->active == 1 && Auth::user()->isBanned == 0) {
            return Redirect::route('home');
        }

        if(Auth::user()->active == 0) {
            $message = '<strong>Error!</strong> Account is not verified.';
        } else if (Auth::user()->isBanned == 1) {
            $message = '<strong>Error!</strong> Your account was banned for misconduct.';
        }

        Auth::logout();

    } else {
        $message = '<strong>Error!</strong> Wrong password.';
    }

    // authentication failure! lets go back to the login page
    return Redirect::route('login')
        ->with('flash', ['message'  =>  $message, 
            'type'      =>  'danger']);
}

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    试试下面的。

    public function postLogin() 
    {
        $user = array(
            'email'     => Input::get('email'),
            'password'  => Input::get('password')
        );
    
    
        if (Auth::attempt($user, Input::has('remember'))) {
    
            if (Auth::user()->active == 1 && Auth::user()->isBanned != 0) {
                return Redirect::route('home');
            }
    
            $message = '<strong>Error!</strong> Account is not verified.';
    
            if (Auth::user()->isBanned == 0) {
                $message = '<strong>Error!</strong> Your account was banned for misconduct.';
            }
    
            Auth::logout();
    
        } else {
            $message = '<strong>Error!</strong> Wrong password.';
        }
    
        return Redirect::route('login')
            ->with('flash', [
                'message'  =>  $message, 
                'type'      =>  'danger'
            ]);
    
    }
    

    【讨论】:

    • 他仍然可以登录,
    • 因为我在filter.php上有Route::filter('guest', function() { if (Auth::check()) return Redirect::route('home'); });
    • 查看更新后的答案。激活检查后忘记注销。
    • 不工作,因为当他得到active=1 时,无论他是否被禁止,他都无法在底部阅读
    • 谢谢...我的答案已经完成了,看看我更新的问题,需要下面的Auth::logout();,所以当我们检查if(Auth::user()-&gt;active == 0)时没有错误,否则他不知道是谁已注销的用户
    猜你喜欢
    • 2020-09-09
    • 2014-06-20
    • 2019-06-20
    • 2014-09-25
    • 2014-03-13
    • 2013-06-11
    • 2014-01-17
    • 2023-03-05
    • 2016-04-17
    相关资源
    最近更新 更多