mweibel / connect-session-sequelize

Sequelize SessionStore for Express/Connect

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async extendDefaultFields support?

zypA13510 opened this issue · comments

I'm trying to do what is described on the wiki - adding a userId field to my session model. However, I'm also trying to constrain the userId field with an FK. So for a new user, I need to create the entry in users first. I'm going to do this in the extendDefaultFields, but turns out it does not support async function.

I've taken a look at the code, connect-session-sequelize.js#L87, it should have no problem supporting async function, since we are going to call fn back anyway.

So what is your question? Can you post example code to be able to reproduce it?

The question is, connect-session-sequelize does not support extendDefaultFields method returning a Promise.

extendDefaultFields: async (defaults, data) => {
	var userId = data && data.passport && data.passport.user && data.passport.user.id;
	if (userId) {
		await User.findOrCreate({
			where: {
				userId: userId
			},
			defaults: {
				firstName: data.passport.user.first_name,
				lastName: data.passport.user.last_name,
				email: data.passport.user.email,
			},
		});
		return {
			data: defaults.data,
			expires: defaults.expires,
			userId: userId,
		};
	} else {
		return {
			data: defaults.data,
			expires: defaults.expires,
		};
	}
}

This code will fail and give an error.

Just tried this zypA13510/connect-session-sequelize, works fine for me. But I'm not sure if it will break existing codes or on older environment.

connect-session-sequelize doesn't support this, no. I don't plan on adding support for this.

I wonder why you'd want to create a user within the session handler? If you have a userId already why don't you have a user already? Is the userId from some external system?
I'm sure you can hook into passport somehow to provide that already earlier. The session handler is most likely the wrong place to put this.

@mweibel Yes, the userId comes from an external system. The middleware provided by the SSO provider would directly interact with express-session. So I have to find a place somewhere in express-session or in the session store (connect-session-sequelize) to create the user if it doesn't exist.
Of course, I also feel that this is probably not the "correct" place to add this code, but it's the best option I find. To support this or not is your decision though, personally, I feel that it does no harm anyway (because the existing extendDefaultFields method does not require synchronous behavior, the worst scenario is probably a delay in the resolution of the Promise returned).

Hi @mweibel,

Just want to get back to this thread to discuss another scenario I've encountered - you may want to perform an asynchronous operation during extendDefaultFields e.g. perform a lookup using sequelize, which would be common I believe, since everyone who uses this package would be using sequelize, and sequelize methods are mostly asynchronous.

So I think maybe you should reconsider this. (Not necessarily using async/await because this feature is only supported by Node.js v7.6 and above, but you can use callback routine)