mirror of https://github.com/tycrek/ass
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.1 KiB
31 lines
1.1 KiB
/**
|
|
* Used for global auth management
|
|
*/
|
|
|
|
const fs = require('fs-extra');
|
|
const { log, path, arrayEquals, generateToken } = require('./utils');
|
|
|
|
// Make sure auth.json exists and generate the first key
|
|
if (!fs.existsSync(path('auth.json'))) {
|
|
let users = {};
|
|
users[generateToken()] = { username: 'ass', count: 0 };
|
|
fs.writeJsonSync(path('auth.json'), { users }, { spaces: 4 });
|
|
log.debug('File created', 'auth.json')
|
|
.success('!! Important', `Save this token in a secure spot: ${Object.keys(users)[0]}`)
|
|
}
|
|
|
|
const users = require('./auth.json').users || {};
|
|
|
|
// Monitor auth.json for changes (triggered by running 'npm run new-token')
|
|
fs.watch(path('auth.json'), { persistent: false },
|
|
(eventType) => eventType === 'change' && fs.readJson(path('auth.json'))
|
|
.then((json) => {
|
|
if (!(arrayEquals(Object.keys(users), Object.keys(json.users)))) {
|
|
Object.keys(json.users).forEach((token) => (!Object.prototype.hasOwnProperty.call(users, token)) && (users[token] = json.users[token]));
|
|
log.info('New token added', Object.keys(users)[Object.keys(users).length - 1] || 'No new token');
|
|
}
|
|
})
|
|
.catch(log.c.error));
|
|
|
|
module.exports = users;
|