Я создаю конечную точку для входа пользователя. он смотрит, есть ли у пользователя refre sh cook ie и, если нет, генерирует пару токенов auth и refre sh.
async login(_, { email }, { req, res }) {
try {
const user = await User.findOne({ where: { email } });
if (!user) {
throw new Error("*** COULDN'T FIND USER WITH EMAIL ", email);
}
const tokenAuth =
jsonwebtoken.sign({ id: user.id, email: user.email }, SECRET_AUTH, { expiresIn: TOKEN_AUTH_EXPIRY });
const tokenRefresh =
jsonwebtoken.sign({ id: user.id, email: user.email }, SECRET_REFRESH, { expiresIn: TOKEN_REFRESH_EXPIRY });
const saveRefreshToken = await Token.create({
hash: tokenRefresh,
user_id: user.id,
})
const tokens = {
tokenAuth,
tokenRefresh
}
res.cookie('tokenAuth', tokenAuth, { httpOnly: true, maxAge: 300000 })
res.cookie('tokenRefresh', tokenRefresh, { httpOnly: true, maxAge: 2592000000 })
res.status(200).send({ data: { message: "Tokens Generated!", tokens, login: true }});
} catch(e) {
console.log('*** Error on /login:', e)
}
}
Но я получаю следующие ошибки
(node:14905) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at apollo_server_core_1.runHttpQuery.then (/Users/z/server/node_modules/apollo-server-express/src/expressApollo.ts:42:17)
(node:14905) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14905) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Я отправляю только одно разрешение, так почему же выдает эту ошибку?