uid<->username lookup routes (#36)
This commit is contained in:
parent
78942467a0
commit
acfc170d59
|
@ -0,0 +1,81 @@
|
||||||
|
const path = require( "path" )
|
||||||
|
const accounts = require( path.join( __dirname, "../shared/accounts.js" ) )
|
||||||
|
|
||||||
|
const { getRatelimit } = require( "../shared/ratelimit.js" )
|
||||||
|
|
||||||
|
module.exports = ( fastify, opts, done ) =>
|
||||||
|
{
|
||||||
|
// exported routes
|
||||||
|
|
||||||
|
// GET /accounts/lookup_uid
|
||||||
|
// attempts to find the uid of a player with a given username
|
||||||
|
fastify.get( "/accounts/lookup_uid",
|
||||||
|
{
|
||||||
|
config: { rateLimit: getRatelimit( "REQ_PER_MINUTE__ACCOUNT_LOOKUPUID" ) }, // ratelimit
|
||||||
|
schema: {
|
||||||
|
querystring: {
|
||||||
|
"username": { type: "string" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async ( request ) =>
|
||||||
|
{
|
||||||
|
if( !request.query.username )
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
username: "",
|
||||||
|
matches: [],
|
||||||
|
error: "No username provided"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let matches = await accounts.AsyncGetPlayersByUsername( request.query.username )
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
username: request.query.username,
|
||||||
|
matches: matches.map( m => m.id )
|
||||||
|
}
|
||||||
|
} )
|
||||||
|
|
||||||
|
// GET /accounts/get_username
|
||||||
|
// attempts to find the username of a player with a given uid
|
||||||
|
fastify.get( "/accounts/get_username",
|
||||||
|
{
|
||||||
|
config: { rateLimit: getRatelimit( "REQ_PER_MINUTE__ACCOUNT_GETUSERNAME" ) }, // ratelimit
|
||||||
|
schema: {
|
||||||
|
querystring: {
|
||||||
|
"uid": { type: "string" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async ( request ) =>
|
||||||
|
{
|
||||||
|
if( !request.query.uid )
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
uid: "",
|
||||||
|
matches: [],
|
||||||
|
error: "No UID provided"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log( request.query.uid )
|
||||||
|
let match = await accounts.AsyncGetPlayerByID( request.query.uid )
|
||||||
|
if( match == null )
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
uid: request.query.uid,
|
||||||
|
matches: [],
|
||||||
|
error: "No user found with that UID"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
uid: request.query.uid,
|
||||||
|
matches: [match.username]
|
||||||
|
}
|
||||||
|
} )
|
||||||
|
|
||||||
|
done()
|
||||||
|
}
|
2
dev.env
2
dev.env
|
@ -28,6 +28,8 @@ REQ_PER_MINUTE__SERVER_HEARTBEAT=60
|
||||||
REQ_PER_MINUTE__SERVER_UPDATEVALUES=20
|
REQ_PER_MINUTE__SERVER_UPDATEVALUES=20
|
||||||
REQ_PER_MINUTE__SERVER_REMOVESERVER=5
|
REQ_PER_MINUTE__SERVER_REMOVESERVER=5
|
||||||
REQ_PER_MINUTE__ACCOUNT_WRITEPERSISTENCE=50
|
REQ_PER_MINUTE__ACCOUNT_WRITEPERSISTENCE=50
|
||||||
|
REQ_PER_MINUTE__ACCOUNT_LOOKUPUID=30
|
||||||
|
REQ_PER_MINUTE__ACCOUNT_GETUSERNAME=30
|
||||||
REQ_PER_MINUTE__PLAYER_DATA=50
|
REQ_PER_MINUTE__PLAYER_DATA=50
|
||||||
|
|
||||||
# origin
|
# origin
|
||||||
|
|
|
@ -88,6 +88,24 @@ function asyncDBGet( sql, params = [] )
|
||||||
} )
|
} )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function asyncDBAll( sql, params = [] )
|
||||||
|
{
|
||||||
|
return new Promise( ( resolve, reject ) =>
|
||||||
|
{
|
||||||
|
playerDB.all( sql, params, ( ex, row ) =>
|
||||||
|
{
|
||||||
|
if ( ex )
|
||||||
|
{
|
||||||
|
console.error( "Encountered error querying player database: " + ex )
|
||||||
|
reject( ex )
|
||||||
|
}
|
||||||
|
else
|
||||||
|
resolve( row )
|
||||||
|
} )
|
||||||
|
} )
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function asyncDBRun( sql, params = [] )
|
function asyncDBRun( sql, params = [] )
|
||||||
{
|
{
|
||||||
return new Promise( ( resolve, reject ) =>
|
return new Promise( ( resolve, reject ) =>
|
||||||
|
@ -181,6 +199,13 @@ module.exports = {
|
||||||
return new PlayerAccount( row.id, row.currentAuthToken, row.currentAuthTokenExpirationTime, row.currentServerId, row.persistentDataBaseline, row.lastAuthIp, row.username )
|
return new PlayerAccount( row.id, row.currentAuthToken, row.currentAuthTokenExpirationTime, row.currentServerId, row.persistentDataBaseline, row.lastAuthIp, row.username )
|
||||||
},
|
},
|
||||||
|
|
||||||
|
AsyncGetPlayersByUsername: async function AsyncGetPlayerByUsername( username )
|
||||||
|
{
|
||||||
|
let rows = await asyncDBAll( "SELECT * FROM accounts WHERE username = ?", [ username ] )
|
||||||
|
|
||||||
|
return rows.map( row => new PlayerAccount( row.id, row.currentAuthToken, row.currentAuthTokenExpirationTime, row.currentServerId, row.persistentDataBaseline, row.lastAuthIp, row.username ) )
|
||||||
|
},
|
||||||
|
|
||||||
AsyncCreateAccountForID: async function AsyncCreateAccountForID( id )
|
AsyncCreateAccountForID: async function AsyncCreateAccountForID( id )
|
||||||
{
|
{
|
||||||
await asyncDBRun( "INSERT INTO accounts ( id, persistentDataBaseline ) VALUES ( ?, ? )", [ id, DEFAULT_PDATA_BASELINE ] )
|
await asyncDBRun( "INSERT INTO accounts ( id, persistentDataBaseline ) VALUES ( ?, ? )", [ id, DEFAULT_PDATA_BASELINE ] )
|
||||||
|
@ -247,6 +272,6 @@ module.exports = {
|
||||||
// newPdataJson = Object.assign( newPdataJson, this.AsyncGetPlayerModPersistence( id, pdiff.hash ) )
|
// newPdataJson = Object.assign( newPdataJson, this.AsyncGetPlayerModPersistence( id, pdiff.hash ) )
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// return PdataJsonToBuffer( newPdataJson, pdefCopy )
|
// return PdataJsonToBuffer( newPdataJson, pdefCopy )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue