Reputation: 69
I've already been searching for almost one hour but I can not find the problem of my code, neither was I able to find a solution on inter and I even asked for help in other coding communities on Discord but no-one knows why I have such an error.
I've been trying to follow this tutorial https://mherman.org/blog/node-passport-and-postgres/#objectives to get to know the passport package - https://www.npmjs.com/package/passport
I have the following files
userController.ts
import { Request, Response } from 'express';
import * as fs from 'fs';
import * as Path from 'path';
import passport = require('../../auth/local');
export const userLogIn = async (req: Request, res: Response) => {
passport.default.authenticate('local', (err, user, info) => {
if (err) { res.status(500).send('error'); }
if (!user) { res.status(404).send('User not found!'); }
if (user)
{
req.logIn(user, function (err) {
if (err) { res.status(500).send('error'); }
res.status(200).send('success!');
});
}
});
return;
}
local.ts
import * as passport from 'passport';
import passport_local = require('passport-local');
const LocalStrategy = passport_local.Strategy;
import init = require('../passport');
import { knex } from '../knex/knex';
import * as bcrypt from 'bcrypt';
const options = {
usernameField: 'email',
passwordField: 'password'
};
init.default();
export default passport.use(new LocalStrategy(options, (username, password, done) => {
knex('accounts').where({userEmail: username}).first()
.then((user) => {
if (!user) return done(null, false)
if(!bcrypt.compareSync(password, user.userPassword)) {
return done(null, false);
}
else {
return done(null, user);
}
})
.catch((err) => { return done(err); });
}));
And the file where I have the problem
passport.ts
import * as passport from 'passport';
import { knex } from './knex/knex';
export default function(){
passport.serializeUser<any, any>(function(user, done) {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
knex('accounts').where({userId: id}).first()
.then((user) => { done(null, user) })
.catch((err) => { done(err, null) });
});
}
My error:
TypeError: passport.serializeUser is not a function
I have @types installed and imported for passport, if I hover my mouse over the import of passport it shows it is imported from the @types folder.
I have no idea what can cause this.
And yes, one function is with 'function' and the other is with arrow because I tried changing it searching for the solution but nothing seem to work.
Upvotes: 0
Views: 459
Reputation: 976
i guess you're importing it the wrong way.
import passport from "passport";
- should work according to the code sources
Upvotes: 1