'Undefined cookies
when I do request with postman code works, but from the front it doesn't. Problem with undefined oldRefreshToken when I do request from front. But I don't know why it parse cookies from postman and does not from front :(
Server login part:
@Public()
@Post('login')
@HttpCode(HttpStatus.OK)
async signinLocal(
@Body() dto: AuthDto,
@Res({ passthrough: true }) response: Response,
): Promise<any> {
const tokens = await this.authService.signinLocal(dto);
response.cookie('refreshToken', tokens.refreshToken, {
maxAge: 30 * 24 * 60 * 60 * 1000,
httpOnly: true,
});
return { accessToken: tokens.accessToken };
}
Server refreshToken part:
@Public()
@UseGuards(RefreshTokenGuard)
@Post('refresh')
@HttpCode(HttpStatus.OK)
async refreshTokens(
@GetCurrentUser('sub') userId: number,
@Req() request: Request,
@Res({ passthrough: true }) response: Response,
): Promise<string> {
const oldRefreshToken = request?.cookies['refreshToken'];
const tokens = await this.authService.refreshTokens(
userId,
oldRefreshToken,
);
response.cookie('refreshToken', tokens.refreshToken, {
maxAge: 30 * 24 * 60 * 60 * 1000,
httpOnly: true,
});
return tokens.accessToken;
}
Frontend part:
const response = await fetch("http://localhost:3001/auth/refresh/", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
});
Solution 1:[1]
Maybe you can't access the token in the frontend, because it's http only (wich means no javascript in the frontend can access the cookie)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DevOskar |
