Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication Token Persistence #47

Open
UltraWelfare opened this issue Jun 18, 2024 · 1 comment
Open

Authentication Token Persistence #47

UltraWelfare opened this issue Jun 18, 2024 · 1 comment
Labels

Comments

@UltraWelfare
Copy link

As far as I understand the token is stored in memory

public function authenticateWithBearerToken(): array
{
$token = $this->token ??= $this->oauth()->requestToken()->access_token;
return [
RequestOptions::HEADERS => [
'Authorization' => "Bearer {$token}",
],
];
}

Which means that if, for ex; N users do a Viva::orders()->create call -> N authentication calls to Viva would happen. Each request would use a different token.

Shouldn't the authentication call be done once, then refresh when it expires? Probably store it using cache or something equivalent.

@sebdesign
Copy link
Owner

The token is stored in memory, which means in the current lifecycle the same token is being used, because the Client is a singleton.

If you want to store the token until it expires, you have to cache it like this:

// get the token from the cache or store it until it expires
$token = Cache::get('viva.token', function () {
    $token = Viva::oauth()->requestToken();

    Cache::put('viva.token', $token->access_token, $token->expires_in);

    return $token->access_token;
});

// set the token to the client
Viva::withToken($token);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants