fastify / fastify-passport

Use passport strategies for authentication within a fastify application
MIT License
256 stars 46 forks source link

Authenticate callback function does not work #273

Closed AaronPorts closed 3 years ago

AaronPorts commented 3 years ago

Prerequisites

Fastify version

3.19.2

Plugin version

0.4.3

Node.js version

16.2.0

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

Callback in fastifyPassport.authenticate function does not trigger if authorization is incorrect, and has only request, reply parameters in case of success authorization.

Steps to Reproduce

import fastify from "fastify";
import fastifySecureSession from "fastify-secure-session";
import fastifyPassport from "fastify-passport";
import LocalStrategy from "passport-local";

fastify.register(fastifySecureSession, {
  cookieName: "ses",
  key: "464d01eddfc334cfebb5d69612554e1b270a7b1493213d84af986db586692f07",
  cookie: { path: "/" },
});
fastify.register(fastifyPassport.initialize());
fastify.register(fastifyPassport.secureSession());

fastifyPassport.use( "local", new LocalStrategy(
    { usernameField: "email", passwordField: "password" },
    (username, password, done) => { done(null, false) },
  ),
);

fastify.post(
  "/login",
  { preValidation: fastifyPassport.authenticate("local", { authInfo: false, session: false })},
  async (request, reply, err, user, info, status) => { reply.send({ hello: "world" }) },
);

Expected Behavior

The callback function is triggered on any request

airhorns commented 3 years ago

I think you need to pass that callback function to the the authenticate handler, not to the route registration. There's two different functions: one passed to authenticate, and one passed to the fastify route that deals with the actual request / response once the authentication handler has run. See the readme! Try that out and feel free to reopen if you still can't get it to work. Also, a better place for issues like this is probably fastify/help!