-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
190 lines (163 loc) · 5.3 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require('dotenv').config()
const fs = require('fs')
const cors = require('cors')
const path = require('path')
const bcrypt = require('bcrypt')
const multer = require('multer')
const JWT = require('jsonwebtoken')
const jsonServer = require('json-server')
const UPLOAD_FOLDER = process.env.UPLOAD_FOLDER || 'public'
const AUTH_READ = process.env.AUTH_READ === 'yes'
const AUTH_WRITE = process.env.AUTH_WRITE !== 'no'
const SECRET_KEY = process.env.SECRET_KEY || 'Annie is Vader'
const JWT_EXPIRATION = process.env.JWT_EXPIRATION || '24h'
const PORT = process.env.PORT || 8000;
const SALT = bcrypt.genSaltSync(process.env.SALT || 10)
const dbFileName = process.env.DB_FILE || 'db.json'
const dbFilePath = path.join(__dirname, dbFileName)
const getDB = () => {
const fileContents = fs.readFileSync(dbFilePath)
return JSON.parse(fileContents)
}
const saveDB = (db) => {
fs.writeFileSync(dbFilePath, JSON.stringify(db))
}
const initDB = () => {
db = {}
if (fs.existsSync(dbFilePath)) {
db = getDB()
}
const users = db.users || null
if (!users) {
db.users = []
}
saveDB(db)
}
const getUsers = () => {
try {
const db = getDB()
return Array.isArray(db.users) ? db.users : []
} catch (err) {
console.error('Error while retrieving users', err)
return []
}
}
const getAuthenticatedUser = (username, password) => {
const encryptedPassword = bcrypt.hashSync(password, SALT)
const users = getUsers()
const user = users.find(user => user.username === username)
if (user && bcrypt.compareSync(password, user.password)) {
return user
}
return null
}
const findUserById = (userId) => {
const users = getUsers();
const user = users.find(user => user.id === userId);
return user;
}
const encryptPassword = password => {
return bcrypt.hashSync(password, SALT)
}
const userExists = username => {
const users = getUsers()
const findedUser = users.find(user => user.username === username) || null
return findedUser
}
const createToken = payload => JWT.sign(payload, SECRET_KEY, { expiresIn: JWT_EXPIRATION });
const verifyToken = token => JWT.verify(token, SECRET_KEY, (err, decode) => ({ err, decode }));
const checkAuth = (req, res, next) => {
try {
const [bearer, token] = req.headers.authorization.split(' ')
const { err, decode } = verifyToken(token)
if (err) {
throw err
}
req.body.userId = decode.userId
if (req.method === 'POST' || req.method === 'PUT') {
req.body.updatedAt = new Date().toISOString()
}
return next();
} catch (err) {
const status = 401
const message = 'Wrong access token'
return res.status(status).json({ status, message })
}
}
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, UPLOAD_FOLDER);
},
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
initDB()
const server = jsonServer.create();
const router = jsonServer.router(dbFilePath)
const middlewares = jsonServer.defaults()
server.use(cors())
server.use(middlewares)
server.use(jsonServer.bodyParser)
server.post('/auth/login', (req, res) => {
const { username, password } = req.body
if (username && password) {
const authenticatedUser = getAuthenticatedUser(username, password)
if (!authenticatedUser) {
const status = 401
const message = 'Wrong username/password'
return res.status(status).json({ message })
}
const accessToken = createToken({
userId: authenticatedUser.id,
username: authenticatedUser.username
});
return res.status(201).json({ accessToken })
}
return res.status(400).json({ message: 'username and password needed.' })
});
server.post('/auth/register', (req, res, next) => {
const { username, password } = req.body
if (username && password) {
if (userExists(username)) {
return res.status(400).json({ message: 'Username is taken' })
}
req.url = '/users/'
req.body.password = encryptPassword(password)
router.handle(req, res, next)
return res
}
return res.status(400).json({ message: 'username and password needed.' })
});
server.get('/auth/me', checkAuth, (req, res) => {
const userId = req.body.userId;
const loggedUser = findUserById(userId);
return res.status(200).json(loggedUser)
})
if (AUTH_READ) {
server.get(/^\/api/, checkAuth)
}
if (AUTH_WRITE) {
server.put(/^\/api/, checkAuth)
server.post(/^\/api/, checkAuth)
server.delete(/^\/api/, checkAuth)
server.post('/upload', checkAuth)
}
server.post('/upload', (req, res) => {
let upload = multer({ storage }).single('file')
upload(req, res, function(err) {
if (req.fileValidationError) {
return res.status(400).json({ message: req.fileValidationError });
} else if (!req.file) {
return res.status(400).json({ message: 'file field is required' });
} else if (err instanceof multer.MulterError) {
return res.status(400).json({ message: err });
} else if (err) {
return res.status(400).json({ message: err });
}
const path = `${req.protocol}://${req.get('host')}/${req.file.path.replace(UPLOAD_FOLDER, '').replace('\\', '/').replace('//', '/')}`
return res.status(201).json({ path })
});
})
server.use('/api/', router)
server.listen(PORT, () => { console.log(`JSON Server is running on port ${PORT}`) })