Код: Выделить всё
import fp from "fastify-plugin";
import fastifyPassport from "@fastify/passport";
import LocalStrategy from "passport-local";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { User } from "./types";
export default fp(async function auth(fastify: FastifyInstance) {
const { store, httpErrors } = fastify;
fastifyPassport.registerUserSerializer(async (user: { id: number }, request) => {
if (!user?.id) return httpErrors.notFound("User ID is missing");
return user.id;
});
fastifyPassport.registerUserDeserializer(async (id: number, request) => {
try {
const user: User[] = await store("users").where({ id }).limit(1);
if (!user || user?.length < 1) return null;
....
return sessionUser
} catch (error) {
console.error("Deserialization error", error);
return null;
}
});
async function handleUserRecovery(req: FastifyRequest, email: string, password: string) {
try {
const user: User[] = await store("users").where({ email }).limit(1);
if (!user || user?.length < 1) return false;
...
return sessionUser;
} catch (err) {
console.error("Error fetching user:", err);
return false;
}
}
fastifyPassport.use(
"local",
new LocalStrategy(
{
passReqToCallback: true,
usernameField: "email",
},
handleUserRecovery
)
);
await fastify.register(fastifyPassport.initialize());
await fastify.register(fastifyPassport.secureSession());
}, {
dependencies: ["fastify-env", "fastify-session", "fastify-store", "fastify-sensible"],
});
Код: Выделить всё
fastify.route({
method: "GET",
url: "/protected",
preValidation: fastifyPassport.authenticate("local", { authInfo: false }),
handler: async (req: FastifyRequest, res: FastifyReply) => {
return res.send({ msg: "ok" });
},
});
Код: Выделить всё
Attempted to send payload of invalid type 'object'. Expected a string or Buffer.
Вот подробное описание вывод журнала:
Код: Выделить всё
responseTime: 73.25737500190735
[01:00:23.679] ERROR (33597): server closing with error
err: {
"type": "FastifyError",
"message": "Attempted to send payload of invalid type 'object'. Expected a string or Buffer.",
"stack": FastifyError: Attempted to send payload of invalid type 'object'. Expected a string or Buffer.
}
Попытка отправить полезную нагрузку недопустимого типа «объект». Ожидается строка или буфер.
В журналах. Я также заметил, что запрос обрабатывается дважды, что неожиданно.
Подробнее здесь: https://stackoverflow.com/questions/793 ... en-accessi
Мобильная версия