LARAVEL5自定义登录 [英] LARAVEL5 Custom login
问题描述
我正在使用需要自定义登录的应用程序.
I'm working in application which requires a custom login.
我必须遵循此流程.
- 用户将进入登录页面.
- 用户提交登录页面.
- 应用程序将检查用户是否在数据库中 3.1(如果用户不在数据库中|它将向第三方发送请求并检查登录是否成功) 3.2如果用户在数据库中,请验证密码.
- User will enter login page.
- User submit login page.
- Application will check if the user is in database 3.1 (If user not in database | it will send a request to a third-party and check if login succeeded) 3.2 If user is in database verify password.
现在我已经完成了针对第三方的课程,代码将按此方式工作
Now i've done class for the third-party and the code will work as this
$third = new Libraries\ThirdParty();
$third->login($username, $password);
如果成功登录,
$third->login
将返回true.
$third->login
will return true if login succeeded.
现在的问题是如何链接此逻辑.使用laravel预定义函数Auth::check()
Now the question is how to link this logic. with the laravel pre-defined function Auth::check()
推荐答案
在安装laravel时,它带有一个默认登录名,该登录名使用特征:
When you install laravel, it comes with a default login, that uses a trait:
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
this class use the trait for login stored in: vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
您可以覆盖此类中的方法以放入自己的逻辑,例如,在类AuthController
中,您可以定义一个新的逻辑:
you can overwrite the methods from this class to put your own logic, for example in the class AuthController
you can define a new:
function postLogin(){
//your new logic for login
}
它会尊重您的功能,而不是特质功能.
无论如何,auth trait
中postLogin
背后的逻辑是:
and it gonna respect your function instead the trait funcion.
anyway, the logic behind the 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
}
您可以做两件事:
- 编辑特征本身(不好的做法),以表达自己的逻辑
- 在
AuthController
中定义自己的postLogin
函数并复制逻辑,但使用自己的自定义逻辑对其进行编辑.
- edit the trait itself (bad practice) to put your own logic
- define your own
postLogin
function inAuthController
and copy the logic but edit it with your own custom logic.
修改 更加了解您的观点:
Edit to be more conrete with your points:
-
用户将进入登录页面:您可以使用laravel为您提供的默认登录页面,也可以覆盖
getLogin
函数并重新显示为您自己的视图.
User will enter login page: you can use the default login page that laravel gives you, or you can overwrite
getLogin
function and redircet to your own view.
用户提交登录页面:表单操作必须为:{{ url('/auth/login') }}
或您放置到postLogin()
User submit login page: the form action needs to be: {{ url('/auth/login') }}
or whatever route you put to postLogin()
应用程序将检查用户是否在数据库中:在代码行1中
Application will check if the user is in database: in the code line 1
3.1(如果用户不在数据库中|它将向第三方发送请求并检查是否成功登录):在代码行3中
3.1 (If user not in database | it will send a request to a third-party and check if login succeeded): in the code line 3
3.2如果用户在数据库中,请验证密码:在代码行2中
3.2 If user is in database verify password: in the code line 2
这篇关于LARAVEL5自定义登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!