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 { Observable } from 'rxjs';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
@Injectable()
export class JwtAuthGuard implements CanActivate { export class JwtAuthGuard implements CanActivate {
constructor( constructor(
...@@ -14,17 +14,23 @@ export class JwtAuthGuard implements CanActivate { ...@@ -14,17 +14,23 @@ export class JwtAuthGuard implements CanActivate {
const req = context.switchToHttp().getRequest() const req = context.switchToHttp().getRequest()
const authHeader = req.headers.authorization const authHeader = req.headers.authorization
console.log(authHeader)
const bearer = authHeader.split(" ")[0] const bearer = authHeader.split(" ")[0]
const token = authHeader.split(" ")[1] const token = authHeader.split(" ")[1]
if (bearer !== 'bearer' || !token){ if (bearer !== 'Bearer' || !token){
throw new UnauthorizedException({ message: "Unauth" }) throw new UnauthorizedException({ message: "Unauth" })
} }
const user = this.jwtService.verify(token) const user = this.jwtService.verify(token, {secret: "secret"})
console.log(user)
return true return true
} catch (error){ } catch (error){
console.log(error)
throw new UnauthorizedException({ message: "Unauth" }) throw new UnauthorizedException({ message: "Unauth" })
} }
} }
......
...@@ -3,12 +3,14 @@ import { UsersService } from './users.service'; ...@@ -3,12 +3,14 @@ import { UsersService } from './users.service';
import { UsersController } from './users.controller'; import { UsersController } from './users.controller';
import { SequelizeModule } from '@nestjs/sequelize'; import { SequelizeModule } from '@nestjs/sequelize';
import { User } from './models/user.entity'; import { User } from './models/user.entity';
import { JwtModule, JwtService } from '@nestjs/jwt';
@Module({ @Module({
providers: [UsersService], providers: [UsersService],
controllers: [UsersController], controllers: [UsersController],
imports: [ imports: [
SequelizeModule.forFeature([User]) SequelizeModule.forFeature([User]),
JwtModule
], ],
exports: [UsersService] 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