N
N
Nikita2020-08-06 16:29:57
Node.js
Nikita, 2020-08-06 16:29:57

How to set cookies for Next.js application from a separate server?

I have a server application separate from Next.js on NeSt, which implements all the business logic. When trying to log in to the Next.js application, there is a problem that cookies from NeSt are not set in the browser. When working with postman cookies are set.
How cookies are set: (/auth/sign-in)

async login(@Req() req: IReq, @Res() res: Response<string>): Promise<void> {
    const tokens = await this.authService.createTokens(req.user);
    
    res.cookie("refresh_token", tokens.refresh_token, {
      httpOnly: true,
      maxAge: 1000 * 60 * 60 * 24 * 30,
      signed: true
    });

    res.json(tokens.access_token).status(201);
  }

Server request:
export async function login(data: any) {
  const headers = new Headers();
  headers.set("Content-Type", "application/x-www-form-urlencoded");

  const response = await fetch(`${process.env.API_URL}/auth/sign-in`, {
    method: "POST",
    headers,
    body: new URLSearchParams({ ...data, username: data.email })
  });

  if (response.ok) {
    return null;
    return {
      access_token: await response.json()
    };
  } else {
    return null;
  }
}

When checking cookies in the browser, there is nothing there.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question