Laravel护照刷新令牌 [英] Laravel passport refresh token

查看:284
本文介绍了Laravel护照刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用Passport进行身份验证的Laravel 5.5版. 我已经成功创建了令牌,并且可以使用auth:api中间件对其进行访问.

I am using a Laravel version 5.5 using Passport for authentication. I have successfully create the token and can access it using the auth:api middleware.

但是,每当用户登录系统时,它都会为该用户创建新令牌.我只想刷新用户的最后一个令牌并将其发送回,而不是创建一个新的令牌.

But whenever user login into system it create new token for that user. I just want to refresh user last token and send it back instead of creating a new token.

我已使用以下代码生成身份验证令牌

I have used the following code to generate auth token

$token = $user->createToken('string-'.$user->id)->accessToken;

它生成具有1075个字符的令牌,但是当我在数据库表oauth_access_tokens中签入时,它向我显示了具有80个字符的令牌.

It generate the token with 1075 characters but when i checked in database table oauth_access_tokens it shows me the token with 80 characters.

如何使用80个字符的令牌获取最后生成的令牌并刷新并将其发送回去?

How can i get last generated token using 80 character token and refresh it and send it back?

预先感谢

推荐答案

如果您的应用程序发布了短暂的访问令牌,则用户将需要通过颁发访问令牌时提供给他们的刷新令牌来刷新他们的访问令牌. .在此示例中,我们将使用Guzzle HTTP库刷新令牌:

If your application issues short-lived access tokens, users will need to refresh their access tokens via the refresh token that was provided to them when the access token was issued. In this example, we'll use the Guzzle HTTP library to refresh the token:

$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'refresh_token',
        'refresh_token' => 'the-refresh-token',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'scope' => '',
    ],
]);

return json_decode((string) $response->getBody(), true);

/oauth/token路由将返回一个包含access_token,refresh_tokenexpires_in属性的JSON响应. expires_in属性包含访问令牌过期之前的秒数.

This /oauth/token route will return a JSON response containing access_token, refresh_token, and expires_in attributes. The expires_in attribute contains the number of seconds until the access token expires.

这篇关于Laravel护照刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆