drkbpwk609294 2016-07-24 21:30
浏览 67
已采纳

使用名称或用户名Laravel 5.2进行验证[关闭]

Am a Laravel Beginnner. So i want the user the be able to login with either the name or email. The auth is generated via php artisan make:auth . Am using Laravel 5.2

-- EDIT -- And where should i put this ?

  • 写回答

2条回答 默认 最新

  • doushi2902 2016-07-25 06:38
    关注

    This is how we make email or username validation in Laravel 5.2, have a look at the code of my AuthControllerfor similar case

    namespace App\Http\Controllers\Auth;
    use App\User;
    use Validator;
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\ThrottlesLogins;
    use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    use Illuminate\Support\Facades\Lang;
    
    class AuthController extends Controller
    {
        use AuthenticatesAndRegistersUsers, ThrottlesLogins;
        protected $redirectTo = '/';
        protected $username = 'username';
        protected $maxLoginAttempts = 10;
        protected $redirectPath = '/';
        protected $redirectAfterLogout = '/';
    
        /**
         * Create a new authentication controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('guest', ['except' => 'logout']);
        }
    
        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        protected function validator(array $data)
        {
            return Validator::make($data, [
                'name' => 'required|max:255',
                'email' => 'required|email|max:255|unique:syscare_users',
                'password' => 'required|confirmed|min:6',
                'username' => 'max:255|min:6|alpha_num|unique:syscare_users',
            ]);
        }
    
    
        /**
         * Handle a login request to the application.  - Overriding
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        public function login(Request $request)
        {
            $this->validate($request, [
                $this->loginUsername() => 'required', 'password' => 'required',
            ]);
    
            // If the class is using the ThrottlesLogins trait, we can automatically throttle
            // the login attempts for this application. We'll key this by the username and
            // the IP address of the client making these requests into this application.
            $throttles = $this->isUsingThrottlesLoginsTrait();
    
            if ($throttles && $this->hasTooManyLoginAttempts($request)) {
                return $this->sendLockoutResponse($request);
            }
    
            $credentials = $this->getCredentials($request);
    
            if (Auth::attempt(['username' => $request->username , 'password' => $request->password], $request->has('remember'))) {
                return $this->handleUserWasAuthenticated($request, $throttles);
            }
            elseif(Auth::attempt(['email' => $request->username , 'password' => $request->password],$request->has('remember')))
            {
                return $this->handleUserWasAuthenticated($request, $throttles);
            }
    
    
            // If the login attempt was unsuccessful we will increment the number of attempts
            // to login and redirect the user back to the login form. Of course, when this
            // user surpasses their maximum number of attempts they will get locked out.
            if ($throttles) {
                $this->incrementLoginAttempts($request);
            }
    
            return redirect()->back()
                ->withInput($request->only($this->loginUsername(), 'remember'))
                ->withErrors([
                    $this->loginUsername() => $this->getFailedLoginMessage(),
                ]);
        }
    
        /**
         * Get the failed login message. - Override
         *
         * @return string
         */
    
         protected function getFailedLoginMessage()
            {
                return Lang::has('auth.failed')
                    ? Lang::get('auth.failed')
                    : 'Invalid username/email or password.';
            }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿