create-sip 1.3.2 → 1.3.4

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.
@@ -0,0 +1,10 @@
1
+ APP_PORT=8000
2
+ APP_KEY=75ce1d709b165b03f6806dcec02b9f97
3
+ APP_LOG=false
4
+
5
+ DB_DIALECT=sqlite
6
+ DB_HOST=127.0.0.1
7
+ DB_NAME=
8
+ DB_USER=
9
+ DB_PASS=
10
+ DB_STORAGE=database.sqlite
@@ -0,0 +1,10 @@
1
+ APP_PORT=8000
2
+ APP_KEY=
3
+ APP_LOG=console.log
4
+
5
+ DB_DIALECT=sqlite
6
+ DB_HOST=127.0.0.1
7
+ DB_NAME=
8
+ DB_USER=
9
+ DB_PASS=
10
+ DB_STORAGE=:memory:
@@ -0,0 +1,7 @@
1
+
2
+ APP_PORT=8000
3
+ APP_KEY=my_secret_key
4
+ APP_LOG=false
5
+
6
+ DB_DIALECT=sqlite
7
+ DB_STORAGE=:memory:
@@ -0,0 +1,4 @@
1
+ /node_modules
2
+ /access.log
3
+ /.env
4
+ database.sqlite
@@ -0,0 +1,179 @@
1
+ # sip expressapi template
2
+
3
+ Express based REST API template
4
+
5
+ ## Install
6
+
7
+ ```cmd
8
+ npm install
9
+ ```
10
+
11
+ ## Copy config file
12
+
13
+ Copy **config/default.json.example** to **config/default.json** file.
14
+
15
+ ## APP KEY generation
16
+
17
+ Run the genkey:
18
+
19
+ ```cmd
20
+ node op key:generate
21
+ ```
22
+
23
+ ## Database settings
24
+
25
+ The database settings can be found at the following location:
26
+
27
+ * config/default.json
28
+
29
+ ### Database dialect
30
+
31
+ The default database is an in-memory database. Its contents are cleared after the server is restarted.
32
+
33
+ One of:
34
+
35
+ * sqlite
36
+ * mariadb
37
+
38
+ After installing the appropriate dependencies, it can be used:
39
+
40
+ * mysql
41
+ * postgres
42
+ * mssql
43
+ * db2
44
+ * snowflake
45
+ * oracle
46
+
47
+ With the `sqlite` option, the usual path setting is `database.sqlite`. The default storage is :memory:, where data is stored in memory only.
48
+
49
+ ## Starting
50
+
51
+ For development:
52
+
53
+ ```cmd
54
+ npm run dev
55
+ ```
56
+
57
+ Run productum:
58
+
59
+ ```cmd
60
+ npm start
61
+ ```
62
+
63
+ ## Model and controller creation
64
+
65
+ You can generate a model and controller with the following commands:
66
+
67
+ ```bash
68
+ node op make:model something
69
+ node op make:controller something
70
+ ```
71
+
72
+ The name after the model and controller statements must be given in the singular. Controller generation automatically appends the "Controller" suffix.
73
+
74
+ ## Admin user
75
+
76
+ The admin user can be created with the following command:
77
+
78
+ ```bash
79
+ node op admin:generate
80
+ ```
81
+
82
+ The command will prompt for the password.
83
+
84
+ ## Config generation
85
+
86
+ The next command generates the default config file:
87
+
88
+ ```bash
89
+ node op conf:generate
90
+ ```
91
+
92
+ ## Database import
93
+
94
+ The database can be seeded with the following command:
95
+
96
+ ```bash
97
+ node op db:import <model_name> <file_path>
98
+ ```
99
+
100
+ The model name must be given in the singular and lowercase. The file extension must be:
101
+
102
+ * .json
103
+ * .csv
104
+
105
+ The keys in the JSON file and the field names in the CSV file must match the model fields.
106
+
107
+ If the CSV file contains quotation marks, they are automatically removed.
108
+
109
+ ## Database synchronization
110
+
111
+ Models and database tables can be synchronized, but this can be dangerous.
112
+
113
+ Database synchronization can be set up in the app/models/modrels.js file. Default values are:
114
+
115
+ ```js
116
+ { alter: true }
117
+ ```
118
+
119
+ This preserves the data and existing structure.
120
+
121
+ Possible values:
122
+
123
+ ```js
124
+ { force: true }
125
+ ```
126
+
127
+ The latter deletes the contents of the database table!
128
+
129
+ If the value is false, there is no synchronization in either case.
130
+
131
+ ## Migration
132
+
133
+ Generate a migration:
134
+
135
+ ```bash
136
+ node op make/migration thing
137
+ ```
138
+
139
+ Run all migration:
140
+
141
+ ```bash
142
+ node op migration:run
143
+ ```
144
+
145
+ Run a migration:
146
+
147
+ ```bash
148
+ node op migration:run <migration_name>
149
+ ```
150
+
151
+ Rollback a migration:
152
+
153
+ ```bash
154
+ node op migration:rollback
155
+ ```
156
+
157
+ Rollback two migrations:
158
+
159
+ ```bash
160
+ node op migration:rollback 2
161
+ ```
162
+
163
+ Reset the database:
164
+
165
+ ```bash
166
+ node op migration:reset
167
+ ```
168
+
169
+ Reset the database and run all migrations:
170
+
171
+ ```bash
172
+ node op migration:fresh
173
+ ```
174
+
175
+ ## Licence
176
+
177
+ May be freely distributed under the MIT license.
178
+
179
+ Copyright (c) 2023 Sallai András
@@ -0,0 +1,17 @@
1
+ import express from 'express'
2
+ import morgan from 'morgan'
3
+ import cors from 'cors'
4
+ import fs from 'fs'
5
+ import router from './routes/api.js'
6
+ import './models/modrels.js'
7
+
8
+ const app = express()
9
+
10
+ const logfile = 'access.log'
11
+ var accessLogStream = fs.createWriteStream(logfile, { flags: 'a' })
12
+ app.use(morgan('dev', { stream: accessLogStream }))
13
+ app.use(cors())
14
+ app.use(express.json())
15
+ app.use('/api', router);
16
+
17
+ export default app
@@ -0,0 +1,103 @@
1
+ import bcrypt from 'bcryptjs'
2
+ import jwt from 'jsonwebtoken'
3
+ import User from '../models/user.js'
4
+ import dotenv from '@dotenvx/dotenvx'
5
+ dotenv.config({ quiet: true })
6
+
7
+ const AuthController = {
8
+ async register(req, res) {
9
+ var clientError = false;
10
+ try {
11
+ if(!req.body.name ||
12
+ !req.body.email ||
13
+ !req.body.password ||
14
+ !req.body.password_confirmation) {
15
+ clientError = true
16
+ throw new Error('Error! Bad request data!')
17
+ }
18
+ if(req.body.password != req.body.password_confirmation) {
19
+ clientError = true
20
+ throw new Error('Error! The two password is not same!')
21
+ }
22
+ const user = await User.findOne({
23
+ where: { name: req.body.name }
24
+ })
25
+ if(user) {
26
+ clientError = true
27
+ throw new Error('Error! User already exists: ' + user.name)
28
+ }
29
+ AuthController.tryRegister(req, res)
30
+ } catch (error) {
31
+ if (clientError) {
32
+ res.status(400)
33
+ }else {
34
+ res.status(500)
35
+ }
36
+ await res.json({
37
+ success: false,
38
+ message: 'Error! User creation failed!',
39
+ error: error.message
40
+ })
41
+ }
42
+ },
43
+ async tryRegister(req, res) {
44
+ const user = {
45
+ name: req.body.name,
46
+ email: req.body.email,
47
+ password: bcrypt.hashSync(req.body.password)
48
+ }
49
+ const result = await User.create(user)
50
+
51
+ res.status(201).json({
52
+ succes: true,
53
+ data: result
54
+ })
55
+
56
+ },
57
+ async login(req, res) {
58
+
59
+ try {
60
+ if(!req.body.name || !req.body.password) {
61
+ res.status(400)
62
+ throw new Error('Error! Bad name or password!')
63
+ }
64
+ const user = await User.findOne({
65
+ where: { name: req.body.name }
66
+ })
67
+
68
+ if(!user) {
69
+ res.status(404)
70
+ throw new Error('Error! User not found!')
71
+ }
72
+ var passwordIsValid = await bcrypt.compare(
73
+ req.body.password,
74
+ user.dataValues.password
75
+ );
76
+ if(!passwordIsValid) {
77
+ res.status(401)
78
+ throw new Error('Error! Password is not valid!')
79
+ }
80
+ AuthController.tryLogin(req, res, user)
81
+
82
+ } catch (error) {
83
+ res.json({
84
+ success: false,
85
+ message: 'Error! The login is failed!',
86
+ error: error.message
87
+ })
88
+ }
89
+ },
90
+ async tryLogin(req, res, user) {
91
+ var token = jwt.sign({ id: user.id }, process.env.APP_KEY, {
92
+ expiresIn: 86400 //24 óra
93
+ })
94
+ res.status(200).json({
95
+ id: user.id,
96
+ name: user.name,
97
+ email: user.email,
98
+ accessToken: token
99
+ })
100
+ }
101
+ }
102
+
103
+ export default AuthController
@@ -0,0 +1,129 @@
1
+ import bcrypt from 'bcryptjs'
2
+ import User from '../models/user.js'
3
+
4
+ const UserController = {
5
+ async index(req, res) {
6
+ try {
7
+ UserController.tryIndex(req, res)
8
+ }catch(error) {
9
+ res.status(500)
10
+ res.json({
11
+ success: false,
12
+ message: 'Error! The query is failed!'
13
+ })
14
+ }
15
+ },
16
+ async tryIndex(req, res) {
17
+ const users = await User.findAll()
18
+ res.status(200)
19
+ res.json({
20
+ success: true,
21
+ data: users
22
+ })
23
+ },
24
+ async show(req, res) {
25
+ try {
26
+ await UserController.tryShow(req, res)
27
+ }catch(error) {
28
+ res.status(500)
29
+ res.json({
30
+ success: false,
31
+ message: 'Error! The query is failed!'
32
+ })
33
+ }
34
+ },
35
+ async tryShow(req, res) {
36
+ const user = await User.findByPk(req.params.id)
37
+ res.status(200)
38
+ res.json({
39
+ success: true,
40
+ data: user
41
+ })
42
+ },
43
+ async create(req, res) {
44
+ var clientError = false;
45
+ try {
46
+ if(!req.body.name ||
47
+ !req.body.email ||
48
+ !req.body.password ||
49
+ !req.body.password_confirmation) {
50
+ clientError = true
51
+ throw new Error('Error! Bad request data!')
52
+ }
53
+ if(req.body.password != req.body.password_confirmation) {
54
+ clientError = true
55
+ throw new Error('Error! The two password is not same!')
56
+ }
57
+ const user = await User.findOne({
58
+ where: { name: req.body.name }
59
+ })
60
+ if(user) {
61
+ clientError = true
62
+ throw new Error('Error! User already exists: ' + user.name)
63
+ }
64
+ await UserController.tryCreate(req, res)
65
+ }catch(error) {
66
+ if (clientError) {
67
+ res.status(400)
68
+ }else {
69
+ res.status(500)
70
+ }
71
+ res.json({
72
+ success: false,
73
+ message: 'Error! The query is failed!',
74
+ error: error.message
75
+ })
76
+ }
77
+ },
78
+ async tryCreate(req, res) {
79
+ const newUser = {
80
+ name: req.body.name,
81
+ email: req.body.email,
82
+ password: bcrypt.hashSync(req.body.password)
83
+ }
84
+ const userData = await User.create(newUser)
85
+ res.status(201)
86
+ res.json({
87
+ success: true,
88
+ data: userData
89
+ })
90
+ },
91
+ async updatePassword(req, res) {
92
+ var clientError = false;
93
+ try {
94
+ if(!req.body.password ||
95
+ !req.body.password_confirmation) {
96
+ clientError = true
97
+ throw new Error('Error! Bad request data!')
98
+ }
99
+ if(req.body.password != req.body.password_confirmation) {
100
+ clientError = true
101
+ throw new Error('Error! The two password is not same!')
102
+ }
103
+ await UserController.tryUpdatePassword(req, res)
104
+ }catch(error) {
105
+ if (clientError) {
106
+ res.status(400)
107
+ }else {
108
+ res.status(500)
109
+ }
110
+ res.json({
111
+ success: false,
112
+ message: 'Error! The query is failed!',
113
+ error: error.message
114
+ })
115
+ }
116
+ },
117
+ async tryUpdatePassword(req, res) {
118
+ const user = await User.findByPk(req.params.id)
119
+ user.password = bcrypt.hashSync(req.body.password)
120
+ await user.save()
121
+ res.status(200)
122
+ res.json({
123
+ success: true,
124
+ data: user
125
+ })
126
+ }
127
+ }
128
+
129
+ export default UserController
@@ -0,0 +1,23 @@
1
+ import Sequelize from 'sequelize'
2
+ import dotenv from '@dotenvx/dotenvx'
3
+
4
+ if (process.env.NODE_ENV === 'test') {
5
+ dotenv.config({ path: '.env.test', quiet: true })
6
+ }else {
7
+ dotenv.config({ quiet: true })
8
+ }
9
+
10
+ const sequelize = new Sequelize(
11
+ process.env.DB_NAME,
12
+ process.env.DB_USER,
13
+ process.env.DB_PASS,
14
+ {
15
+ dialect: process.env.DB_DIALECT,
16
+ storage: process.env.DB_STORAGE,
17
+ host: process.env.DB_HOST,
18
+ logging: process.env.APP_LOG === 'true' ? console.log : false,
19
+ dialectOptions: {}
20
+ }
21
+ )
22
+
23
+ export default sequelize
@@ -0,0 +1,10 @@
1
+ import app from './app.js'
2
+ import dotenv from '@dotenvx/dotenvx'
3
+
4
+ dotenv.config({ quiet: true })
5
+
6
+ const PORT = process.env.APP_PORT || 8000
7
+
8
+ app.listen(PORT, () => {
9
+ console.log(`Listening on port: ${PORT}`)
10
+ })
@@ -0,0 +1,25 @@
1
+ import jwt from 'jsonwebtoken';
2
+ import dotenv from '@dotenvx/dotenvx'
3
+ dotenv.config({ quiet: true })
4
+
5
+ const verifyToken = (req, res, next) => {
6
+ let authData = req.headers.authorization;
7
+ if(!authData) {
8
+ return res.status(403).send({
9
+ message: 'No token provided!'
10
+ })
11
+ }
12
+ let token = authData.split(' ')[1];
13
+
14
+ jwt.verify(token, process.env.APP_KEY, (err, decoded) => {
15
+ if(err) {
16
+ return res.status(401).send({
17
+ message: "Unauthorized!"
18
+ })
19
+ }
20
+ req.userId = decoded.id;
21
+ next()
22
+ })
23
+ };
24
+
25
+ export default verifyToken
@@ -0,0 +1,12 @@
1
+ import User from './user.js';
2
+ import sequelize from '../database/database.js'
3
+
4
+ const db = {};
5
+
6
+ db.User = User;
7
+
8
+ await sequelize.sync({
9
+ alter: true
10
+ })
11
+
12
+ export default db;
@@ -0,0 +1,11 @@
1
+ import { DataTypes } from 'sequelize'
2
+ import sequelize from '../database/database.js'
3
+
4
+ const User = sequelize.define('user', {
5
+ name: { type: DataTypes.STRING, allowNull: false },
6
+ email: { type: DataTypes.STRING, allowNull: true },
7
+ password: { type: DataTypes.STRING , allowNull: false },
8
+ roleId: { type: DataTypes.INTEGER, defaultValue: 0 }
9
+ })
10
+
11
+ export default User
@@ -0,0 +1,14 @@
1
+ import Router from 'express'
2
+ const router = Router()
3
+
4
+ import AuthController from '../controllers/authController.js';
5
+ import UserController from '../controllers/userController.js';
6
+ import verifyToken from '../middleware/authjwt.js';
7
+
8
+ router.post('/register', AuthController.register)
9
+ router.post('/login', AuthController.login)
10
+ router.get('/users', [verifyToken], UserController.index)
11
+ router.get('/users/:id', [verifyToken], UserController.show)
12
+ router.put('/users/:id/password', [verifyToken], UserController.updatePassword)
13
+
14
+ export default router
@@ -0,0 +1,34 @@
1
+ import { DataTypes } from 'sequelize';
2
+
3
+ async function up({context: QueryInterface}) {
4
+ await QueryInterface.createTable('users', {
5
+ id: {
6
+ allowNull: false,
7
+ autoIncrement: true,
8
+ primaryKey: true,
9
+ type: DataTypes.INTEGER
10
+ },
11
+ name: {
12
+ type: DataTypes.STRING,
13
+ allowNull: false
14
+ },
15
+ email: {
16
+ type: DataTypes.STRING,
17
+ allowNull: true
18
+ },
19
+ password: {
20
+ type: DataTypes.STRING,
21
+ allowNull: false
22
+ },
23
+ roleId: {
24
+ type: DataTypes.INTEGER,
25
+ defaultValue: 0
26
+ }
27
+ });
28
+ }
29
+
30
+ async function down({context: QueryInterface}) {
31
+ await QueryInterface.dropTable('users');
32
+ }
33
+
34
+ export { up, down }
@@ -0,0 +1,27 @@
1
+ # expressapi
2
+
3
+ ## API KEY
4
+
5
+ The app key generated with generate-api-key package.
6
+
7
+ ## Testing
8
+
9
+ Run tests:
10
+
11
+ ```cmd
12
+ npm test
13
+ ```
14
+
15
+ The test using the .env.test file and run in the memory database.
16
+
17
+ Tests can be placed in the test directory, where Mocha runs them.
18
+
19
+ ## Development
20
+
21
+ Start the application:
22
+
23
+ ```cmd
24
+ npm run dev
25
+ ```
26
+
27
+ Restarting the application when saving is done by nodemon.