Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
auth
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
Цой Кирилл
auth
Commits
65e9618b
Commit
65e9618b
authored
Aug 03, 2024
by
Цой Кирилл
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Получение пользователя че6рез jwt токен
parent
d8a0d9c0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
5 deletions
+13
-5
jwtAuthGuard.ts
auth-service/src/auth/guards/jwtAuthGuard.ts
+10
-4
users.module.ts
auth-service/src/users/users.module.ts
+3
-1
No files found.
auth-service/src/auth/guards/jwtAuthGuard.ts
View file @
65e9618b
import
{
CanActivate
,
ExecutionContext
,
UnauthorizedException
}
from
'@nestjs/common'
;
import
{
CanActivate
,
ExecutionContext
,
Injectable
,
UnauthorizedException
}
from
'@nestjs/common'
;
import
{
Observable
}
from
'rxjs'
;
import
{
JwtService
}
from
'@nestjs/jwt'
;
@
Injectable
()
export
class
JwtAuthGuard
implements
CanActivate
{
constructor
(
...
...
@@ -14,17 +14,23 @@ export class JwtAuthGuard implements CanActivate {
const
req
=
context
.
switchToHttp
().
getRequest
()
const
authHeader
=
req
.
headers
.
authorization
console
.
log
(
authHeader
)
const
bearer
=
authHeader
.
split
(
" "
)[
0
]
const
token
=
authHeader
.
split
(
" "
)[
1
]
if
(
bearer
!==
'
b
earer'
||
!
token
){
if
(
bearer
!==
'
B
earer'
||
!
token
){
throw
new
UnauthorizedException
({
message
:
"Unauth"
})
}
const
user
=
this
.
jwtService
.
verify
(
token
)
const
user
=
this
.
jwtService
.
verify
(
token
,
{
secret
:
"secret"
})
console
.
log
(
user
)
return
true
}
catch
(
error
){
console
.
log
(
error
)
throw
new
UnauthorizedException
({
message
:
"Unauth"
})
}
}
...
...
auth-service/src/users/users.module.ts
View file @
65e9618b
...
...
@@ -3,12 +3,14 @@ import { UsersService } from './users.service';
import
{
UsersController
}
from
'./users.controller'
;
import
{
SequelizeModule
}
from
'@nestjs/sequelize'
;
import
{
User
}
from
'./models/user.entity'
;
import
{
JwtModule
,
JwtService
}
from
'@nestjs/jwt'
;
@
Module
({
providers
:
[
UsersService
],
controllers
:
[
UsersController
],
imports
:
[
SequelizeModule
.
forFeature
([
User
])
SequelizeModule
.
forFeature
([
User
]),
JwtModule
],
exports
:
[
UsersService
]
})
...
...
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