cyperdark / osu-api-extended

Package for advanced work with "osu" api
MIT License
49 stars 15 forks source link

How to automatically refresh OAuth token? #7

Closed KeziahMoselle closed 2 years ago

KeziahMoselle commented 2 years ago

Not an issue but title.

Do I need to run my own interval to refresh the access token? Because it seems like the library does not support auto refresh

KeziahMoselle commented 2 years ago

Seems like it is not possible to run an interval since cache_token is never being invalidated since it just checks for a truthy value.

https://github.com/cyperdark/osu-api-extended/blob/master/src/api/v2/index.ts#L21

What I did for a project is something along thoses lines:

if (
    (this.tokenExpire && new Date() > this.tokenExpire) ||
    !this.tokenExpire
  ) {
    console.log('osu! API v2 access_token is expired or needs to be created')
    await this.getToken()
  }
cyperdark commented 2 years ago

oh, let me added this

Tunnelbliick commented 2 years ago

With the API login osu should return

{
    "access_token": "verylongstring",
    "expires_in": 86400,
    "refresh_token": "anotherlongstring",
    "token_type": "Bearer"
}

technically you can use the refresh_token if the lease expires to instantly request a new access_token. This is probanly a better way of doing it then to compleatly relogging like proposed.

But from what ive seen so far the refresh_token gets compleatly thrown away.

cyperdark commented 2 years ago

i guess auto refresh is working but i can't test it properly, and also there new way to auth now will make a migrate.md for it and will push

cyperdark commented 2 years ago

new version is published

migration https://github.com/cyperdark/osu-api-extended/blob/master/changes/2.1.2.md

KeziahMoselle commented 2 years ago

Nice thanks @cyperdark!

KeziahMoselle commented 2 years ago

So I tried running my server for more than 24 hours and it was yielding the auth error, Is it a me problem or it doesn't seem to work? :(

cyperdark commented 2 years ago

hmm, let me see

cyperdark commented 2 years ago

new version is out

KeziahMoselle commented 2 years ago

image @cyperdark after 24 hours 😢 (I am calling the auth method before each API call)

cyperdark commented 2 years ago

bruh, i guess i need to run debug for a 24 hours to see why its failing

cyperdark commented 2 years ago

update the package, i hope this one will work

cyperdark commented 2 years ago

i have not that much time to test/work on package, so i make a temporary fix. Will try to fix it later when i finish one thing

KeziahMoselle commented 2 years ago

Sure thing don't worry Updated, will let you know!

KeziahMoselle commented 2 years ago

Look like it works with this temp fix 👍

cyperdark commented 2 years ago

nice to hear that

cyperdark commented 2 years ago

i pushed new version, could i ask you to test it (was complete rewrited)

also docs are look better now https://github.com/cyperdark/osu-api-extended/wiki

some sort of migration https://github.com/cyperdark/osu-api-extended/blob/master/changes/2.5.0.md

KeziahMoselle commented 2 years ago

Do I still need to call auth.login() in each of my files? From what I've seen, you are relogging if the request yield the auth error, does that mean I can just run auth.login() in my root file and everywhere else just doing resources calls ?

cyperdark commented 2 years ago

once, in any files and then it will work in any files