Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
Homework_95_Tsoy_Danil
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
Цой Данил
Homework_95_Tsoy_Danil
Commits
569a1e4f
Commit
569a1e4f
authored
Apr 11, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Started working with backend/specially user methods: login, create and etc
parent
671f3ad8
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
235 additions
and
31 deletions
+235
-31
.env
backend/.env
+1
-1
package-lock.json
backend/package-lock.json
+1
-28
package.json
backend/package.json
+1
-2
healthCheck.ts
backend/src/controllers/healthCheck.ts
+20
-0
EStatuses.ts
backend/src/enum/EStatuses.ts
+4
-0
generateJWT.ts
backend/src/helpers/generateJWT.ts
+7
-0
IMessage.ts
backend/src/interfaces/IMessage.ts
+8
-0
IMessageDto.ts
backend/src/interfaces/IMessageDto.ts
+6
-0
IModifiedRequest.ts
backend/src/interfaces/IModifiedRequest.ts
+6
-0
IResponse.ts
backend/src/interfaces/IResponse.ts
+7
-0
IUser.ts
backend/src/interfaces/IUser.ts
+8
-0
IUserCreateDto.ts
backend/src/interfaces/IUserCreateDto.ts
+7
-0
IUserGetDto.ts
backend/src/interfaces/IUserGetDto.ts
+8
-0
auth.ts
backend/src/middlewares/auth.ts
+29
-0
User.ts
backend/src/models/User.ts
+38
-0
mongooseDB.ts
backend/src/repository/mongooseDB.ts
+84
-0
No files found.
backend/.env
View file @
569a1e4f
APP_PORT=8000
APP_PORT=8000
PG_HOST=localhost
PG_HOST=localhost
MONGO_CLIENT_URL=mongodb://localhost/TsoyDanil
Music
DB
MONGO_CLIENT_URL=mongodb://localhost/TsoyDanil
Chat
DB
ENV_SALT=10
ENV_SALT=10
SECRET_KEY=key
SECRET_KEY=key
\ No newline at end of file
backend/package-lock.json
View file @
569a1e4f
...
@@ -25,8 +25,7 @@
...
@@ -25,8 +25,7 @@
"sequelize"
:
"^6.29.3"
,
"sequelize"
:
"^6.29.3"
,
"sequelize-typescript"
:
"^2.1.5"
,
"sequelize-typescript"
:
"^2.1.5"
,
"shortid"
:
"^2.2.16"
,
"shortid"
:
"^2.2.16"
,
"uuid"
:
"^9.0.0"
,
"uuid"
:
"^9.0.0"
"ws"
:
"^8.13.0"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"@types/bcrypt"
:
"^5.0.0"
,
"@types/bcrypt"
:
"^5.0.0"
,
...
@@ -2660,26 +2659,6 @@
...
@@ -2660,26 +2659,6 @@
"resolved"
:
"https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz"
,
"resolved"
:
"https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz"
,
"integrity"
:
"sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
"integrity"
:
"sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
},
},
"node_modules/ws"
:
{
"version"
:
"8.13.0"
,
"resolved"
:
"https://registry.npmjs.org/ws/-/ws-8.13.0.tgz"
,
"integrity"
:
"sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA=="
,
"engines"
:
{
"node"
:
">=10.0.0"
},
"peerDependencies"
:
{
"bufferutil"
:
"^4.0.1"
,
"utf-8-validate"
:
">=5.0.2"
},
"peerDependenciesMeta"
:
{
"bufferutil"
:
{
"optional"
:
true
},
"utf-8-validate"
:
{
"optional"
:
true
}
}
},
"node_modules/xtend"
:
{
"node_modules/xtend"
:
{
"version"
:
"4.0.2"
,
"version"
:
"4.0.2"
,
"resolved"
:
"https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz"
,
"resolved"
:
"https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz"
,
...
@@ -4707,12 +4686,6 @@
...
@@ -4707,12 +4686,6 @@
"resolved"
:
"https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz"
,
"resolved"
:
"https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz"
,
"integrity"
:
"sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
"integrity"
:
"sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
},
},
"ws"
:
{
"version"
:
"8.13.0"
,
"resolved"
:
"https://registry.npmjs.org/ws/-/ws-8.13.0.tgz"
,
"integrity"
:
"sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA=="
,
"requires"
:
{}
},
"xtend"
:
{
"xtend"
:
{
"version"
:
"4.0.2"
,
"version"
:
"4.0.2"
,
"resolved"
:
"https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz"
,
"resolved"
:
"https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz"
,
...
...
backend/package.json
View file @
569a1e4f
...
@@ -27,8 +27,7 @@
...
@@ -27,8 +27,7 @@
"sequelize"
:
"^6.29.3"
,
"sequelize"
:
"^6.29.3"
,
"sequelize-typescript"
:
"^2.1.5"
,
"sequelize-typescript"
:
"^2.1.5"
,
"shortid"
:
"^2.2.16"
,
"shortid"
:
"^2.2.16"
,
"uuid"
:
"^9.0.0"
,
"uuid"
:
"^9.0.0"
"ws"
:
"^8.13.0"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"@types/bcrypt"
:
"^5.0.0"
,
"@types/bcrypt"
:
"^5.0.0"
,
...
...
backend/src/controllers/healthCheck.ts
0 → 100644
View file @
569a1e4f
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
export
class
HealthCheckController
{
private
router
:
Router
constructor
()
{
this
.
router
=
express
.
Router
()
this
.
router
.
get
(
'/'
,
this
.
checkHealth
)
}
public
getRouter
=
():
Router
=>
{
return
this
.
router
}
private
checkHealth
=
(
req
:
Request
,
res
:
Response
):
void
=>
{
res
.
send
(
'Server is OK'
)
}
}
export
const
healthCheckController
=
new
HealthCheckController
()
\ No newline at end of file
backend/src/enum/EStatuses.ts
0 → 100644
View file @
569a1e4f
export
const
enum
EStatuses
{
SUCCESS
=
'Success'
,
FAILURE
=
'Failure'
}
\ No newline at end of file
backend/src/helpers/generateJWT.ts
0 → 100644
View file @
569a1e4f
import
jwt
from
'jsonwebtoken'
import
{
Types
}
from
'mongoose'
export
const
generateJWT
=
(
payload
:
{[
key
:
string
]:
number
|
string
|
boolean
|
Types
.
ObjectId
})
=>
{
return
jwt
.
sign
(
payload
,
process
.
env
.
SECRET_KEY
||
''
,
{
expiresIn
:
'24h'
})
}
backend/src/interfaces/IMessage.ts
0 → 100644
View file @
569a1e4f
import
{
Types
}
from
"mongoose"
;
import
IUser
from
"./IUser"
;
export
default
interface
IMessage
{
_id
:
Types
.
ObjectId
author
:
IUser
[
'username'
]
message
:
string
}
\ No newline at end of file
backend/src/interfaces/IMessageDto.ts
0 → 100644
View file @
569a1e4f
import
IMessage
from
"./IMessage"
;
export
default
interface
IMessageDto
{
author
:
IMessage
[
'author'
]
message
:
IMessage
[
'message'
]
}
\ No newline at end of file
backend/src/interfaces/IModifiedRequest.ts
0 → 100644
View file @
569a1e4f
import
{
Request
}
from
"express"
;
import
{
JwtPayload
}
from
"jsonwebtoken"
;
export
default
interface
IModifiedRequest
extends
Request
{
verifiedData
:
string
|
JwtPayload
}
\ No newline at end of file
backend/src/interfaces/IResponse.ts
0 → 100644
View file @
569a1e4f
import
{
EStatuses
}
from
"../enum/EStatuses"
;
export
default
interface
IResponse
<
T
=
null
>
{
status
:
EStatuses
result
:
T
message
:
string
}
\ No newline at end of file
backend/src/interfaces/IUser.ts
0 → 100644
View file @
569a1e4f
import
{
Document
,
Types
}
from
"mongoose"
export
default
interface
IUser
extends
Document
{
_id
:
Types
.
ObjectId
username
:
string
password
:
string
checkPassword
:
(
pass
:
string
)
=>
boolean
}
\ No newline at end of file
backend/src/interfaces/IUserCreateDto.ts
0 → 100644
View file @
569a1e4f
import
IUser
from
"./IUser"
;
export
default
interface
IUserCreateDto
{
username
:
IUser
[
'username'
]
password
:
IUser
[
'password'
]
}
backend/src/interfaces/IUserGetDto.ts
0 → 100644
View file @
569a1e4f
import
{
Types
}
from
"mongoose"
;
import
IUSer
from
"./IUser"
;
export
default
interface
IUserGetDto
{
_id
:
Types
.
ObjectId
username
:
IUSer
[
'username'
]
token
:
string
}
\ No newline at end of file
backend/src/middlewares/auth.ts
0 → 100644
View file @
569a1e4f
import
{
NextFunction
,
Request
,
Response
}
from
"express"
;
import
{
EStatuses
}
from
"../enum/EStatuses"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
jwt
from
'jsonwebtoken'
import
IModifiedRequest
from
"../interfaces/IModifiedRequest"
;
export
const
auth
=
(
expressReq
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
const
req
=
expressReq
as
IModifiedRequest
if
(
req
.
method
===
'OPTIONS'
)
{
next
()
}
try
{
const
token
=
req
.
header
(
'Authorization'
)?.
replace
(
'Bearer '
,
''
);
if
(
!
token
){
throw
new
Error
(
'Token not provided'
)
}
const
verifiedData
=
jwt
.
verify
(
token
,
process
.
env
.
SECRET_KEY
as
string
);
req
.
verifiedData
=
verifiedData
next
()
}
catch
(
err
:
unknown
){
const
error
=
err
as
Error
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
FAILURE
,
result
:
undefined
,
message
:
error
.
message
}
res
.
status
(
418
).
send
(
response
)
}
}
\ No newline at end of file
backend/src/models/User.ts
0 → 100644
View file @
569a1e4f
import
mongoose
,
{
Schema
}
from
"mongoose"
;
import
bcrypt
from
"bcrypt"
;
import
IUser
from
"../interfaces/IUser"
;
const
UserSchema
:
Schema
=
new
Schema
<
IUser
>
({
username
:
{
type
:
String
,
unique
:
true
,
trim
:
true
,
minlength
:
1
,
required
:
[
true
,
'Username should exist'
]
},
password
:
{
type
:
String
,
trim
:
true
,
minlength
:
1
,
required
:
[
true
,
'Password should exist'
]
}
},{
versionKey
:
false
})
UserSchema
.
pre
(
'save'
,
async
function
(
next
){
if
(
!
this
.
isModified
(
'password'
))
return
next
()
const
salt
=
await
bcrypt
.
genSalt
()
const
hash
=
await
bcrypt
.
hash
(
this
.
password
,
salt
)
this
.
password
=
hash
next
()
})
UserSchema
.
set
(
'toJSON'
,
{
transform
(
doc
,
ret
,
options
)
{
delete
ret
.
password
return
ret
}})
UserSchema
.
methods
.
checkPassword
=
async
function
(
password
:
string
)
{
return
await
bcrypt
.
compare
(
password
,
this
.
password
);
}
export
const
User
=
mongoose
.
model
<
IUser
>
(
'User'
,
UserSchema
)
\ No newline at end of file
backend/src/repository/mongooseDB.ts
0 → 100644
View file @
569a1e4f
import
dotenv
from
'dotenv'
import
mongoose
,
{
Mongoose
}
from
'mongoose'
import
IResponse
from
'../interfaces/IResponse'
import
{
EStatuses
}
from
'../enum/EStatuses'
import
IUserGetDto
from
'../interfaces/IUserGetDto'
import
{
generateJWT
}
from
'../helpers/generateJWT'
import
IUserCreateDto
from
'../interfaces/IUserCreateDto'
import
{
User
}
from
'../models/User'
dotenv
.
config
()
export
class
MongooseDB
{
private
client
:
Mongoose
|
null
=
null
public
close
=
async
()
=>
{
if
(
!
this
.
client
)
return
await
this
.
client
.
disconnect
();
}
public
init
=
async
():
Promise
<
void
>
=>
{
try
{
this
.
client
=
await
mongoose
.
connect
(
process
.
env
.
MONGO_CLIENT_URL
||
''
)
console
.
log
(
'Server connected to MongoDB'
);
}
catch
(
err
)
{
const
error
=
err
as
Error
;
console
.
error
(
'Connected error MongooseDB:'
,
error
);
}
}
public
createUser
=
async
(
userDto
:
IUserCreateDto
):
Promise
<
IResponse
<
IUserGetDto
|
null
>>
=>
{
try
{
const
user
=
new
User
(
userDto
)
await
user
.
save
()
const
data
:
IUserGetDto
=
{
_id
:
user
.
_id
,
username
:
user
.
username
,
token
:
generateJWT
({
_id
:
user
.
_id
,
username
:
user
.
username
})
}
const
response
:
IResponse
<
IUserGetDto
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
data
,
message
:
'User added'
}
return
response
}
catch
(
err
:
unknown
){
const
error
=
err
as
Error
const
response
:
IResponse
<
null
>
=
{
status
:
EStatuses
.
FAILURE
,
result
:
null
,
message
:
error
.
message
}
return
response
}
}
public
loginUser
=
async
(
userDto
:
IUserCreateDto
):
Promise
<
IResponse
<
IUserGetDto
|
null
>>
=>
{
try
{
const
user
=
await
User
.
findOne
({
username
:
userDto
.
username
})
if
(
!
user
)
throw
new
Error
(
'User not found'
)
const
isMatch
:
boolean
=
await
user
.
checkPassword
(
userDto
.
password
)
if
(
!
isMatch
)
throw
new
Error
(
'Wrong password'
)
const
data
=
{
_id
:
user
.
_id
,
username
:
user
.
username
,
token
:
generateJWT
({
_id
:
user
.
_id
,
username
:
user
.
username
})
}
await
user
.
save
()
const
response
:
IResponse
<
IUserGetDto
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
data
,
message
:
'Access granted'
}
return
response
}
catch
(
err
:
unknown
){
const
error
=
err
as
Error
const
response
:
IResponse
<
null
>
=
{
status
:
EStatuses
.
FAILURE
,
result
:
null
,
message
:
error
.
message
}
return
response
}
}
}
\ No newline at end of file
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