mirror of
https://github.com/streamwall/streamwall.git
synced 2026-01-31 01:12:48 -05:00
Persist auth data across restarts
This commit is contained in:
@@ -64,13 +64,24 @@ export class StateWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Auth extends EventEmitter {
|
export class Auth extends EventEmitter {
|
||||||
constructor({ adminUsername, adminPassword }) {
|
constructor({ adminUsername, adminPassword, persistData }) {
|
||||||
super()
|
super()
|
||||||
this.adminUsername = adminUsername
|
this.adminUsername = adminUsername
|
||||||
this.adminPassword = adminPassword
|
this.adminPassword = adminPassword
|
||||||
this.salt = rand62(16)
|
this.salt = persistData?.salt || rand62(16)
|
||||||
this.tokensById = new Map()
|
this.tokensById = new Map()
|
||||||
this.tokensByHash = new Map()
|
this.tokensByHash = new Map()
|
||||||
|
for (const token of persistData?.tokens ?? []) {
|
||||||
|
this.tokensById.set(token.id, token)
|
||||||
|
this.tokensByHash.set(token.tokenHash, token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getPersistData() {
|
||||||
|
return {
|
||||||
|
salt: this.salt,
|
||||||
|
tokens: [...this.tokensById.values()],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getState() {
|
getState() {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
markDataSource,
|
markDataSource,
|
||||||
combineDataSources,
|
combineDataSources,
|
||||||
} from './data'
|
} from './data'
|
||||||
|
import * as persistence from './persistence'
|
||||||
import { Auth, StateWrapper } from './auth'
|
import { Auth, StateWrapper } from './auth'
|
||||||
import StreamWindow from './StreamWindow'
|
import StreamWindow from './StreamWindow'
|
||||||
import TwitchBot from './TwitchBot'
|
import TwitchBot from './TwitchBot'
|
||||||
@@ -195,6 +196,8 @@ async function main() {
|
|||||||
callback(false)
|
callback(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const persistData = await persistence.load()
|
||||||
|
|
||||||
const idGen = new StreamIDGenerator()
|
const idGen = new StreamIDGenerator()
|
||||||
let updateCustomStreams
|
let updateCustomStreams
|
||||||
const customStreamData = new Repeater(async (push) => {
|
const customStreamData = new Repeater(async (push) => {
|
||||||
@@ -216,6 +219,7 @@ async function main() {
|
|||||||
const auth = new Auth({
|
const auth = new Auth({
|
||||||
adminUsername: argv.control.username,
|
adminUsername: argv.control.username,
|
||||||
adminPassword: argv.control.password,
|
adminPassword: argv.control.password,
|
||||||
|
persistData: persistData.auth,
|
||||||
})
|
})
|
||||||
|
|
||||||
let browseWindow = null
|
let browseWindow = null
|
||||||
@@ -368,6 +372,7 @@ async function main() {
|
|||||||
|
|
||||||
auth.on('state', (authState) => {
|
auth.on('state', (authState) => {
|
||||||
updateState({ auth: authState })
|
updateState({ auth: authState })
|
||||||
|
persistence.save({ auth: auth.getPersistData() })
|
||||||
})
|
})
|
||||||
|
|
||||||
const dataSources = [
|
const dataSources = [
|
||||||
|
|||||||
31
src/node/persistence.js
Normal file
31
src/node/persistence.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { app } from 'electron'
|
||||||
|
import { promises as fsPromises, stat } from 'fs'
|
||||||
|
import path from 'path'
|
||||||
|
import throttle from 'lodash/throttle'
|
||||||
|
|
||||||
|
const stateFilePath = path.join(app.getPath('userData'), 'streamwall.json')
|
||||||
|
|
||||||
|
let lastState = {}
|
||||||
|
|
||||||
|
async function _save(partialState) {
|
||||||
|
const state = { ...lastState, ...partialState }
|
||||||
|
lastState = state
|
||||||
|
const data = JSON.stringify(state)
|
||||||
|
await fsPromises.writeFile(stateFilePath, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const save = throttle(_save, 501)
|
||||||
|
|
||||||
|
export async function load() {
|
||||||
|
try {
|
||||||
|
const data = await fsPromises.readFile(stateFilePath)
|
||||||
|
return JSON.parse(data)
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === 'ENOENT') {
|
||||||
|
// Ignore missing file.
|
||||||
|
} else {
|
||||||
|
console.warn('error reading persisted state:', err)
|
||||||
|
}
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user