cookie rejected on Next.js with status 401 unauthorized but accepted using postman with status 200
Unanswered
Minskin posted this in #help-forum
MinskinOP
So I am using Next.js in the frontend and Nest.js in the backend. Right this morning I have solved the cookie problem routing from browser client -> next.js server -> nest.js server and successfully gain access to resources. But after I open up my computer again, the cookie is rejected and my client cannot gain access to resources from nest.js server. Does anyone have simmilar issue?
(next.js server action)
nest.js server
(next.js server action)
(login from form)
async function LoginHandler(credentials: FormData) {
'use server';
try {
const response = await axios.post(process.env.BASE_URL + '/auth/login', {
email: credentials.get('email'),
password: credentials.get('password'),
});
redirect('/users');
} catch (error: any) {
if (error.message === 'NEXT_REDIRECT') throw error;
throw error;
}
}
(server action accessing resource)
export async function getUserList(offset: string, limit: string) {
try {
const token = cookies().getAll();
console.log(token);
const response = await axios.get(
process.env.BASE_URL + '/users?offset=' + offset + '&limit=' + limit,
{
headers: {
Cookie: `access_token=${token[0].value}`,
},
withCredentials: true,
},
);
return response;
} catch (error) {
console.log(error);
}
}nest.js server
//controller
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(Role.Admin)
@Get()
findAll(@Query('offset') offset: string, @Query('limit') limit: string) {
return this.usersService.getManyUser(+offset, +limit);
}
//service
async getManyUser(offset: number, limit: number) {
const users = await this.prisma.user.findMany({
skip: offset,
take: limit,
orderBy: {
createdAt: 'asc',
},
});
return users;
}3 Replies
MinskinOP
Postman cookie success
Browser Cookie success
Response in next.js