当你安装 laravel 时,它会附带一个默认登录名,该登录名使用一个特征:
class AuthController extends Controller {
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
}
此类使用存储在以下位置的登录特征:vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
您可以覆盖此类中的方法以放置您自己的逻辑,例如在类中AuthController
你可以定义一个新的:
function postLogin(){
//your new logic for login
}
它会尊重你的功能而不是特征功能。
无论如何,背后的逻辑postLogin
from auth trait
is:
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email', 'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if ($this->auth->attempt($credentials, $request->has('remember')))
{ //this if validate if the user is on the database line 1
return redirect()->intended($this->redirectPath());
//this redirect if user is the db line 2
}
return redirect($this->loginPath())
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
//redirect again to login view with some errors line 3
}
你可以做两件事:
- 编辑特征本身(不好的做法)放置你自己的逻辑
- 定义你自己的
postLogin
函数于AuthController
并复制逻辑,但使用您自己的自定义逻辑进行编辑。
Edit更具体地表达你的观点:
用户将进入登录页面:您可以使用laravel为您提供的默认登录页面,也可以覆盖getLogin
函数并重定向到您自己的视图。
用户提交登录页面:表单操作需要是:{{ url('/auth/login') }}
或您选择的任何路线postLogin()
-
应用程序将检查用户是否在数据库中:在代码第 1 行中
3.1(如果用户不在数据库中|会向第三方发送请求并检查是否登录成功):代码第3行
3.2 如果用户在数据库中验证密码:在代码第2行中