laravel LARAVEL5 自定义登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30656113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LARAVEL5 Custom login
提问by devnull
I'm working in application which requires a custom login.
我正在使用需要自定义登录的应用程序。
I've to follow this flow.
我必须遵循这个流程。
- 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.
- 用户将进入登录页面。
- 用户提交登录页面。
- 应用程序将检查用户是否在数据库中 3.1(如果用户不在数据库中|它将向第三方发送请求并检查登录是否成功) 3.2 如果用户在数据库中验证密码。
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
will return true if login succeeded.
$third->login
如果登录成功,将返回 true。
Now the question is how to link this logic. with the laravel pre-defined function Auth::check()
现在的问题是如何链接这个逻辑。使用 Laravel 预定义函数Auth::check()
回答by Carlos Herrera Plata
When you install laravel, it comes with a default login, that uses a trait:
当您安装 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']);
}
}
this class use the trait for login stored in: vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
此类使用存储在以下位置的登录特征: vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php
you can overwrite the methods from this class to put your own logic, for example in the class AuthController
you can define a new:
您可以覆盖此类中的方法以放置您自己的逻辑,例如在类中AuthController
您可以定义一个新的:
function postLogin(){
//your new logic for login
}
and it gonna respect your function instead the trait funcion.
anyway, the logic behind the postLogin
from auth trait
is:
它会尊重你的功能而不是特质功能。无论如何,postLogin
from背后的逻辑auth trait
是:
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
}
you can do two things:
你可以做两件事:
- 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.
- 编辑特征本身(不好的做法)以放置您自己的逻辑
- 定义您自己的
postLogin
函数AuthController
并复制逻辑,但使用您自己的自定义逻辑对其进行编辑。
Editto be more conrete with your points:
编辑以更符合您的观点:
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.User submit login page: the form action needs to be:
{{ url('/auth/login') }}
or whatever route you put topostLogin()
Application will check if the user is in database: in the code line 1
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
用户将进入登录页面:您可以使用laravel 给您的默认登录页面,或者您可以覆盖
getLogin
函数并重定向到您自己的视图。用户提交登录页面:表单操作需要是:
{{ url('/auth/login') }}
或您放置的任何路由postLogin()
应用程序将检查用户是否在数据库中:在代码行 1
3.1(如果用户不在数据库中|它会向第三方发送请求并检查是否登录成功):在代码第3行
3.2 If user is in database verify password: in the code line 2
3.2 如果用户在数据库中验证密码:在代码第2行