基于API响应调用的laravel自定义身份验证

时间:2019-02-01 06:25:39

标签: php laravel api authentication oauth-2.0

当前,我使用Laravel 5.7并尝试建立登录机制。类似于Custom user authentication base on the response of an API call的情况。

我的情况是我在本地没有自己的数据库和用户表。所有需要的是调用API以通过传递用户名,密码,客户端ID,客户端密码来进行验证。

我对邮递员对API的请求:

POST Body   
{
  "username": "tester",
  "password": "ps",
  "CLIENT_ID": "xx",
  "CLIENT_SECRET": "yy"
}

API对邮递员成功事件的响应。用户信息通过解码在此JWT令牌中。

{
    "token_type": "Bearer",
    "id_token": "eyJraWQiOiNGUvdFZC...",
    "access_token": "eyJraWQiOi....",
    "expire_in": 3600,
    "refresh_token": "eyJjdHkiOiJK..."
}

我希望在loginContoller中做类似的事情,并使用Auth :: **:

public function postSignIn(Request $request)
{
    $username = strtolower($request->username);
    $password = $request->password;

    if (Auth::attempt(['username' => $username, 'password' => $password])) {
        return Redirect::to('/dashboard')->with('success', 'Hi '. $username .'! You have been successfully logged in.');
    } else {
        return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username);
    }
}

问题:

  1. 如何在laravel中实现API身份验证? (使用枪口,服务提供商,可认证合同和驱动程序Auth?)

  2. 如何将访问令牌存储在会话/ cookie中,以附加到以后每次发送给API的每个请求中?

  3. 如何存储刷新令牌并在无效后使用它获取访问令牌?

感谢所有帮助或任何示例/指导。

1 个答案:

答案 0 :(得分:0)

您不必自己动手,有很多插件可以像lavael护照一样为您提供帮助,就像使用lavael护照一样,它是如此简单易用,您必须先看一下