Supports Fastify versions 4.x
npm i fastify-oauth2-server
Require fastify-oauth2-server
and register.
const fastify = require("fastify")();
fastify.register(require("fastify-oauth2-server"), {
model: {}, // See https://github.com/node-oauth/node-oauth2-server for specification,
// ...oauth2-server options
});
// Token route
fastify.post("/", fastify.oauthServer.token());
// Authorize route
fastify.post(
"/",
fastify.oauthServer.authorize({
authenticateHandler: {
handle: async (request) => {
// authorization logic here
return user;
},
},
})
);
// Protected route
fastify.get(
"/",
{ preValidation: fastify.oauthServer.authenticate() },
(request, reply) => reply.send("Protected route")
);
fastify.listen({ port: 3000 });
1.Add tests
Licensed under MIT.