create-sip 0.12.2 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/expressapi/app/controllers/authcontroller.js +3 -1
- package/expressapi/app/database/database.js +3 -2
- package/expressapi/app/index.js +3 -1
- package/expressapi/app/middlewares/authjwt.js +3 -1
- package/expressapi/op +58 -1
- package/expressapi/package.json +1 -0
- package/expressapi/tools/readjson.js +17 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,3 +78,11 @@ Default database type is sqlite :memory:. If you want to use another database, e
|
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
Must be set the database path in the config/default.json file.
|
|
81
|
+
|
|
82
|
+
### Default admin user
|
|
83
|
+
|
|
84
|
+
Generate admin user:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
node op admin:generate
|
|
88
|
+
```
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import bcrypt from 'bcryptjs'
|
|
2
2
|
import jwt from 'jsonwebtoken'
|
|
3
3
|
import User from '../models/user.js'
|
|
4
|
-
import
|
|
4
|
+
import { readJson } from '../../tools/readjson.js'
|
|
5
|
+
|
|
6
|
+
const config = await readJson('config/default.json')
|
|
5
7
|
|
|
6
8
|
const AuthController = {
|
|
7
9
|
async register(req, res) {
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Sequelize from 'sequelize'
|
|
2
|
-
import
|
|
2
|
+
import { readJson } from '../../tools/readjson.js'
|
|
3
|
+
|
|
4
|
+
const config = await readJson('config/default.json')
|
|
3
5
|
|
|
4
6
|
if(config.app.log != false) {
|
|
5
7
|
var log = console.log
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
console.log(typeof console.log)
|
|
9
10
|
const sequelize = new Sequelize(
|
|
10
11
|
config.db.name,
|
|
11
12
|
config.db.user,
|
package/expressapi/app/index.js
CHANGED
|
@@ -3,7 +3,9 @@ import morgan from 'morgan'
|
|
|
3
3
|
import cors from 'cors'
|
|
4
4
|
import fs from 'fs'
|
|
5
5
|
import router from './routes/api.js'
|
|
6
|
-
import
|
|
6
|
+
import { readJson } from '../tools/readjson.js'
|
|
7
|
+
|
|
8
|
+
const config = await readJson('config/default.json')
|
|
7
9
|
|
|
8
10
|
const app = express()
|
|
9
11
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import jwt from 'jsonwebtoken';
|
|
2
|
-
import
|
|
2
|
+
import { readJson } from '../../tools/readjson.js'
|
|
3
|
+
|
|
4
|
+
const config = await readJson('config/default.json')
|
|
3
5
|
|
|
4
6
|
const verifyToken = (req, res, next) => {
|
|
5
7
|
let authData = req.headers.authorization;
|
package/expressapi/op
CHANGED
|
@@ -4,12 +4,17 @@ import fs from 'fs-extra';
|
|
|
4
4
|
import fsp from 'fs/promises'
|
|
5
5
|
import { generateApiKey } from 'generate-api-key'
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import bcrypt from 'bcryptjs';
|
|
8
|
+
import path from 'path';
|
|
9
|
+
|
|
10
|
+
import { read } from 'read'
|
|
8
11
|
|
|
9
12
|
if(process.argv.length == 3 && process.argv[2] == 'key:generate') {
|
|
10
13
|
startGenerateKey();
|
|
11
14
|
} else if(process.argv.length == 3 && process.argv[2] == 'conf:generate') {
|
|
12
15
|
startGenerateConf();
|
|
16
|
+
} else if(process.argv.length == 3 && process.argv[2] == 'admin:generate') {
|
|
17
|
+
startGenerateAdmin();
|
|
13
18
|
} else if(process.argv.length < 4) {
|
|
14
19
|
console.log('Usage:');
|
|
15
20
|
console.log('node op <command> <type> <name>');
|
|
@@ -102,3 +107,55 @@ function startGenerateConf() {
|
|
|
102
107
|
fs.copyFile(sourceFileName, destinationFileName)
|
|
103
108
|
.catch(error => console.log(error));
|
|
104
109
|
}
|
|
110
|
+
|
|
111
|
+
async function loadConfig() {
|
|
112
|
+
try {
|
|
113
|
+
const filePath = path.resolve('./config/default.json');
|
|
114
|
+
const data = await fsp.readFile(filePath, 'utf8');
|
|
115
|
+
const config = JSON.parse(data);
|
|
116
|
+
return config;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
console.error('Error loading config:', error);
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function inputPassword() {
|
|
124
|
+
const password = await read({
|
|
125
|
+
prompt: 'Password: ',
|
|
126
|
+
silent: true,
|
|
127
|
+
replace: '*'
|
|
128
|
+
})
|
|
129
|
+
return password
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function startGenerateAdmin() {
|
|
133
|
+
console.log('Generate admin...')
|
|
134
|
+
try {
|
|
135
|
+
const config = await loadConfig();
|
|
136
|
+
const isMemoryDb = config.db.path === ':memory:';
|
|
137
|
+
if(isMemoryDb) {
|
|
138
|
+
console.log('Admin cannot be created in memory db!')
|
|
139
|
+
}else {
|
|
140
|
+
const { default: User } = await import('./app/models/user.js')
|
|
141
|
+
await User.sync()
|
|
142
|
+
|
|
143
|
+
const isUserExist = await User.findOne({ where: { name: 'admin' } })
|
|
144
|
+
if (isUserExist) {
|
|
145
|
+
console.log('Admin already exists!')
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const password = await inputPassword()
|
|
149
|
+
const hashedPassword = await bcrypt.hash(password, 10);
|
|
150
|
+
await User.create({
|
|
151
|
+
name: 'admin',
|
|
152
|
+
email: 'admin',
|
|
153
|
+
password: hashedPassword
|
|
154
|
+
})
|
|
155
|
+
console.log('Admin created!')
|
|
156
|
+
}
|
|
157
|
+
} catch( err) {
|
|
158
|
+
console.error('Error creating admin!')
|
|
159
|
+
console.error(err)
|
|
160
|
+
}
|
|
161
|
+
}
|
package/expressapi/package.json
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
import fsp from 'fs/promises'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
|
|
5
|
+
async function readJson(dirPath) {
|
|
6
|
+
try {
|
|
7
|
+
const filePath = path.resolve(dirPath);
|
|
8
|
+
const data = await fsp.readFile(filePath, 'utf8');
|
|
9
|
+
const dataObject = JSON.parse(data);
|
|
10
|
+
return dataObject;
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.error('Error loading config:', error);
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { readJson }
|