gpt4 book ai didi

php - LARAVEL5 自定义登录

转载 作者:可可西里 更新时间:2023-10-31 22:42:13 25 4
gpt4 key购买 nike

我正在使用需要自定义登录的应用程序。

我必须遵循这个流程。

  1. 用户将进入登录页面。
  2. 用户提交登录页面。
  3. 应用程序将检查用户是否在数据库中3.1(如果用户不在数据库中|会向第三方发送请求,检查是否登录成功)3.2 如果用户在数据库中验证密码。

现在我已经为第三方完成了类(class),代码将像这样工作

$third = new Libraries\ThirdParty();
$third->login($username, $password);

$third->login 如果登录成功则返回 true。

现在的问题是如何链接这个逻辑。使用 laravel 预定义函数 Auth::check()

最佳答案

当你安装 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
}

它会尊重你的功能而不是特征功能。无论如何,来自 auth traitpostLogin 背后的逻辑是:

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
}

你可以做两件事:

  1. 编辑特征本身(不良做法)以放入您自己的逻辑
  2. AuthController 中定义您自己的 postLogin 函数并复制逻辑,但使用您自己的自定义逻辑对其进行编辑。

编辑更具体地表达你的观点:

  1. 用户将进入登录页面:您可以使用 laravel 给您的默认登录页面,或者您可以覆盖 getLogin 函数并重定向到您自己的 View 。

    <
  2. 用户提交登录页面:表单操作需要是:{{ url('/auth/login') }} 或者您放置到 postLogin() 的任何路径

  3. 应用程序将检查用户是否在数据库中:在代码行 1 中

    3.1(如果用户不在数据库中|会向第三方发送请求,检查是否登录成功):在代码行3

3.2 如果用户在数据库中验证密码:在代码行2

关于php - LARAVEL5 自定义登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30656113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com