mirror of
https://github.com/smogon/pokemon-showdown-loginserver.git
synced 2026-09-14 13:15:13 -05:00
Session: Improve existing user check to fix crash in addUser
This commit is contained in:
@@ -260,6 +260,9 @@ export class DatabaseTable<Row> {
|
||||
insert(partialRow: PartialOrSQL<Row>, where?: SQLStatement) {
|
||||
return this.queryExec()`INSERT INTO \`${this.name}\` (${partialRow as SQLValue}) ${where}`;
|
||||
}
|
||||
insertIgnore(partialRow: PartialOrSQL<Row>, where?: SQLStatement) {
|
||||
return this.queryExec()`INSERT IGNORE INTO \`${this.name}\` (${partialRow as SQLValue}) ${where}`;
|
||||
}
|
||||
async tryInsert(partialRow: PartialOrSQL<Row>, where?: SQLStatement) {
|
||||
try {
|
||||
return await this.insert(partialRow, where);
|
||||
|
||||
@@ -123,16 +123,15 @@ export class Session {
|
||||
async addUser(username: string, password: string) {
|
||||
const hash = await bcrypt.hash(password, Config.passwordSalt);
|
||||
const userid = toID(username);
|
||||
const exists = await users.get(userid, ['userid']);
|
||||
if (exists) return null;
|
||||
const ip = this.context.getIp();
|
||||
|
||||
const result = await users.insert({
|
||||
const result = await users.insertIgnore({
|
||||
userid, username, passwordhash: hash, email: null, registertime: time(), ip,
|
||||
});
|
||||
|
||||
if (!result.affectedRows) {
|
||||
throw new Error(`User could not be created. (${userid}, ${ip})`);
|
||||
// 0 affected rows almost always means user already exists
|
||||
return null;
|
||||
}
|
||||
return this.login(username, password);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user