everyauth is not defined
Asked Answered
S

3

9

I am new to nodejs, everyauth,etc. I am having some trouble with everyauth. In my view, if I access everyauth object, I get an error 'everyauth is not defined'. However the oauth flow itself works fine with everyauth. Here are the details,

entry point - 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');
});

home.jade

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

node modules installed,

[email protected]:\dev\misc\hge\highrise
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └─┬ [email protected]
│ │   ├── [email protected]
│ │   └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
└─┬ [email protected]
  └── [email protected]

Edit - Adding the example which I followed,

From everyauth site

In the main app file - https://github.com/bnoguchi/everyauth/blob/master/example/server.js, render the view using..

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

In the view file,access the everyauth object - https://github.com/bnoguchi/everyauth/blob/master/example/views/home.jade

- if (!everyauth.loggedIn)
  h2 Not Authenticated

The everyauth object is not passed to the view here, unless I am missing something.

Sorenson answered 27/8, 2012 at 2:53 Comment(0)
C
3

This is an old question, but I was having the same problem and was very frustrated to see that another dev had the exactly same problem and the only answers where completely off

It took me a couple of hours to do a line-by-line comparisson of my code and the examples, but finally got it right:

you have:

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

but shoud be

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

and that's it. now everyauth is defined on the jade views and all works as expected.

Conscript answered 2/1, 2013 at 1:40 Comment(1)
This solution isn't working for me in the latest express and everyauth versions. Posted the solution below that worked for me. Hope it helps.Pool
P
0

The following solution from this LINK worked very well for me -

Add the following functions to your 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();
  };
};

And update this line

app.use(everyauth.middleware());

like this -

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

Hope it helps.

Pool answered 8/5, 2013 at 23:39 Comment(0)
W
-1

That's because you trying to access everyauth from jade template. Although you could pass the everyauth variable to the template, this is not good practice.

Instead the if/else block should remain in your controller. Then you can render a different template if the user is login or not.

Something like this

if(everyauth.loggedIn){

  template = 'authenicated';
}
 else{

  template = 'unauthenicated';
}

res.render(template, {

    title: { title: 'Title' }

});
Winne answered 27/8, 2012 at 4:13 Comment(1)
I added the example I followed from everyauth github page. The everyauth object is not passed to the template. I was trying to understand if I a missing something here.Sorenson

© 2022 - 2024 — McMap. All rights reserved.