我有登录路由,返回用户:
@Post('login')
async login(@Body() user: LoginRequest, @Res() reply): Promise<User> {
const foundUser = await this.authService.validateUser(user.email, user.password);
reply.setCookie('t', foundUser._id, { path: '/' });
// reply.send(foundUser);
return foundUser;
}
我的问题是它没有返回任何内容(停留在待处理...)),除非我做 reply.send(foundUser);
我正在使用允许 cors 来源的代理:
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
// enable cors for static angular site.
const corsOptions = {
origin: 'http://localhost:4200',
optionsSuccessStatus: 200,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
};
app.register(require('fastify-cors'), corsOptions);
// enable cookie for auth.
app.register(require('fastify-cookie'));
// validate types and extra
app.useGlobalPipes(new ValidationPipe({ forbidUnknownValues: true }));
await app.listen(3000);
}
Link到源代码。
最佳答案
您必须从您的 Controller 函数中删除@Res() reply
。一旦注入(inject) response
对象,许多嵌套功能就会停止工作,例如拦截器。
@Post('login')
async login(@Body() user: LoginRequest): Promise<User> {
return this.authService.validateUser(user.email, user.password);
}
你可以使用 interceptor改为动态设置 cookie。
关于javascript - Controller 不会在没有重播的情况下返回结果。使用 fastify 平台发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55945069/