我正在构建一个完整的网页应用程序,这基本上是电子商务商店。这样做的目的是,如果用户已经有了一些数据库,就可以获得购物车或订单。Projec是使用React、NestJS、TypeOrm和MySQL构建的。
问题是,我无法使Jwt授权工作。我只能登录。如果我将@UseGuard添加到端点中,则始终会得到错误"401 (未经授权)“。我一直在检查文档和在线教程,我仍然找不出哪里出了问题。
链接到回购 https://github.com/Jacob120/e-commerce-fullstack-app
auth.controller.ts
import { Controller, Request, Post, UseGuards, Body, } from '@nestjs/common'; import { AuthService } from 'src/auth/service/auth.service'; import { HttpException, HttpStatus } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard'; import { Users } from 'src/auth/user.entity'; @Controller('api/auth/') export class AuthController { constructor(private usersService: AuthService) {} @Post('signup') async signup(@Body() user: Users): Promise<Users> { const checkUsername = await this.usersService.findOne(user.username); if (checkUsername) { throw new HttpException( 'Username already exists', HttpStatus.BAD_REQUEST, return this.usersService.signup(user); @UseGuards(AuthGuard('local')) @Post('login') async login(@Request() req) { return this.usersService.login(req.user); @UseGuards(JwtAuthGuard) @Get('profile') getProfile(@Request() req) { return req.user; }
auth.service.ts
import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Users } from '../user.entity'; import * as bcrypt from 'bcrypt'; import { JwtService } from '@nestjs/jwt'; @Injectable() export class AuthService { constructor( @InjectRepository(Users) private userRepository: Repository<Users>, private jwt: JwtService, async signup(user: Users): Promise<Users> { const salt = await bcrypt.genSalt(10); const hash = await bcrypt.hash(user.password, salt); user.password = hash; user.role = 'user'; return await this.userRepository.save(user); async validateUser(username: string, password: string): Promise<any> { const foundUser = await this.userRepository.findOneBy({ username }); if (foundUser) { if (await bcrypt.compare(password, foundUser.password)) { const { password, ...result } = foundUser; return result; return null; return null; async login(user: any) { const payload = { username: user.username, sub: user.id, role: user.role }; return { access_token: this.jwt.sign(payload), role: user.role, username: user.username, async findOne(username: string): Promise<Users | undefined> { return await this.userRepository.findOne({ where: { username }, }
auth.module.ts
import { Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; import { jwtConstants } from './guards/constants'; import { PassportModule } from '@nestjs/passport'; import { TypeOrmModule } from '@nestjs/typeorm'; import { AuthService } from './service/auth.service'; import { JwtStrategy } from './guards/jwt-strategy'; import { LocalStrategy } from './local.strategy'; import { AuthController } from './controller/auth/auth.controller'; import { Users } from './user.entity'; @Module({ imports: [ PassportModule, JwtModule.register({ secret: jwtConstants.secret, signOptions: { expiresIn: '1d' }, TypeOrmModule.forFeature([Users]), providers: [AuthService, JwtStrategy, LocalStrategy], controllers: [AuthController], exports: [AuthService, PassportModule], export class AuthModule {}
jw-auth.guard.ts
import { Injectable } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; @Injectable() export class JwtAuthGuard extends AuthGuard('jwt') {}
jwt-strategy.ts
import { ExtractJwt, Strategy } from 'passport-jwt'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable } from '@nestjs/common'; import { jwtConstants } from './constants'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { constructor() { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey: jwtConstants.secret, async validate(payload: any) { return { userId: payload.sub, username: payload.username, role: payload.role, }
编辑
我更改了jwt-auth.guard一点,而 console.log(info) 在 handleRequest(err, user, info) 中返回了"Error: No auth token“
console.log(info)
handleRequest(err, user, info)
import { ExecutionContext, Injectable, UnauthorizedException, } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; @Injectable() export class JwtAuthGuard extends AuthGuard('jwt') { canActivate(context: ExecutionContext) { // Add your custom authentication logic here // for example, call super.logIn(request) to establish a session. return super.canActivate(context); handleRequest(err, user, info) { console.log(info);