How to use passport with koa-generic-session ()?

I created a Koa application that uses a passport with a local authentication strategy. I would like to use the koa-generic-session module to save session data in Redis.

How to use these two together?

I found this repo that does this, but it really doesn't look like sessions, and I'm not sure if this is correct: https://github.com/dozoisch/koa-react-full-example p>

+5
source share
1 answer

(Disclaimer: I am not familiar with Coa, but I am with Express and Passport.)

I looked at the link you provided, and here is how they use the kao general passport session.

In the server.js file , the following lines relate to the configuration of the passport.

08 - const passport = require("koa-passport"); 13 - const config = require("./config/config"); 38 - require("./config/passport")(passport, config); 40 - require("./config/koa")(app, config, passport); 

Line 38 is a traditional passport configuration file that simply sets serialization and deserialization.
Line 40 includes koa.js and passes the application, configuration file, and passport files to it.

The following code from koa.js:

 04 - const session = require("koa-generic-session"); 18 - app.keys = config.app.keys; 

Looking at koa.js, koa-generic-session is assigned to a session variable. This variable is then called here:

 35 - app.use(session({ 36 - key: "koareactfullexample.sid", 37 - store: new MongoStore({ url: config.mongo.url }), 38 - })); 

On line 18, app.keys initialized as documentation for the koa-generic-session . While your project uses MongoStore with koa-generic-session, you can simply replace this constructor with the koa-redis constructor, as shown in the koa- generic-session .

Finally, the passport is initialized:

 41 - app.use(passport.initialize()); 42 - app.use(passport.session()); 

This code is similar to using Passport with Express, since all Passport must manage session authentication.

+3
source

Source: https://habr.com/ru/post/1247986/


All Articles