Skip to content

Commit

Permalink
server: fix first run account creation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Apr 5, 2023
1 parent bc57951 commit 43e5822
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
12 changes: 2 additions & 10 deletions server/src/scrypted-server-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,7 @@ async function start(mainFilename: string, options?: {
if (process.env.SCRYPTED_ADMIN_USERNAME && process.env.SCRYPTED_ADMIN_TOKEN) {
let user = await db.tryGet(ScryptedUser, process.env.SCRYPTED_ADMIN_USERNAME);
if (!user) {
user = new ScryptedUser();
user._id = process.env.SCRYPTED_ADMIN_USERNAME;
setScryptedUserPassword(user, crypto.randomBytes(8).toString('hex'), Date.now());
user.token = crypto.randomBytes(16).toString('hex');
await db.upsert(user);
user = await scrypted.usersService.addUserInternal(process.env.SCRYPTED_ADMIN_USERNAME, crypto.randomBytes(8).toString('hex'), undefined);
hasLogin = true;
}
}
Expand Down Expand Up @@ -528,11 +524,7 @@ async function start(mainFilename: string, options?: {
return;
}

const user = new ScryptedUser();
user._id = username;
setScryptedUserPassword(user, password, timestamp);
user.token = crypto.randomBytes(16).toString('hex');
await db.upsert(user);
const user = await scrypted.usersService.addUserInternal(username, password, undefined);
hasLogin = true;

const userToken = new UserToken(username, user.aclId, timestamp);
Expand Down
9 changes: 8 additions & 1 deletion server/src/services/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,23 @@ export class UsersService {
this.updateUsersPromise();
}

async addUser(username: string, password: string, aclId: string) {
async addUserInternal(username: string, password: string, aclId: string) {
await this.ensureUsersPromise();

const user = new ScryptedUser();
user._id = username;
user.aclId = aclId;
user.token = crypto.randomBytes(16).toString('hex');
setScryptedUserPassword(user, password, Date.now());
await this.scrypted.datastore.upsert(user);
this.users.set(username, user);
this.updateUsersPromise();

return user;
}

async addUser(username: string, password: string, aclId: string) {
await this.addUserInternal(username, password, aclId);
}
}

Expand Down

0 comments on commit 43e5822

Please sign in to comment.