profile
viewpoint
elad gasner gasner closeapp israel Love PHP,JS,VUE & LARAVEL

gasner/cardcom 1

cardcom api php package

gasner/vue-lock-screen 1

lock screen with template for vuejs

amihanya/myLaw 0

aGame

gasner/awesome-vuejs-israel 0

A curated list of Israeli companies & organisations who chose Vue JS

gasner/express 0

Fast, unopinionated, minimalist web framework for node.

issue commentfastify/fastify-passport

Local Strategy bad request

const fastify = require("fastify");
const fastifyPassport = require("fastify-passport");
const fastifySecureSession = require("fastify-secure-session");
const LocalStrategy = require("passport-local");
const mongoose = require("mongoose");
const path = require("path");
const fs = require("fs");

mongoose.connect("mongodb://127.0.0.1:27017/", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    dbName: "test",
});
const Schema = mongoose.Schema;
const userSchema = new Schema(
    {
        username: {
            type: String,
            required: true,
            unique: true,
        },
        password: { type: String, required: true },
    },
    { timestamps: true }
);
const User = mongoose.model("user", userSchema);

const server = fastify();
server.register(fastifySecureSession, {
    key: fs.readFileSync(path.join(__dirname, "secret-key")),
});

server.register(fastifyPassport.initialize());
server.register(fastifyPassport.secureSession());

fastifyPassport.use(
    "local",
    new LocalStrategy(function (username, password, done) {
        User.findOne({ username: username }, function (err, user) {
            if (err) {
                return done(err);
            }
            if (!user) {
                return done(null, false, { message: "Incorrect username." });
            }
            return done(null, user);
        });
    })
);

fastifyPassport.registerUserSerializer(async (user, request) => {
    return user.id;
});

fastifyPassport.registerUserDeserializer(async (id, request) => {
    let user = await User.findById(id);
    return user;
});

server.post("/register", async (req, reply) => {
    await User.create({
        username: req.body.username,
        password: req.body.password,
    });
    return { a: "hey" };
});

server.post(
    "/login",
    {
        preValidation: fastifyPassport.authenticate("local", {
            successRedirect: "/auth",
            authInfo: false,
        }),
    },
    () => {}
);

server.get(
    "/auth",
    {
        preValidation: fastifyPassport.authenticate("local", {
            authInfo: true,
        }),
    },
    async (req, res) => {
        console.log(req, res);
        return "Hey";
    }
);

server.listen(3000);

Thanks!

gasner

comment created time in 2 months

issue openedfastify/fastify-passport

Local Strategy bad request

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

3

Plugin version

0.4.3

Node.js version

14

Operating system

Linux

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

20

Description

Hi, I trying to use LocalStrategy. I succeeded to do login but the other request return a Bad Request.

Steps to Reproduce

module.exports = fp(async function (fastify, opts) {
    fastify.register(fastifySecureSession, {
        key: fs.readFileSync(path.join(__dirname, '../secret-key')),
    })
    fastify.register(fastifyPassport.initialize())
    fastify.register(fastifyPassport.secureSession())

    fastifyPassport.use("local", new LocalStrategy(
        function (username, password, done) {
            User.findOne({email: username}, function (err, user) {
                if (err) {
                    return done(err);
                }
                if (!user) {
                    return done(null, false);
                }
                if (!user.comparePassword(password)) {
                    return done(null, false);
                }
                return done(null, user);
            });
        }
    ));
    // register a serializer that stores the user object's id in the session ...
    fastifyPassport.registerUserSerializer(async (user, request) => user.id);

    // ... and then a deserializer that will fetch that user from the database when a request with an id in the session arrives
    fastifyPassport.registerUserDeserializer(async (id, request,) =>{
        return await User.findById(id);
    });

})

and this is my route

  // always return Bad Request
    fastify.get('/auth', {preValidation:  fastifyPassport.authenticate('local',{ successFlash: 'Welcome!' })},
        async (req) => {
            return {
                "title": 'hello world!',
                user: req.user

            }
        }
    )
    
    //work good!
    fastify.post(
        '/login',
        {
            preValidation: fastifyPassport.authenticate('local', {
                successRedirect: "/",
                failureRedirect: "/login",
                failureFlash: true,
            })
        },
        () => {
            return {hello: "world"}
        }
    )

Expected Behavior

Expected to get the user and not Bad Request

created time in 2 months

startedadonisjs/core

started time in 2 months

more