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
08b0c7d5
Commit
08b0c7d5
authored
Jan 11, 2021
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец занятия №85
parent
b7814c0d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
8 deletions
+32
-8
auth.js
app/middleware/auth.js
+19
-0
User.js
app/models/User.js
+11
-0
products.js
app/products.js
+2
-8
No files found.
app/middleware/auth.js
0 → 100644
View file @
08b0c7d5
const
User
=
require
(
'../models/User'
);
const
auth
=
async
(
req
,
res
,
next
)
=>
{
const
token
=
req
.
get
(
"Authorization"
);
if
(
!
token
)
{
return
res
.
status
(
401
).
send
({
error
:
"No token present"
});
}
const
user
=
await
User
.
findOne
({
token
});
if
(
!
user
)
{
return
res
.
status
(
401
).
send
({
error
:
"Wrong token"
});
}
req
.
user
=
user
;
next
();
};
module
.
exports
=
auth
;
\ No newline at end of file
app/models/User.js
View file @
08b0c7d5
...
...
@@ -10,6 +10,17 @@ const UserSchema = new Schema({
username
:
{
type
:
String
,
unique
:
true
,
required
:
true
,
validate
:
{
validator
:
async
value
=>
{
const
user
=
await
User
.
findOne
({
username
:
value
});
if
(
user
)
return
false
;
},
message
:
'User is already registered!'
}
},
email
:
{
type
:
String
,
required
:
true
},
password
:
{
...
...
app/products.js
View file @
08b0c7d5
...
...
@@ -6,6 +6,7 @@ const {nanoid} = require('nanoid');
const
config
=
require
(
'./config'
);
const
Product
=
require
(
'./models/Products'
);
const
User
=
require
(
"./models/User"
);
const
auth
=
require
(
'./middleware/auth'
);
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
...
...
@@ -40,14 +41,7 @@ const createRouter = () => {
}
});
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
async
(
req
,
res
)
=>
{
const
token
=
req
.
get
(
"Authorization"
);
const
user
=
await
User
.
findOne
({
token
});
if
(
!
user
)
{
return
res
.
status
(
401
).
send
({
error
:
"Wrong token"
});
}
router
.
post
(
'/'
,
auth
,
upload
.
single
(
'image'
),
async
(
req
,
res
)
=>
{
const
product
=
new
Product
(
req
.
body
);
if
(
req
.
file
)
{
product
.
image
=
req
.
file
.
filename
;
...
...
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