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

How to authenticate #30

Closed
franckstifler opened this issue Nov 9, 2017 · 18 comments
Closed

How to authenticate #30

franckstifler opened this issue Nov 9, 2017 · 18 comments
Assignees

Comments

@franckstifler
Copy link

I am curious on how to authenticate using server. I don't see how to configure my session store to be mongoDB using connect-mongo. A tutorial on how to this will be really helpful.

@franciscop
Copy link
Owner

franciscop commented Nov 10, 2017

You don't have access to the session that I know of, which is a known bug to be fixed. Meanwhile, you can do something like this manually:

const mongoose = require('mongoose');
 
// Basic usage
mongoose.connect(connectionOptions);

server({ session: false }, server.utils.modern(session({
    store: new MongoStore({ 
      mongooseConnection: mongoose.connection 
    })
})));

You can use any express middleware by wrapping it with server.utils.modern().

@franciscop
Copy link
Owner

Or even easier, you could use the native server store option:

const mongoose = require('mongoose');
mongoose.connect(connectionOptions);

// See options docs
const options = {
  session: {
    store: new MongoStore({ 
      mongooseConnection: mongoose.connection 
    })
  }
};
server(options, ...);

@franciscop
Copy link
Owner

@franckstifler please let me know if that answers your question

@franckstifler
Copy link
Author

franckstifler commented Nov 11, 2017

@franciscop Let me get to my computer giving you a feedback in about 30min. I can make use of flash messages too right?

@franckstifler
Copy link
Author

The problem persists. Using connect-mongo documentation this is how I have to require the modules: const session = require('express-session'); const MongoStore = require('connect-mongo')(session);
I suppose the session is already set into server right? How are my then to pass the session to the next line? const MongoStore = require('connect-mongo')(???)

@franciscop franciscop added bug and removed fixed? labels Nov 12, 2017
@franciscop
Copy link
Owner

You are totally right, for this version you don't have access to session, which is definitely a bug. In the meantime, could you share some of your connection/startup code please? In the connect-mongo docs there are few examples that don't need the session object.

@franciscop franciscop self-assigned this Nov 13, 2017
@franckstifler
Copy link
Author

https://github.com/franckstifler/CheckAM/blob/master/index.js sorry for the late reply had to adjust some things before making a push. That is a project I am building powered by server.js.

@franciscop
Copy link
Owner

franciscop commented Nov 26, 2017

Did you try the snippet above?

const mongoose = require('mongoose');
mongoose.connect(connectionOptions);

// See options docs
const options = {
  session: {
    store: new MongoStore({ 
      mongooseConnection: mongoose.connection 
    })
  }
};
server(options, ...);

Seems like you are using mongoose already from your project so you should be able to do that. You even have that in line 23 (but it is commented):

store: new MongoStore({ mongooseConnection: mongoose.connection })

Does that work? The line 4 in that situation should be like this:

const MongoStore = require('connect-mongo');  // NO (server)

@franckstifler
Copy link
Author

In deed I tried. The problem is actually in line 4 because you must init connect-mongo with a session.

@franciscop
Copy link
Owner

franciscop commented Nov 29, 2017

You can skip it in line 4 and just load it without (session), did you try that as well?

const MongoStore = require('connect-mongo');  // NO (server)

Edit: that would be the same as this example, where they are inconveniently missing the line I wrote above (or so I think): https://github.com/jdesboeufs/connect-mongo#re-use-a-mongoose-connection

@mariusk
Copy link

mariusk commented Dec 28, 2017

I believe I am struggling with the exact same issues wrt connect-pg-simple, and until the interfaces are cleared up I would suggest downplaying the Express session / middleware compatibility features a notch. Alternatively, improve the docs on how it should be done (no, I haven't figured it out unfortunately). It seems the pattern with initializing a store with a session object is pretty common..

@franckstifler
Copy link
Author

In deed I loaded without session but it wont work, complaining that it needs a store.

@franciscop
Copy link
Owner

What about something like this? I think it should work properly now:

const server = require('server');

// "server.session" contains the result of require('express-session');
// so you can inject it wherever it's needed
// Not to be confused with a current session
const MongoStore = require('connect-mongo')(server.session);
const store = new MongoStore(options);

server({ session: { store } }, ...);

It should be available in the latest [email protected] that was just released, please let me know what you think about this approach works for you or any other feedback!

@franckstifler
Copy link
Author

Sorry for the delay. Testing right a way and giving you a feedback.

@franckstifler
Copy link
Author

Good it works like charm now.

@franciscop
Copy link
Owner

Glad to hear! And thanks for the feedback 😇

@franckstifler
Copy link
Author

Sure I can continue to work on my project. Think of creating a tutorial for that in the docs. I think it is very important.

@franciscop
Copy link
Owner

I did add it to the documentation for session stores, but I should add it to the Sessions in Production tutorial as well.

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

No branches or pull requests

3 participants