不允许非活动用户登录 Laravel [重复]

Posted

技术标签:

【中文标题】不允许非活动用户登录 Laravel [重复]【英文标题】:Don't allow inactive users to login in Laravel [duplicate] 【发布时间】:2017-07-11 10:22:09 【问题描述】:

我是 Laravel 的新手,我已经用用户做了一个系统。验证用户由“php artisan make:auth”完成,我在users 表中有一个名为activated 的列;当activated = 0 时,我不想让用户登录。请帮帮忙?对不起,如果这个线索很无聊。

【问题讨论】:

这能回答你的问题吗? Login only if user is active using Laravel 【参考方案1】:

您需要使用AuthenticatesUsers trait 或手动处理您的AuthController 的一部分,方法是检查登录表单中的响应并返回适当的响应:

<?php

namespace App\Http\Controllers;

use App;

use Illuminate\Auth\AuthManager;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

/**
 * Class AuthController
 * @package Speechlink\Http\Controllers\Auth
 */
class AuthController extends Controller

    use AuthenticatesUsers;

    /**
     * Create a new authentication controller instance.
     * @param AuthManager      $auth
     */
    public function __construct(AuthManager $auth)
    
        $this->auth = $auth;
    

    /**
     * Deal with the login attempt (overrides the trait's postLogin method)
     *
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postLogin(Request $request)
    
        $this->validate($request, [
            'username' => 'required',
            'password' => 'required',
        ]);

        $credentials = $request->only('username', 'password');

        if ($this->auth->attempt($credentials)) 
            // We authenticated so continue
            return redirect('path/to/logged_in');
         else 
            // We failed authentication so redirect somewhere else
            return redirect('path/to/disabled_user');
        

    

【讨论】:

【参考方案2】:

我自己发现了,我只是将它添加到 myLoginController.php

  protected function credentials(Request $request)

    $credentials = $request->only($this->username(), 'password');
    $credentials['activated'] = 1;

    return $credentials;

【讨论】:

以上是关于不允许非活动用户登录 Laravel [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 不活动时间设置

Laravel 身份验证在 laravel 5.3 中无法正常工作

Laravel用户活动属性[重复]

在AuthController中覆盖laravel的5.2身份验证方法

Laravel 5.8 JWT API 仅在用户状态为活动时登录用户

在不强制用户登录/允许的情况下嵌入公共 Facebook 页面的活动源