Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit cc385b8

Browse files
authored
Merge pull request #241 from codeoverflow-org/feature/236-automatic-login
Implement automatic login after startup
2 parents 0555c75 + a58d07f commit cc385b8

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

nodecg-io-core/configschema.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"automaticLogin": {
7+
"type": "object",
8+
"properties": {
9+
"enabled": {
10+
"type": "boolean",
11+
"description": "Whether automatic login should be enabled. Can be disabled using this flag for temporary testing reasons."
12+
},
13+
"password": {
14+
"type": "string",
15+
"description": "The password of your nodecg-io installation. Used to login once nodecg has started."
16+
}
17+
},
18+
"required": ["enabled", "password"]
19+
}
20+
},
21+
"required": []
22+
}

nodecg-io-core/extension/persistenceManager.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface EncryptedData {
3131
}
3232

3333
/**
34-
* Decryptes the passed encrypted data using the passed password.
34+
* Decrypts the passed encrypted data using the passed password.
3535
* If the password is wrong an error will be returned.
3636
*
3737
* @param cipherText the ciphertext that needs to be decrypted.
@@ -67,6 +67,7 @@ export class PersistenceManager {
6767
persistent: true, // Is ok since it is encrypted
6868
defaultValue: {},
6969
});
70+
this.checkAutomaticLogin();
7071
}
7172

7273
/**
@@ -270,4 +271,53 @@ export class PersistenceManager {
270271
this.nodecg.log.info("Finished creating service instances from stored configuration.");
271272
this.save();
272273
}
274+
275+
/**
276+
* Checks whether automatic login is setup and enabled. If yes it will do it using {@link PersistenceManager.setupAutomaticLogin}.
277+
*/
278+
private checkAutomaticLogin(): void {
279+
if (!this.nodecg.bundleConfig.automaticLogin) {
280+
return; // Not configured
281+
}
282+
283+
// If the automaticLogin object exists the JSON schema guarantees that enabled is a boolean and password is a string
284+
const enabled: boolean = this.nodecg.bundleConfig.automaticLogin.enabled;
285+
const password: string = this.nodecg.bundleConfig.automaticLogin.password;
286+
287+
if (enabled === false) {
288+
this.nodecg.log.info("Automatic login is setup but disabled.");
289+
return;
290+
}
291+
292+
this.setupAutomaticLogin(password);
293+
}
294+
295+
/**
296+
* Setups everything needed to automatically login using the provided password after nodecg has loaded.
297+
*/
298+
private setupAutomaticLogin(password: string): void {
299+
// We need to do the login after all bundles have been loaded because when loading these might add bundle dependencies
300+
// or even register services which we need to load nodecg-io.
301+
// There is no official way to wait for nodecg to be done loading so we use more or less a hack to find that out:
302+
// When we declare the replicant here we will trigger a change event with a empty array.
303+
// Once nodecg is done loading all bundles it'll assign a array of bundles that were loaded to this replicant
304+
// So if we want to wait for nodecg to be loaded we can watch for changes on this replicant and
305+
// if we get a non-empty array it means that nodecg has finished loading.
306+
this.nodecg.Replicant<unknown[]>("bundles", "nodecg").on("change", async (bundles) => {
307+
if (bundles.length > 0) {
308+
try {
309+
this.nodecg.log.info("Attempting to automatically login...");
310+
const loadResult = await this.load(password);
311+
312+
if (!loadResult.failed) {
313+
this.nodecg.log.info("Automatic login successful.");
314+
} else {
315+
this.nodecg.log.error(`Failed to automatically login: ${loadResult.errorMessage}`);
316+
}
317+
} catch (err) {
318+
this.nodecg.log.error(`An error occurred while automatically logging in: ${err}`);
319+
}
320+
}
321+
});
322+
}
273323
}

0 commit comments

Comments
 (0)