create-sipere 1.1.1 → 1.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sipere",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "main": "create-sipere.js",
5
5
  "bin": {
6
6
  "create-sipere": "create-sipere.js"
@@ -1,6 +1,10 @@
1
1
  # sipere expressapi template
2
2
 
3
- Express based REST API template
3
+ Express based REST API template.
4
+
5
+ Website:
6
+
7
+ * [https://sipere.github.io/sipere/](https://sipere.github.io/sipere/)
4
8
 
5
9
  ## Install dependencies
6
10
 
@@ -4,11 +4,13 @@ import cors from 'cors'
4
4
  import fs from 'fs'
5
5
  import router from './routes/api.js'
6
6
  import './models/modrels.js'
7
+ import { UPLOAD_PATH } from './utils/paths.js'
7
8
 
8
9
  const app = express()
9
10
 
10
11
  const logfile = 'access.log'
11
12
  var accessLogStream = fs.createWriteStream(logfile, { flags: 'a' })
13
+ app.use('/images', express.static(UPLOAD_PATH))
12
14
  app.use(morgan('dev', { stream: accessLogStream }))
13
15
  app.use(cors())
14
16
  app.use(express.json())
@@ -0,0 +1,28 @@
1
+ import User from '../models/user.js'
2
+
3
+ const isAdmin = async (req, res, next) => {
4
+ try {
5
+ const user = await User.findByPk(req.userId)
6
+ if(!user) {
7
+ return res.status(404).json({
8
+ success: false,
9
+ message: 'User not found'
10
+ })
11
+ }
12
+ if(user.roleId === 1) {
13
+ next()
14
+ }else {
15
+ return res.status(403).json({
16
+ success: false,
17
+ message: 'You are not admin'
18
+ })
19
+ }
20
+ }catch(err) {
21
+ return res.status(500).json({
22
+ success: false,
23
+ message: err.message
24
+ })
25
+ }
26
+ }
27
+
28
+ export default isAdmin
@@ -0,0 +1,34 @@
1
+ import User from '../models/user.js'
2
+
3
+ const checkRole = (requiredRole) => {
4
+ return async (req, res, next) => {
5
+ try {
6
+ const user = await User.findByPk(req.userId)
7
+ if(!user) {
8
+ return res.status(404).json({
9
+ success: false,
10
+ message: 'User not found'
11
+ })
12
+ }
13
+ const roles = [0, 1, 2]
14
+ const userRoleLevel = roles.indexOf(user.roleId)
15
+ const requiredRoleLevel = roles.indexOf(requiredRole)
16
+
17
+ if(userRoleLevel >= requiredRoleLevel) {
18
+ next()
19
+ }else {
20
+ return res.status(403).json({
21
+ success: false,
22
+ message: 'You are not allowed to do this action'
23
+ })
24
+ }
25
+ }catch(err) {
26
+ return res.status(500).json({
27
+ success: false,
28
+ message: err.message
29
+ })
30
+ }
31
+ }
32
+ }
33
+
34
+ export default checkRole
@@ -0,0 +1,10 @@
1
+ import { fileURLToPath } from 'url';
2
+ import { dirname, resolve, join } from 'path';
3
+
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = dirname(__filename);
6
+
7
+ const ROOT_DIR = resolve(__dirname, '../..');
8
+ const UPLOAD_PATH = join(ROOT_DIR, 'public', 'uploads');
9
+
10
+ export { ROOT_DIR, UPLOAD_PATH }
@@ -2,7 +2,7 @@ const { exec } = require('child_process');
2
2
  const assert = require('assert');
3
3
 
4
4
  describe('create-sipere', function() {
5
- this.timeout(8000);
5
+ this.timeout(14000);
6
6
  it('create project', (done) => {
7
7
  const cmd = './create-sipere.js';
8
8
  const child = exec(cmd);