本文介绍了延长laravel 5内置的身份验证仅&QUOT登录;如果用户==活跃"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用laravel 5.1.6所包含的认证,想知道我怎么可以扩展它,像这样的工作:

I use the included authentication of laravel 5.1.6 and want to know how I can extend it, to work like this:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
    // The user is active, not suspended, and exists.
}

如果用户不是有效的,登录应是不可能的。我在用户表中的主动专栏,以0或者1的值。我怎样才能做到这一点的同时仍使用内置在登录throtteling认证。

If the user is not "active", the login should not be possible. I have an 'active' column in the users table , with 0 or 1 as value. How can i do this while still using the built in authentication with login throtteling.

编辑:

我没有postLogin功能在AuthController,只有使用AuthenticatesAndRegistersUsers,ThrottlesLogins; ,一个 __构造(),一个验证器()创建()功能。我必须改变性状东西照亮\\基金会\\验证\\ .. 或我必须添加的 postLogin()在AuthController功能?

I don't have a postLogin function in the AuthController, only a use AuthenticatesAndRegistersUsers, ThrottlesLogins; , a __construct(), a validator() and a create() function. Do I have to change something in the trait in Illuminate\Foundation\Auth\.. or must I add the the postLogin() function in the AuthController ?

推荐答案

您可以只覆盖 getCredentials(显示)在AuthController方式:

You can just override the getCredentials() method in your AuthController:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers;

    public function getCredentials($request)
    {
        $credentials = $request->only($this->loginUsername(), 'password');

        return array_add($credentials, 'active', '1');
    }
}

这将试图验证用户时添加激活= 1 约束。

This will add the active = 1 constraint when trying to authenticate a user.

编辑:如果您想要像BrokenBinary说,然后Laravel允许你定义一个名为方法验证 A用户已通过验证后调用,但重定向之前,让你做任何登录后处理。所以,你可以通过检查验证的用户是主动利用这一点,并抛出一个异常,或显示错误消息如果不是:

If you want a separate error message like BrokenBinary says, then Laravel allows you to define a method called authenticated that is called after a user has been authenticated, but before the redirect, allowing you to do any post-login processing. So you could utilise this by checking if the authenticated user is active, and throw an exception or display an error message if not:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers;

    public function authenticated(Request $request, User $user)
    {
        if ($user->active) {
            return redirect()->intended($this->redirectPath());
        } else {
            // Raise exception, or redirect with error saying account is not active
        }
    }
}

不要忘了导入请求类和用户模型类。

这篇关于延长laravel 5内置的身份验证仅&QUOT登录;如果用户==活跃"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:05