护照。没有蒙古的js本地策略

时间:2022-06-02 17:13:12

I'm trying to find resources about the most simple login with passport for a node app. I mean, using:

我正在为一个节点应用寻找关于passport最简单登录的资源。我的意思是,使用:

middleware:

中间件:

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));
    app.use(cookieParser());

    app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
    app.use(passport.initialize());
    app.use(passport.session());

    app.use(express.static('public'));

passport:

护照:

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    done({ id: id, nickname: "test"})
});


    passport.use(new LocalStrategy(
      function(username, password, done) {
        console.log("test");
          if (username === 'username') {
              return done(null, { name: "test", id: '1234'});
          } else {
              return done(null, false, { message: 'Incorrect cred.' });
          }
      })
    )

and redirection:

和重定向:

    app.post('/login',
      passport.authenticate('local', { 
          successRedirect: '/index.html',
          failureRedirect: '/login'
     })
    );

my app structure:

我的程序结构:

/app.js
/public/index.html
/public/login.html

And that's it. the Web is full of the same examples of Mongoose with plugins, everyone simply copy-pasting from each other.

就是这样。Web上充满了与Mongoose相同的插件示例,每个人都简单地相互复制粘贴。

The point is that I would like, later on, to insert my own code ti the LocalStrategy code (would probably used LDAP).

重点是,稍后我希望将我自己的代码插入LocalStrategy代码(可能使用LDAP)。

Currently, instead of redirecting to /index.html with the page created in the public folder, I just receive [object Object] on index.html.

目前,不是重定向到/index。使用在公共文件夹中创建的页面,我只在index.html上接收[object]。

2 个解决方案

#1


2  

Your passport.deserializeUser() is incorrect:

你passport.deserializeUser()是不正确的:

passport.deserializeUser(function(id, done) {
  done({ id: id, nickname: "test"})
});

It's passing the "user" object as first argument to done, which is reserved for errors. This causes Express to render an error page where it tries to stringify the error, resulting in [object Object].

它将“user”对象作为要完成的第一个参数传递给用户,这是为错误预留的。这导致Express呈现一个错误页面,在该页面中尝试对错误进行字符串化,从而导致[object]。

Try this:

试试这个:

passport.deserializeUser(function(id, done) {
  done(null, { id: id, nickname: "test"})
});

#2


0  

Found the issue! the redirection didn't work, so I looked back up and noticed that the express.static mw was set AFTER the passport initialize(), and not BEFORE. I just moved it up to the top and.. rainbows

发现问题!重定向不起作用,所以我抬头一看,发现了那辆快车。静态mw是在passport initialize()之后设置的,而不是在之前。我把它移到顶部。彩虹

#1


2  

Your passport.deserializeUser() is incorrect:

你passport.deserializeUser()是不正确的:

passport.deserializeUser(function(id, done) {
  done({ id: id, nickname: "test"})
});

It's passing the "user" object as first argument to done, which is reserved for errors. This causes Express to render an error page where it tries to stringify the error, resulting in [object Object].

它将“user”对象作为要完成的第一个参数传递给用户,这是为错误预留的。这导致Express呈现一个错误页面,在该页面中尝试对错误进行字符串化,从而导致[object]。

Try this:

试试这个:

passport.deserializeUser(function(id, done) {
  done(null, { id: id, nickname: "test"})
});

#2


0  

Found the issue! the redirection didn't work, so I looked back up and noticed that the express.static mw was set AFTER the passport initialize(), and not BEFORE. I just moved it up to the top and.. rainbows

发现问题!重定向不起作用,所以我抬头一看,发现了那辆快车。静态mw是在passport initialize()之后设置的,而不是在之前。我把它移到顶部。彩虹