Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
shop-api-js5
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vadim
shop-api-js5
Commits
ac05badd
Commit
ac05badd
authored
Jan 25, 2021
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец занятия №89
parent
978516a0
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
31 additions
and
4 deletions
+31
-4
mongoDB.js
app/db/mongoDB.js
+1
-1
permit.js
app/middleware/permit.js
+14
-0
User.js
app/models/User.js
+6
-0
products.js
app/products.js
+3
-2
users.js
app/users.js
+5
-1
fixtures.js
fixtures.js
+2
-0
No files found.
app/db/mongoDB.js
View file @
ac05badd
...
...
@@ -5,7 +5,7 @@ let client = null;
const
connect
=
async
()
=>
{
client
=
await
MongoClient
.
connect
(
"mongodb://localhost"
);
client
=
await
MongoClient
.
connect
(
"mongodb://localhost"
,
{
useNewUrlParser
:
true
}
);
db
=
client
.
db
(
'shop'
)
};
...
...
app/middleware/permit.js
0 → 100644
View file @
ac05badd
const
permit
=
(...
roles
)
=>
{
return
(
req
,
res
,
next
)
=>
{
if
(
!
req
.
user
)
{
return
res
.
status
(
401
).
send
({
message
:
"Unauthenticated"
});
}
if
(
!
roles
.
includes
(
req
.
user
.
role
))
{
return
res
.
status
(
403
).
send
({
message
:
"Unauthorized"
});
}
next
();
}
};
module
.
exports
=
permit
;
\ No newline at end of file
app/models/User.js
View file @
ac05badd
...
...
@@ -30,6 +30,12 @@ const UserSchema = new Schema({
token
:
{
type
:
String
,
required
:
true
},
role
:
{
type
:
String
,
required
:
true
,
enum
:
[
'user'
,
'admin'
],
default
:
'user'
}
});
...
...
app/products.js
View file @
ac05badd
...
...
@@ -7,6 +7,7 @@ const config = require('./config');
const
Product
=
require
(
'./models/Products'
);
const
User
=
require
(
"./models/User"
);
const
auth
=
require
(
'./middleware/auth'
);
const
permit
=
require
(
'./middleware/permit'
);
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
...
...
@@ -41,12 +42,12 @@ const createRouter = () => {
}
});
router
.
post
(
'/'
,
auth
,
upload
.
single
(
'image'
)
,
async
(
req
,
res
)
=>
{
router
.
post
(
'/'
,
[
auth
,
permit
(
'admin'
),
upload
.
single
(
'image'
)]
,
async
(
req
,
res
)
=>
{
const
product
=
new
Product
(
req
.
body
);
if
(
req
.
file
)
{
product
.
image
=
req
.
file
.
filename
;
}
product
.
user
=
user
.
_id
;
product
.
user
=
req
.
user
.
_id
;
try
{
await
product
.
save
();
res
.
send
(
product
);
...
...
app/users.js
View file @
ac05badd
...
...
@@ -10,7 +10,11 @@ const createRouter = () => {
});
router
.
post
(
"/"
,
async
(
req
,
res
)
=>
{
try
{
const
user
=
new
User
(
req
.
body
);
const
user
=
new
User
({
username
:
req
.
body
.
username
,
email
:
req
.
body
.
email
,
password
:
req
.
body
.
password
});
user
.
generateToken
();
await
user
.
save
();
res
.
send
(
user
);
...
...
fixtures.js
View file @
ac05badd
...
...
@@ -41,11 +41,13 @@ db.once('open', async () => {
username
:
'admin'
,
email
:
'admin@admin.com'
,
password
:
'123456'
,
role
:
'admin'
,
token
:
nanoid
()
},
{
username
:
'user'
,
email
:
'user@user.com'
,
password
:
'123456'
,
role
:
'user'
,
token
:
nanoid
()
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment