@@ -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