Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: done is not a function #2

Open
elisavetsky opened this issue Jul 11, 2022 · 4 comments
Open

TypeError: done is not a function #2

elisavetsky opened this issue Jul 11, 2022 · 4 comments

Comments

@elisavetsky
Copy link

Preface

I am very excited to integrate my first (and probably only) non-local strategy in passport.js though I am having a small issue that has been bugging me for a while. I checked the official SimpleLogin docs to find instructions to integrate SIWSL and it seemed pretty straightforward until I encountered the error TypeError: done is not a function. I was able to reach the SimpleLogin auth page and logged in but that is where the fun ended. Once reaching the "http:localhost:3000/authorization-code/callback?state=..." route I get that error. This has not been tested in a production environment yet as this error is holding me back.

Can't wait to get this up and running, thank you SimpleLogin team for all the hard work.

My troublesome code

const express = require("express");
const config = require('dotenv').config({ path: __dirname + "/.env" });
const bodyParser = require("body-parser");
var _ = require('lodash');

// MODULES
const mongoose = require(__dirname + "/modules/schemas.js").mongoose;
let schemas = require(__dirname + "/modules/schemas.js");

// Authentication
const passport = require('passport');
const passportLocalMongoose = require('passport-local-mongoose');
const OidcStrategy = require('passport-openidconnect').Strategy;

// Start App
const app = express();

const session = require('express-session');
const sess = {
	secret: process.env.CRYPT_KEY,
	resave: false,
	saveUninitialized: false,
	cookie: {}
}

if (app.get('env') === 'production') {
	app.set('trust proxy', 1) // trust first proxy
	sess.cookie.secure = true // serve secure cookies
}

// APP.LISTEN
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ... ` + port));

// APP.USE
app.use(bodyParser.urlencoded({ extended: true }));

// Creating the model
const Entry = mongoose.model("Entry", journalEntrySchema);
const Comment = mongoose.model("Comment", entryCommentSchema);
const User = mongoose.model("User", userSchema);

// Initialize Passport & Session
app.use(session(sess));
app.use(passport.initialize());
app.use(passport.session());

// Create User Strategy for Passport
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

// SimpleLogin Integration
passport.use('oidc', new OidcStrategy({
	// SimpleLogin OIDC Settings
	issuer: 'https://app.simplelogin.io',
	authorizationURL: 'https://app.simplelogin.io/oauth2/authorize',
	tokenURL: 'https://app.simplelogin.io/oauth2/token',
	userInfoURL: 'https://app.simplelogin.io/oauth2/userinfo',
	clientID: process.env.CLIENT_ID,
	clientSecret: process.env.CLIENT_SECRET,
	// you might need to change the callbackURL when deploying on production
	callbackURL: 'http://localhost:3000/authorization-code/callback',
	// openid needs to be in scope
	scope: 'openid profile',
}, (issuer, sub, profile, accessToken, refreshToken, done) => {
	return done(null, profile);
//       ^^^^^^^^^--------------------- ERROR on this line :/
}));
// redirect user to authorization page
app.use('/continue-with-simplelogin', passport.authenticate('oidc'));
// user is redirected back with the *code*
app.use('/authorization-code/callback',
	passport.authenticate('oidc', {
		failureRedirect: '/error'
	}), (req, res) => {
		var user = req.user._json
		res.send(`
    		Welcome ${user.name}! <br>
    		Your email: ${user.email} <br>
    		Avatar: <img src="${user.avatar_url}">
    	`)
	}
);
@nguyenkims
Copy link
Collaborator

@gabesean thanks for your kind words! There seems to be a breaking change in passport-openidconnect, the lib we use in the example. Can you try with its previous version by running npm i passport-openidconnect@0.0.2 --save?

@elisavetsky
Copy link
Author

You're welcome! I just tried with passport-openidconnect@0.0.2 and now I am getting a different error:

TypeError: user.get is not a function
    at /Users/gabesean/micro_journal/node_modules/passport-local-mongoose/index.js:212:21
    at pass (/Users/gabesean/micro_journal/node_modules/passport/lib/authenticator.js:291:9)
    at Authenticator.serializeUser (/Users/gabesean/micro_journal/node_modules/passport/lib/authenticator.js:296:5)
    at /Users/gabesean/micro_journal/node_modules/passport/lib/sessionmanager.js:33:10
    at Immediate._onImmediate (/Users/gabesean/micro_journal/node_modules/express-session/session/store.js:54:5)
    at processImmediate (node:internal/timers:466:21)

Would any of these dependency versions be the problem?

    "dotenv": "^16.0.1",
    "ejs": "^3.1.8",
    "express": "^4.18.1",
    "express-session": "^1.17.3",
    "lodash": "^4.17.21",
    "mongoose": "^6.3.4",
    "mongoose-encryption": "^2.1.2",
    "passport": "^0.6.0",
    "passport-anonymous": "^1.0.1",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^7.1.2",
    "passport-openidconnect": "^0.0.2"

@nguyenkims
Copy link
Collaborator

@gabesean this error seems to come from mongoose which unfortunately I'm not familiar with.

@elisavetsky
Copy link
Author

@nguyenkims That's fine! I'll open an issue with that plugin then. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants