209 похожих чатов

Добрейший вечерок всем в хату Как устроен этот сраный @nestjs/passport? @Injectable() export

class BearerAuthGuard extends AuthGuard('bearer') {
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const user = request.user;
return !!user;
}
}


@Injectable()
export class HttpBearerStrategy extends PassportStrategy(Strategy, 'bearer') {
constructor(private authApiService: AuthApiService) {
super();
}

async validate(token: string) {
const { user } = await this.authApiService.validateToken(token);
return user;
}
}


@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
constructor(private authApiService: AuthApiService) {
super({
usernameField: 'email',
passwordField: 'hashPassword',
session: false,
});
}

async validate(email: string, password: string): Promise<UserEntity> {
return this.authApiService.searchUser(email, password);
}
}


@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {
}



в модуле в providers указано использование BearerAuthGuard как глобальный


{
provide: APP_GUARD,
useClass: BearerAuthGuard,
},

есть контроллер AuthApiController в котором следующий метод для авторизации

@Post()
@HttpCode(HttpStatus.CREATED)
@ApiBody({ required: true, type: LocalAuthDao })
@UseGuards(new LocalAuthGuard())
async logIn(@Req() req: Request) {
return this.authApiService.createSession(req.user as any);
}

3 ответов

27 просмотров

"return !!user" какого черта?

Bogdan-Bednyi Автор вопроса
ilshaw
"return !!user" какого черта?

Потому что проверяю существует ли объект

Похожие вопросы

Обсуждают сегодня

Карта сайта