File tree 3 files changed +36
-6
lines changed
3 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,9 @@ export class RawAPI extends EventEmitter {
54
54
} ,
55
55
me ( ) {
56
56
return self . req ( 'GET' , '/api/auth/me' )
57
+ } ,
58
+ queryToken ( token ) {
59
+ return self . req ( 'GET' , '/api/auth/query-token' , { token } )
57
60
}
58
61
} ,
59
62
register : {
@@ -261,6 +264,9 @@ export class RawAPI extends EventEmitter {
261
264
} ,
262
265
console ( expression , shard = DEFAULT_SHARD ) {
263
266
return self . req ( 'POST' , '/api/user/console' , { expression, shard } )
267
+ } ,
268
+ name ( ) {
269
+ return self . req ( 'GET' , '/api/user/name' )
264
270
}
265
271
} ,
266
272
experimental : {
Original file line number Diff line number Diff line change @@ -110,8 +110,32 @@ export class ScreepsAPI extends RawAPI {
110
110
return `https://screeps.com/a/#!/account/auth-tokens/noratelimit?token=${ this . token . slice ( 0 , 8 ) } `
111
111
}
112
112
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
115
139
}
116
140
get history ( ) { return this . raw . history }
117
141
get authmod ( ) { return this . raw . authmod }
Original file line number Diff line number Diff line change @@ -179,8 +179,8 @@ export class Socket extends EventEmitter {
179
179
}
180
180
async subscribe ( path , cb ) {
181
181
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 } ` }
184
184
if ( this . authed ) {
185
185
this . send ( `subscribe ${ path } ` )
186
186
} else {
@@ -193,8 +193,8 @@ export class Socket extends EventEmitter {
193
193
}
194
194
async unsubscribe ( path ) {
195
195
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 } ` }
198
198
this . send ( `unsubscribe ${ path } ` )
199
199
this . emit ( 'unsubscribe' , path )
200
200
if ( this . __subs [ path ] ) this . __subs [ path ] --
You can’t perform that action at this time.
0 commit comments