Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ajs12_shop
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
Egor Kremnev
ajs12_shop
Commits
0ecb4814
Commit
0ecb4814
authored
May 03, 2023
by
Egor Kremnev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add authenticate
parent
da105c4b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
642 additions
and
9 deletions
+642
-9
.gitignore
api_client/db/.gitignore
+0
-1
index.js
api_client/index.js
+2
-0
User.js
api_client/models/User.js
+51
-0
package-lock.json
api_client/package-lock.json
+535
-8
package.json
api_client/package.json
+1
-0
users.js
api_client/routes/users.js
+53
-0
No files found.
api_client/db/.gitignore
deleted
100644 → 0
View file @
da105c4b
*.json
api_client/index.js
View file @
0ecb4814
...
...
@@ -5,12 +5,14 @@ const {port, db: dbConfig} = require('./config');
const
createProductsRoutes
=
require
(
'./routes/products'
);
const
mongoose
=
require
(
'mongoose'
);
const
categoryRoutes
=
require
(
'./routes/categories'
);
const
userRoutes
=
require
(
'./routes/users'
);
app
.
use
(
cors
());
app
.
use
(
express
.
json
());
app
.
use
(
express
.
static
(
'public'
));
app
.
use
(
'/api/v1/products'
,
createProductsRoutes
());
app
.
use
(
'/api/v1/categories'
,
categoryRoutes
);
app
.
use
(
'/api/v1/users'
,
userRoutes
);
const
run
=
async
()
=>
{
await
mongoose
.
connect
(
...
...
api_client/models/User.js
0 → 100644
View file @
0ecb4814
const
mongoose
=
require
(
'mongoose'
);
const
Schema
=
mongoose
.
Schema
;
const
bcrypt
=
require
(
'bcrypt'
);
const
{
nanoid
}
=
require
(
'fix-esm'
).
require
(
'nanoid'
);
const
SALT_WORK_FACTOR
=
10
;
const
UserSchema
=
new
Schema
({
password
:
{
type
:
String
,
required
:
true
},
username
:
{
type
:
String
,
required
:
true
,
unique
:
true
},
token
:
{
type
:
String
,
required
:
false
,
unique
:
true
}
});
UserSchema
.
pre
(
'save'
,
async
function
(
next
)
{
if
(
!
this
.
isModified
(
'password'
))
return
next
();
const
salt
=
await
bcrypt
.
genSalt
(
SALT_WORK_FACTOR
);
this
.
password
=
await
bcrypt
.
hash
(
this
.
password
,
salt
);
next
();
});
UserSchema
.
set
(
'toJSON'
,
{
transform
:
(
doc
,
ret
,
options
)
=>
{
delete
ret
.
password
;
return
ret
;
}
});
UserSchema
.
methods
.
checkPassword
=
function
(
password
)
{
return
bcrypt
.
compare
(
password
,
this
.
password
)
};
UserSchema
.
methods
.
generateToken
=
function
()
{
this
.
token
=
nanoid
();
};
const
User
=
mongoose
.
model
(
'User'
,
UserSchema
);
module
.
exports
=
User
;
api_client/package-lock.json
View file @
0ecb4814
This diff is collapsed.
Click to expand it.
api_client/package.json
View file @
0ecb4814
...
...
@@ -12,6 +12,7 @@
"author"
:
""
,
"license"
:
"ISC"
,
"dependencies"
:
{
"bcrypt"
:
"^5.1.0"
,
"cors"
:
"^2.8.5"
,
"express"
:
"^4.18.2"
,
"fix-esm"
:
"^1.0.1"
,
...
...
api_client/routes/users.js
0 → 100644
View file @
0ecb4814
const
router
=
require
(
'express'
).
Router
();
const
User
=
require
(
'../models/User'
);
router
.
post
(
'/'
,
async
(
req
,
res
)
=>
{
try
{
const
user
=
new
User
(
req
.
body
);
user
.
generateToken
();
await
user
.
save
();
res
.
send
(
user
);
}
catch
(
e
)
{
res
.
status
(
400
).
send
(
e
);
}
});
router
.
post
(
'/login'
,
async
(
req
,
res
)
=>
{
const
user
=
await
User
.
findOne
({
username
:
req
.
body
.
username
});
if
(
!
user
)
return
res
.
status
(
400
)
.
send
({
error
:
'Username or password incorrect'
});
if
(
!
await
user
.
checkPassword
(
req
.
body
.
password
.
toString
()))
return
res
.
status
(
400
)
.
send
({
error
:
'Username or password incorrect'
});
user
.
generateToken
();
await
user
.
save
();
res
.
send
({
message
:
"User success authenticated"
,
user
});
});
router
.
get
(
'/profile'
,
async
(
req
,
res
)
=>
{
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
(
'Token is wrong'
);
res
.
send
({
message
:
"Большой большой сикрет"
,
username
:
user
.
username
});
});
module
.
exports
=
router
;
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