Commit 65e9618b authored by Цой Кирилл's avatar Цой Кирилл

Получение пользователя че6рез jwt токен

parent d8a0d9c0
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 !== 'bearer' || !token){
if (bearer !== 'Bearer' || !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" })
}
}
......
......@@ -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]
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment