não consegui esta salvar o refresh_token iria comparar o codigo para entender...
import nookies from "nookies";
import { httpClient } from "./../../src/services/infra/httpClient/httpClient";
const REFRESH_TOKEN_NAME = "REFRESH_TOKEN_NAME";
const controllers = {
async storeRefreshToken(req, resp) {
const ctx = { req, resp };
const refresh_token = req.body.refreshToken;
nookies.set(ctx, REFRESH_TOKEN_NAME, refresh_token, {
httpOnly: true,
sameSite: "lax",
path: "/",
});
resp.json({ data: { message: "Stored with success!" } });
},
async displayCookies(req, resp) {
const ctx = { req, resp };
resp.json({ data: { cookies: nookies.get(ctx) } });
},
async regenerateTokens(req, resp) {
const ctx = { req, resp };
const cookies = nookies.get(ctx);
const refresh_token = cookies[REFRESH_TOKEN_NAME] || req.body.refresh_token;
const refreshResponse = await httpClient(
`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/refresh`,
{
method: "POST",
body: { refresh_token },
}
);
if (refreshResponse.ok) {
nookies.set(
ctx,
REFRESH_TOKEN_NAME,
refreshResponse.body.data.refresh_token,
{
httpOnly: true,
sameSite: "lax", //somente o dominio que vc esta tera acesso
}
);
resp.status(200).json({ data: refreshResponse.body.data });
} else {
resp.status(401).json({ status: 401, message: "Não autorizado" });
}
},
};
const controllerBy = {
POST: controllers.storeRefreshToken,
PUT: controllers.regenerateTokens,
GET: controllers.regenerateTokens,
};
export default function handler(req, res) {
if (controllerBy[req.method]) return controllerBy[req.method](req, res);
response.status(404).json({
status: 404,
message: "Not Found",
});
}