如何在laravel 4上进行自定义auth :: attempt消息

时间:2015-12-03 10:15:06

标签: php laravel-4

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

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']);
}

1 个答案:

答案 0 :(得分:0)

请尝试以下操作。

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'
        ]);

}