Skip to content

Commit 52f1047

Browse files
committed
More robust user ID fetching
Uses /api/auth/me if the token is a full access token, otherwise uses /api/user/name and /api/user/find
1 parent def7e0c commit 52f1047

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/RawAPI.js

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export class RawAPI extends EventEmitter {
5454
},
5555
me () {
5656
return self.req('GET', '/api/auth/me')
57+
},
58+
queryToken (token) {
59+
return self.req('GET', '/api/auth/query-token', { token })
5760
}
5861
},
5962
register: {
@@ -261,6 +264,9 @@ export class RawAPI extends EventEmitter {
261264
},
262265
console (expression, shard = DEFAULT_SHARD) {
263266
return self.req('POST', '/api/user/console', { expression, shard })
267+
},
268+
name () {
269+
return self.req('GET', '/api/user/name')
264270
}
265271
},
266272
experimental: {

src/ScreepsAPI.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,32 @@ export class ScreepsAPI extends RawAPI {
110110
return `https://screeps.com/a/#!/account/auth-tokens/noratelimit?token=${this.token.slice(0, 8)}`
111111
}
112112
async me () {
113-
this.user = await this.raw.auth.me()
114-
return this.user
113+
if (this._user) return this._user
114+
const tokenInfo = await this.tokenInfo()
115+
if (tokenInfo.full) {
116+
this._user = await this.raw.auth.me()
117+
} else {
118+
const { username } = await this.raw.user.name()
119+
const { user } = await this.raw.user.find(username)
120+
this._user = user
121+
}
122+
return this._user
123+
}
124+
async tokenInfo () {
125+
if (this._tokenInfo) {
126+
return this._tokenInfo
127+
}
128+
if (this.opts.token) {
129+
const { token } = await this.raw.auth.queryToken(this.token)
130+
this._tokenInfo = token
131+
} else {
132+
this._tokenInfo = { full: true }
133+
}
134+
return this._tokenInfo
135+
}
136+
async userID () {
137+
const user = await this.me()
138+
return user._id
115139
}
116140
get history () { return this.raw.history }
117141
get authmod () { return this.raw.authmod }

src/Socket.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ export class Socket extends EventEmitter {
179179
}
180180
async subscribe (path, cb) {
181181
if (!path) return
182-
if (!this.api.user) { await this.api.me() }
183-
if (!path.match(/^(\w+):(.+?)$/)) { path = `user:${this.api.user._id}/${path}` }
182+
const userID = await this.api.userID()
183+
if (!path.match(/^(\w+):(.+?)$/)) { path = `user:${userID}/${path}` }
184184
if (this.authed) {
185185
this.send(`subscribe ${path}`)
186186
} else {
@@ -193,8 +193,8 @@ export class Socket extends EventEmitter {
193193
}
194194
async unsubscribe (path) {
195195
if (!path) return
196-
if (!this.api.user) { await this.api.me() }
197-
if (!path.match(/^(\w+):(.+?)$/)) { path = `user:${this.api.user._id}/${path}` }
196+
const userID = await this.api.userID()
197+
if (!path.match(/^(\w+):(.+?)$/)) { path = `user:${userID}/${path}` }
198198
this.send(`unsubscribe ${path}`)
199199
this.emit('unsubscribe', path)
200200
if (this.__subs[path]) this.__subs[path]--

0 commit comments

Comments
 (0)