Laravel 5.4,当用户不活跃时显示错误消息身份验证登录 [英] Laravel 5.4, show error message auth login when user is not active

查看:194
本文介绍了Laravel 5.4,当用户不活跃时显示错误消息身份验证登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户试图使用有效的凭据登录但其状态字段为0(Inactive),则我的焦点是向用户显示一条错误消息您尚未激活或尚未激活".

My focus is to show the user an error message called "You are inactive or not activated yet", if a user is tried to login with valid credentials but his status field is 0 ( Inactive ).

如果用户的状态通过LoginController中的以下功能处于非活动状态,我可以将其重定向到登录页面.

I am able to redirect a user to login page if his/her status is inactive by below function in LoginController.

protected function credentials(\Illuminate\Http\Request $request) {
    return ['email' => $request->{$this->username()}, 'password' => $request->password, 'status' => 1];

        }

我如何在laravel 5.4中实现这一目标.

How can I achieve this in laravel 5.4.

任何指南都对我有帮助.预先感谢.

Any guide is helpful for me. Thanks in advance.

推荐答案

您可以尝试在LoginController.php中修改sendFailedLoginResponse函数

You can try to modify the sendFailedLoginResponse function in your LoginController.php

路径:app \ Http \ Controllers \ LoginController

Path: app\Http\Controllers\LoginController

记住要导入Illuminate \ Http \ Request和App \ User(这里是App \ Models \ User,因为我将User模型移到了App \ Models);

Remember to import Illuminate\Http\Request and App\User(Here is App\Models\User because I moved my User model to App\Models);

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Models\User;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function credentials(Request $request) {
      return array_merge($request->only($this->username(), 'password'), ['active' => 1]);
    }

    /**
     * Get the failed login response instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendFailedLoginResponse(Request $request)
    {
        $errors = [$this->username() => trans('auth.failed')];

        // Load user from database
        $user = User::where($this->username(), $request->{$this->username()})->first();

        // Check if user was successfully loaded, that the password matches
        // and active is not 1. If so, override the default error message.
        if ($user && \Hash::check($request->password, $user->password) && $user->active != 1) {
            $errors = [$this->username() => trans('auth.notactivated')];
        }

        if ($request->expectsJson()) {
            return response()->json($errors, 422);
        }
        return redirect()->back()
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors($errors);
    }
}

顺便说一句,我在resources \ lang \ en \ auth.php中添加了一行,以便可以将trans('auth.notactivated')用作错误消息.

By the way, I add a line in resources\lang\en\auth.php so I can use trans('auth.notactivated') as my error message.

<?php

return [
    'failed' => 'These credentials do not match our records.',
    'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
    'notactivated' => 'This account has not been activated yet.',
];

这篇关于Laravel 5.4,当用户不活跃时显示错误消息身份验证登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆