паспорт: различное перенаправление для входа в систему и регистрации учетной записи

я использую модуль паспорта (аутентификация github) в своем приложении, и я хочу перенаправить в зависимости от действия... я проверяю, обычный ли это вход в систему или пользователь входит в систему в первый раз .

passport.use(new GitHubStrategy({
    clientID: conf.github.app_id,
    clientSecret: conf.github.app_secret,
    callbackURL: conf.github.callback_url
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {

      // To keep the example simple, the user's GitHub profile is returned to
      // represent the logged-in user.  In a typical application, you would want
      // to associate the GitHub account with a user record in your database,
      // and return that user instead.

      Models_User.findOrCreateUser(profile, function(msg){
        console.log("auth type:" + msg);
      });

      return done(null, profile);

    });
  }
));

В моей функции findOrCreateUser я проверяю, является ли это новый пользователь, и выполняю все действия с базой данных... для тестирования я позволяю функции возвращать переменную msg, которая представляет собой только строку, которая говорит «логин» или «новая_регистрация».

Поэтому мой вопрос заключается в том, как «перенести» ту переменную, которую я получаю от findOrCreateUser, чтобы я мог соответствующим образом перенаправить («/welcome» или «/back_again») после завершения аутентификации паспорта.

другой код паспорта в моем приложении:

// GET /auth/github
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  The first step in GitHub authentication will involve redirecting
//   the user to github.com.  After authorization, GitHubwill redirect the user
//   back to this application at /auth/github/callback
app.get('/auth/github',
  passport.authenticate('github'),
  //passport.authenticate('github', { scope: ['user', 'public_repo', 'gist'] }),
  function(req, res){
    // The request will be redirected to GitHub for authentication, so this
    // function will not be called.
  });

// GET /auth/github/callback
//   Use passport.authenticate() as route middleware to authenticate the
//   request.  If authentication fails, the user will be redirected back to the
//   login page.  Otherwise, the primary route function function will be called,
//   which, in this example, will redirect the user to the home page.
app.get('/auth/github/callback', 
  passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });
9
задан laggingreflex 6 January 2015 в 14:54
поделиться