dommilosz / minecraft-auth

29 stars 3 forks source link

Refresh tokens #3

Closed taep96 closed 2 years ago

taep96 commented 2 years ago

Do I need to refresh tokens myself or does the package do it for me?

dommilosz commented 2 years ago

If you use account.use() it will automatically refresh the token if needed

dommilosz commented 2 years ago

Here is code fragment that does it:

async use() {
        if (await this.checkValidToken()) {
            return this.accessToken;
        } else {
            if (this.login_username && this.login_password) {
                try {
                    await this.refresh();
                    return this.accessToken;
                } catch (e) {
                    await this.Login();
                    return this.accessToken;
                }
            } else {
                await this.refresh();
                return this.accessToken;
            }
        }
    }

If the token is valid it returns it If the token is invalid it will first refresh it. Than if it fails login with account credentials. If no login credentials are provided It will also refresh it.

dommilosz commented 2 years ago

If you have any questions/suggestions feel free to ask.