9

我是 nodejs、everyauth 等的新手。我在everyauth方面遇到了一些麻烦。在我看来,如果我访问everyauth 对象,我会收到一个错误“未定义everyauth”。然而,oauth 流程本身适用于everyauth。以下是详细信息,

入口点 - app.js

var express = require('express');
var everyauth = require('everyauth');
everyauth.debug = true;
var app = express();


everyauth['37signals']
  .appId('e6e76726501abf1b5627fe854b384ef8d62d7a55')
  .appSecret('7c6891f46cb19aaf1831785968630ed4a1b3c342')
  .findOrCreateUser( function (sess, accessToken, accessSecret, _37signalsUser) {
  //code to handle find or create
}
  .redirectPath('/');

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'foobar' }));
  app.use(express.bodyParser());
  app.use(everyauth.middleware());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
  everyauth.helpExpress(app);
});




app.configure('development', function(){
  console.log('inside development configure');
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
  app.use(express.errorHandler()); 
});

app.get('/', function (req, res) {
  console.log('everyauthloggedin='+ everyauth.loggedIn); // everyauth.loggedIn is undefined
    res.render('home');
});

家玉

if(!everyauth.loggedIn) // get everyauth is not defined
    h2 Not authenicated 
else
    h2 Authenicated
    p= JSON.stringify(everyauth['37signals'].user)  

安装的节点模块,

highrise@0.0.1c:\dev\misc\hge\highrise
├─┬ everyauth@0.2.34
│ ├─┬ connect@2.3.9
│ │ ├── bytes@0.1.0
│ │ ├── cookie@0.0.4
│ │ ├── crc@0.2.0
│ │ ├── formidable@1.0.11
│ │ ├── fresh@0.1.0
│ │ ├── qs@0.4.2
│ │ └─┬ send@0.0.3
│ │   ├── mime@1.2.6
│ │   └── range-parser@0.0.4
│ ├── debug@0.5.0
│ ├── node-swt@0.1.1
│ ├── node-wsfederation@0.1.1
│ ├── oauth@0.9.8
│ ├── openid@0.4.2
│ ├── request@2.9.203
│ └─┬ xml2js@0.1.14
│   └── sax@0.4.2
├─┬ express@3.0.0rc3
│ ├── commander@0.6.1
│ ├─┬ connect@2.4.3
│ │ ├── bytes@0.1.0
│ │ ├── formidable@1.0.11
│ │ ├── pause@0.0.1
│ │ └── qs@0.4.2
│ ├── cookie@0.0.4
│ ├── crc@0.2.0
│ ├── debug@0.7.0
│ ├── fresh@0.1.0
│ ├── methods@0.0.1
│ ├── mkdirp@0.3.3
│ ├── range-parser@0.0.4
│ └─┬ send@0.0.3
│   └── mime@1.2.6
├─┬ jade@0.27.2
│ ├── commander@0.6.1
│ └── mkdirp@0.3.0
├── request@2.10.0
├─┬ sequelize@1.5.0
│ ├── commander@0.6.1
│ ├── generic-pool@1.0.9
│ ├── lingo@0.0.5
│ ├── moment@1.1.1
│ ├─┬ mysql@0.9.6
│ │ └─┬ hashish@0.0.4
│ │   └── traverse@0.6.3
│ ├── underscore@1.2.4
│ ├── underscore.string@2.0.0
│ └── validator@0.3.9
└─┬ xml2js@0.1.14
  └── sax@0.4.2

编辑 - 添加我遵循的示例,

来自everyauth 网站

在主应用程序文件 - https://github.com/bnoguchi/everyauth/blob/master/example/server.js中,使用..渲染视图

app.get('/', function (req, res) {
  res.render('home');
});

在视图文件中,访问everyauth 对象 - https://github.com/bnoguchi/everyauth/blob/master/example/views/home.jade

- if (!everyauth.loggedIn)
  h2 Not Authenticated

除非我遗漏了某些东西,否则everyauth 对象不会传递给这里的视图。

4

3 回答 3

3

这是一个老问题,但我遇到了同样的问题,并且非常沮丧地看到另一个开发人员遇到了完全相同的问题并且唯一的答案完全不正确

我花了几个小时对我的代码和示例进行逐行比较,但最终做对了:

你有:

app.configure(function(){
    //...
    app.use(everyauth.middleware());
    //...
}

但应该是

app.configure(function(){
    //...
    app.use(everyauth.middleware(app));
    //...
}

就是这样。现在everyauth在玉视图上定义,所有工作都按预期进行。

于 2013-01-02T01:40:17.280 回答
0

此LINK中的以下解决方案对我来说效果很好 -

将以下函数添加到您的 app.js

function preEveryauthMiddlewareHack() {
 return function (req, res, next) {
  var sess = req.session
    , auth = sess.auth
    , ea = { loggedIn: !!(auth && auth.loggedIn) };

  // Copy the session.auth properties over
  for (var k in auth) {
    ea[k] = auth[k];
  }

  if (everyauth.enabled.password) {
    // Add in access to loginFormFieldName() + passwordFormFieldName()
    ea.password || (ea.password = {});
    ea.password.loginFormFieldName = everyauth.password.loginFormFieldName();
    ea.password.passwordFormFieldName = everyauth.password.passwordFormFieldName();
  }

  res.locals.everyauth = ea;

  next();
}

};

function postEveryauthMiddlewareHack() {
  var userAlias = everyauth.expressHelperUserAlias || 'user';
  return function( req, res, next) {
    res.locals.everyauth.user = req.user;
    res.locals[userAlias] = req.user;
    next();
  };
};

并更新这一行

app.use(everyauth.middleware());

像这样 -

   app.use(preEveryauthMiddlewareHack());
   app.use(everyauth.middleware());
   app.use(postEveryauthMiddlewareHack());

希望能帮助到你。

于 2013-05-08T23:39:38.490 回答
-1

那是因为您试图从玉模板访问everyauth。尽管您可以将everyauth 变量传递给模板,但这不是一个好的做法。

相反,if/else 块应该保留在您的控制器中。然后,无论用户是否登录,您都可以呈现不同的模板。

像这样的东西

if(everyauth.loggedIn){

  template = 'authenicated';
}
 else{

  template = 'unauthenicated';
}

res.render(template, {

    title: { title: 'Title' }

});
于 2012-08-27T04:13:59.543 回答