-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.js
40 lines (31 loc) · 950 Bytes
/
Auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const xdgBasedir = require('xdg-basedir');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
class Auth {
constructor() {
this.username = null;
this.password = null;
this.jsonPath = path.resolve(xdgBasedir.config, 'droplr-cli.json');
this.getAuth();
}
getAuth() {
if (!fs.existsSync(this.jsonPath)) {
return;
}
try {
const json = require(this.jsonPath);
this.username = json.username;
this.password = json.password;
} catch (err) {
console.log(`Error reading ${this.jsonPath}: ${chalk.white(err.message)}`);
process.exit(1);
}
}
setAuth(username, password) {
this.username = username;
this.password = password;
fs.writeFileSync(this.jsonPath, JSON.stringify({ username, password }));
}
}
module.exports = Auth;