server: fix set cookie

This commit is contained in:
fantasticit 2022-05-23 11:37:22 +08:00
parent c691cbd52b
commit a3cce12243
3 changed files with 22 additions and 21 deletions

View File

@ -17,7 +17,6 @@ import {
UseInterceptors,
} from '@nestjs/common';
import { UserService } from '@services/user.service';
import { wrapResponse } from '@transforms/http-response.transform';
import { Response as ExpressResponse } from 'express';
@Controller('user')
@ -36,8 +35,8 @@ export class UserController {
@HttpCode(HttpStatus.OK)
async login(@Body() user: LoginUserDto, @Res({ passthrough: true }) response: ExpressResponse) {
const { user: data, token } = await this.userService.login(user);
response.cookie('token', token, { httpOnly: true, sameSite: 'none', secure: true });
return response.send(wrapResponse({ data: { ...data, token }, statusCode: HttpStatus.OK }));
response.cookie('token', token, { httpOnly: true, secure: true, sameSite: 'lax' });
return { ...data, token };
}
@Get('logout')

View File

@ -30,7 +30,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
secretOrKey: jwtConfig.secretkey,
jwtFromRequest: ExtractJwt.fromExtractors([
(request: RequestType) => {
const token = request?.cookies['token'];
const token = request?.cookies?.token;
return token;
},
]),

View File

@ -236,7 +236,7 @@ importers:
eslint: 8.14.0
eslint-config-prettier: 8.5.0_eslint@8.14.0
eslint-plugin-import: 2.26.0_eslint@8.14.0
eslint-plugin-prettier: 4.0.0_74ebb802163a9b4fa8f89d76ed02f62a
eslint-plugin-prettier: 4.0.0_740be41c8168d0cc214a306089357ad0
eslint-plugin-react: 7.29.4_eslint@8.14.0
eslint-plugin-react-hooks: 4.5.0_eslint@8.14.0
eslint-plugin-simple-import-sort: 7.0.0_eslint@8.14.0
@ -280,6 +280,7 @@ importers:
'@think/config': workspace:^1.0.0
'@think/constants': workspace:^1.0.0
'@think/domains': workspace:^1.0.0
'@types/cookie-parser': ^1.4.3
'@types/cron': ^2.0.0
'@types/express': ^4.17.13
'@types/jest': 27.0.2
@ -292,6 +293,7 @@ importers:
class-transformer: ^0.5.1
class-validator: ^0.13.2
compression: ^1.7.4
cookie-parser: ^1.4.6
date-fns: ^2.28.0
eslint: ^8.14.0
eslint-config-prettier: ^8.5.0
@ -348,6 +350,7 @@ importers:
class-transformer: 0.5.1
class-validator: 0.13.2
compression: 1.7.4
cookie-parser: 1.4.6
date-fns: 2.28.0
express: 4.17.2
express-rate-limit: 6.2.0_express@4.17.2
@ -375,6 +378,7 @@ importers:
'@nestjs/cli': 8.2.0_eslint@8.14.0
'@nestjs/schematics': 8.0.5_typescript@4.5.5
'@nestjs/testing': 8.2.6_b893ca8083ee374883b6d648098a9aeb
'@types/cookie-parser': 1.4.3
'@types/cron': 2.0.0
'@types/express': 4.17.13
'@types/jest': 27.0.2
@ -3101,6 +3105,12 @@ packages:
'@types/node': 16.11.21
dev: true
/@types/cookie-parser/1.4.3:
resolution: {integrity: sha512-CqSKwFwefj4PzZ5n/iwad/bow2hTCh0FlNAeWLtQM3JA/NX/iYagIpWG2cf1bQKQ2c9gU2log5VUCrn7LDOs0w==}
dependencies:
'@types/express': 4.17.13
dev: true
/@types/cookiejar/2.1.2:
resolution: {integrity: sha512-t73xJJrvdTjXrn4jLS9VSGRbz0nUY3cl2DMGDU48lKl+HR9dbbjW2A9r3g40VA++mQpy6uuHg33gy7du2BKpog==}
dev: true
@ -4684,6 +4694,14 @@ packages:
dependencies:
safe-buffer: 5.1.2
/cookie-parser/1.4.6:
resolution: {integrity: sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==}
engines: {node: '>= 0.8.0'}
dependencies:
cookie: 0.4.1
cookie-signature: 1.0.6
dev: false
/cookie-signature/1.0.6:
resolution: {integrity: sha1-4wOogrNCzD7oylE6eZmXNNqzriw=}
dev: false
@ -5397,22 +5415,6 @@ packages:
prettier-linter-helpers: 1.0.0
dev: true
/eslint-plugin-prettier/4.0.0_74ebb802163a9b4fa8f89d76ed02f62a:
resolution: {integrity: sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==}
engines: {node: '>=6.0.0'}
peerDependencies:
eslint: '>=7.28.0'
eslint-config-prettier: '*'
prettier: '>=2.0.0'
peerDependenciesMeta:
eslint-config-prettier:
optional: true
dependencies:
eslint: 8.14.0
eslint-config-prettier: 8.5.0_eslint@8.14.0
prettier-linter-helpers: 1.0.0
dev: true
/eslint-plugin-react-hooks/4.5.0_eslint@8.14.0:
resolution: {integrity: sha512-8k1gRt7D7h03kd+SAAlzXkQwWK22BnK6GKZG+FJA6BAGy22CFvl8kCIXKpVux0cCxMWDQUPqSok0LKaZ0aOcCw==}
engines: {node: '>=10'}