Laravel:使用多列进行身份验证

编程入门 行业动态 更新时间:2024-10-27 08:27:09
本文介绍了Laravel:使用多列进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Laravel 6应用程序.

I have a Laravel 6 application.

在典型的应用程序中,用户只需指定自己的 email ,其中电子邮件是唯一的.

In a typical application, the user would just specify their email, where the email is unique.

但是,在我的应用程序中,用户模型中有2列用于验证用户身份.

However, in my application, I have 2 columns in the User model that is used to authenticate users.

  • app_id
  • 电子邮件
  • 唯一(app_id,电子邮件)

因此,要登录,我们需要同时传递 app_id 和 email 以及 password .相同的 email 可以用于不同的 app_id s.

So in order to login, we need to pass both an app_id and an email, along with the password. The same email could be used across different app_ids.

我将如何实现?

推荐答案

Auth :: routes()提供的默认登录操作如下:

The default login actions provided by Auth::routes() are the following:

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::post('login', 'Auth\LoginController@login');

这是默认的 login 函数,是 LoginController 使用的 AuthenticatesUsers 特性的一部分:

This is the default login function, part of the AuthenticatesUsers trait used by the LoginController:

/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse * * @throws \Illuminate\Validation\ValidationException */ public function login(Request $request) { $this->validateLogin($request); // 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. if (method_exists($this, 'hasTooManyLoginAttempts') && $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } if ($this->attemptLogin($request)) { return $this->sendLoginResponse($request); } // 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. $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); }

有几种方法可以解决这个问题.

There are a few ways to approach this.

# app/Http/Controllers/Auth/LoginController.php public function login(Request $request) { // add more stuff like validation, return the view you want, etc. // This is barebones auth()->attempt($request->only(['app_id', 'login', 'password']); }

选项2:覆盖 LoginController 中的 validateLogin 和 credentials 函数

# app/Http/Controllers/Auth/LoginController.php protected function validateLogin(Request $request) { $request->validate([ 'app_id' => 'required|string', 'email' => 'required|string', 'password' => 'required|string', ]); } /** * Get the needed authorization credentials from the request. * * @param \Illuminate\Http\Request $request * @return array */ protected function credentials(Request $request) { return $request->only('app_id', 'email', 'password'); }

更多推荐

Laravel:使用多列进行身份验证

本文发布于:2023-10-10 12:15:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!