r6api.js icon indicating copy to clipboard operation
r6api.js copied to clipboard

Cannot use multiple instances of R6API

Open BadCoder1337 opened this issue 4 years ago • 3 comments

I need to create a different session with non-default Ubi-AppId to interact with another domain of endpoints. Example code:

const friendsApi = new R6API(credentials)
const chatApi = new R6API({ ...credentials, ubiAppId: OVERLAY_APP_ID })

const friendsApiAuth = await friendsApi.getAuth()
const chatApiAuth = await chatApi.getAuth()
console.log(friendsApiAuth, chatApiAuth)

But because of global variables in auth.ts and other places I cannot construct 2nd R6API instance. getAuth from both instances gives the same object which is impossible since session endpoint is non-idempotent.

BadCoder1337 avatar Sep 01 '21 18:09 BadCoder1337

You can call setAuthFileName method before you need to change ubi app id, it's non-ideal solution but a workaround.

const friendsApi = new R6API(credentials)
- const chatApi = new R6API({ ...credentials, ubiAppId: OVERLAY_APP_ID })
+ const chatApi = new R6API(credentials)

+ const defaultAppId = '3587dcbb-7f81-457c-9781-0e3f29f6f56a';
+ const altAppId = '83564d31-7cd7-4bc0-a763-6524e78d1a7f';

+ friendsApi.setAuthFileName(`r6api.js-auth-${defaultAppId}`);
const friendsApiAuth = await friendsApi.getAuth()
+ chatApi.setAuthFileName(`r6api.js-auth-${altAppId}`);
const chatApiAuth = await chatApi.getAuth()
console.log(friendsApiAuth, chatApiAuth)

danielwerg avatar Sep 02 '21 07:09 danielwerg

@BadCoder1337 did example above resolve your issue?

danielwerg avatar Sep 07 '21 21:09 danielwerg

I split code into separate workers. It's easier for me than creating and loading JSON. But the issue is still relevant as a refactor goal.

BadCoder1337 avatar Sep 07 '21 21:09 BadCoder1337