When i use postman to send request to my api, sessionId doesnt change, and all work good. But, when i use axios to request on api, sessionId change with every single request. In axios config i set withCrendentails: true, and it doesnt help.
nest config (main.ts)
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: true,
credentials: true,
})
app.use(session({
secret: SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {secure: false, maxAge: 360000, sameSite: 'strict'},
})
)
app.setGlobalPrefix('api')
await app.listen(8000);
}
nest controller
@Post()
async addToCart(@Body() body: AddToCartDto, @Session() session: Record<string, any>, @Res() res: Response, @Req() req: Request) {
const {productId, quantity} = body
const cart = session.cart || {items: [], totalPrice: 0}
console.log(req)
const existingItem = cart.items.find((item) => item.id === productId);
if (existingItem) {
existingItem.quantity += quantity;
} else {
const product = await this.productsService.findOne(productId)
const newItem = { id: productId, title: product.title, quantity, price: product.priceUAN };
cart.items.push(newItem);
}
cart.totalPrice = cart.items.reduce((total, item) => total + item.price * item.quantity, 0);
session.cart = cart
res.json({cart})
}
client side code
export const addToCart = (productId: number, quantity: number): ThunkType => async dispatch => {
try {
const data = {productId, quantity}
const config = {
headers: {
"Content-type": "application/json1",
},
withCredentials: true,
}
const response = await axios.post(`${process.env.REACT_APP_API_URL}/cart`, data, config)
dispatch({type: SET_CART_SUCCESS, cart: response.data.cart})
} catch (e) {
console.log(e)
}
}