密码不正确或在护照中过期

时间:2019-02-11 12:27:50

标签: javascript express

我不断得到

  

传递的代码不正确或已过期。

     

GET / api / users / auth / github / callback?code = afefcf8b12561c910798--ms-   -[0]未定义[0] TokenError:传递的代码不正确或已过期。 [0]位于Strategy.OAuth2Strategy.parseErrorResponse   (/用户/eli/nodework/sequelize-demo/node_modules/passport-oauth2/lib/strategy.js:329:12)   [0]在Strategy.OAuth2Strategy._createOAuthError   (/Users/eli/nodework/sequelize-demo/node_modules/passport-oauth2/lib/strategy.js:376:16)

我认为我已经正确设置了护照-github 0auth。

我重设了所有令牌,但仍然出现错误。

config / passport-config.js

const GitHubStrategy = require('passport-github').Strategy;
const models = require( '../models/index');
require('dotenv').config();


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

  // from the user id, figure out who the user is...
  passport.deserializeUser(function(userId, done){
    models.User
      .find({ where: { id: userId } })
      .then(function(user){
        done(null, user);
      }).catch(function(err){
        done(err, null);
      });
  });


  passport.use(new GitHubStrategy({
      clientID: process.env.clientID,
      clientSecret: process.env.secret,
      // if the callback is set to 5000 the 0auth app will not work for some reason
      callbackURL: 'http://127.0.0.1:5000/api/users/auth/github/callback'

    },
    function(accessToken, refreshToken, profile, cb) {
      models.User.findOne({ where: {'id': profile.id } }, 

      function (err, user) {
        if(err) {
          console.log(err);  // handle errors!
        }
        if (!err && user !== null) {
          done(null, user);
        } else {
          models.User.create({
            id: profile.id,
            username: profile.displayName,
            createdAt: Date.now()

          }).then(user => {
            console.log( refreshToken );
            console.log('user created');
            return done(null, user);
          });

        }
      });
    }
  ));
};

routes / users.js

router.get('/auth/github', passport.authenticate('github') );

router.get('/auth/github/callback', 
  passport.authenticate('github', { failureRedirect: '/' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/dashboard');

    console.log('this works');
});

1 个答案:

答案 0 :(得分:0)

此仓库的信誉

https://github.com/vittau/todoapp/blob/master/server.js

config / passport-github.js

  passport.use(new GitHubStrategy({
      clientID: process.env.clientID,
      clientSecret: process.env.secret,
      // if the callback is set to 5000 the 0auth app will not work for some reason
      callbackURL: 'http://127.0.0.1:5000/api/users/auth/github/callback'

    },
    function(accessToken, refreshToken, profile, cb) {

      models.User
      .findOrCreate({where: {id: profile.id}, defaults: {username: profile.displayName}})
      .spread(function(user, created) {
        cb(null, user)
      });

router.get('/ auth / github',护照.authenticate('github',{会话:false,范围:['profile']}))))

router.get('/auth/github/callback', 
  passport.authenticate('github', { failureRedirect: '/' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('http://127.0.0.1:3000/dashboard');

    console.log('this works');
});