bnoguchi / everyauth

node.js auth package (password, facebook, & more) for Connect and Express apps
http://everyauth.com/
3.49k stars 447 forks source link

Cannot GET /auth/twitter #208

Open JanVanRyswyck opened 12 years ago

JanVanRyswyck commented 12 years ago

Hi,

I'm developing a web app using Express (+ CoffeeScript) and I wanted to incorporate everyauth. I followed the code from the examples and the NodeTuts episode, but I always get "Cannot GET /auth/twitter" as soon as I want to login (/auth/twitter). I tried the same approach in a seperate spike application, but this seems to work fine. I only get this issue in the web app. I've been trying to get this working for several days now but still no luck. I guess it has something to do with routing/middleware but I can't seem to point my finger to anything useful.

Does anyone have a clue regarding this matter. Many thanks in advance.

bnoguchi commented 12 years ago

Can you provide a gist here, so I can reproduce the issue?

bnoguchi commented 12 years ago

One thing to try is to make sure that if you explicitly declare your route middleware, then make sure it is declared after you declare your everyauth middleware. In other words, don't do this:

app.use(app.router);
app.use(everyauth.middleware());

Do this:


app.use(everyauth.middleware());
app.use(app.router);

The other scenario is if you don't explicitly declare the router middleware but instead rely on express's implicit and lazy loading of it via your first route declaration. In this case, make sure that the call to everyauth.middleware() occurs before your first app.get(...), app.post(...), etc.

i.e., don't do this:

app.get('/some/path', function (req, res, next) {
});

app.use(everyauth.middleware());

Do this:

app.use(everyauth.middleware());

app.get('/some/path', function (req, res, next) {
});
JanVanRyswyck commented 12 years ago

I correctly added the middleware as far as I can see. I added a gist that contains the bootstrapping code of my app:

https://gist.github.com/2015931

digitalrinaldo commented 12 years ago

I am having the same issue with both twitter and gtihub. I am also unable to get the example to work.

bash-3.2# node server.js Go to http://local.host:3000 ReferenceError: /tmp/e/views/home.jade:1

1| - if (!everyauth.loggedIn) 2| h2 Not Authenticated 3| #register 4| a(href='/register') Register

bnoguchi commented 12 years ago

@digitalrinaldo Run the example from the npm-installed version or from tag v0.2.32. The current HEAD for master is v0.3.x work in progress.

bnoguchi commented 12 years ago

@JanVanRyswyck The function you pass to findOrCreateUser is supposed to return a user object (that your application/db creates) with an id. Instead of returning the twitterUser (which is just metadata from twitter's api), find or create a user with couchdb and return that inside of a promise. e.g.,

everyauth.twitter
  .consumerKey(myConsumerKey)
  .consumerSecret(myConsumerSecret)
  .findOrCreateUser(
    (session, accessToken, accessTokenSecret, twitterUserMetadata) ->
      userPromise = @Promise()
      findOrCreateUserWithCouchDb accessToken, accessTokenSecret, twitterUserMetadata, (err, user) ->
        return userPromise.fail err if err
        userPromise.fulfill user

      return userPromise
    )
  .redirectPath('/')
JanVanRyswyck commented 12 years ago

Is that the reason why I'm not getting the Twitter login page. I assumed that the findOrCreateUser was called after the login process.

bnoguchi commented 12 years ago

That's not the reason; just pointing out something to fix. Still looking over your gist.

bnoguchi commented 12 years ago

What version of everyauth are you on?

bnoguchi commented 12 years ago

Can you update the gist so that it contains a package.json and the coffee code in the right order so I can run it completely on my machine to try and re-produce the issue? Then, I can npm install in a local directory with the exact same dependencies you are using.

digitalrinaldo commented 12 years ago

I did an npm install and am getting a 404 for /auth/twitter or /auth/github

bash-3.2# npm list /private/tmp/e ├─┬ everyauth@0.2.32 │ ├─┬ connect@1.8.5 │ │ ├── formidable@1.0.9 │ │ ├── mime@1.2.5 │ │ └── qs@0.4.2 │ ├── debug@0.5.0 │ ├── node-swt@0.1.1 │ ├── node-wsfederation@0.1.1 │ ├── oauth@0.9.6 │ ├── openid@0.4.1 │ ├── request@2.9.153 │ ├── restler@2.0.0 │ └─┬ xml2js@0.1.13 │ └── sax@0.3.5 ├─┬ express@2.5.8 │ ├─┬ connect@1.8.5 │ │ └── formidable@1.0.9 │ ├── mime@1.2.4 │ ├── mkdirp@0.3.0 │ └── qs@0.4.2 └─┬ jade@0.21.0 ├── commander@0.5.2 └── mkdirp@0.3.0 bash-3.2#

bash-3.2# npm -v 1.0.106 bash-3.2# node -v v0.6.5 bash-3.2#

digitalrinaldo commented 12 years ago

A gist ot the code I am using is here https://gist.github.com/2018224

bnoguchi commented 12 years ago

@digitalrinaldo You need to move all configuration of everyauth before the call to everyauth.middleware(). See my fork of your gist.

digitalrinaldo commented 12 years ago

I feel like an idiot, thanks for catching that. It is working. I really like the organization of your library. I hope to use it extensively. Is there any convention on the id returned for say github, any unique string of digits will do as long as you can look them up when needed?

bnoguchi commented 12 years ago

@digitalrinaldo Yes, please refer to the github documentation for details about info returned from their API.

JanVanRyswyck commented 12 years ago

You can find the full source code of the app I'm building at the following location: https://github.com/JanVanRyswyck/trackmyrun

CCVonLewin commented 12 years ago

greetings, sporadically i experience the same issue but not always (which is the worst type of issue to debug). please forgive my inexperience w/ commenting on a ticket, this is my first post in github on an issue. btw amazing module - i love it!

VERSIONS node 0.6.5 everyauth@0.2.32 express@2.5.2 jade@0.21.0 mongodb@0.9.9-7

APP var everyauth = require("everyauth"), ...;

everyauth.debug = true; console.log( "fb appId[0]: " + CONFIG.authentication.facebook.appId.charAt(0) );

// add user var usersById = {}, usersByFbId = {}, usersByTwitId = {}, nextUserId = 0;

function addUser (source, sourceUser) { var user; user = usersById[++nextUserId] = {id: nextUserId}; user[source] = sourceUser; return user; }

// facebook everyauth.facebook .appId(CONFIG.authentication.facebook.appId) .appSecret(CONFIG.authentication.facebook.appSecret) .findOrCreateUser( function (session, accessToken, accessTokenExtra, fbUserMetadata) { console.log( "fb meta id: " + fbUserMetadata.id); return usersByFbId[fbUserMetadata.id] || (usersByFbId[fbUserMetadata.id] = addUser('facebook', fbUserMetadata)); }) .redirectPath("#!/client/facebook/");

// twitter everyauth.twitter .consumerKey(CONFIG.authentication.twitter.consumerKey) .consumerSecret(CONFIG.authentication.twitter.consumerSecret) .findOrCreateUser( function (sess, accessToken, accessSecret, twitUser) { return usersByTwitId[twitUser.id] || (usersByTwitId[twitUser.id] = addUser('twitter', twitUser)); }) .redirectPath("#!/client/twitter/");

var app = module.exports = express.createServer(); app.configure(function(){ ... app.use( everyauth.middleware() ); app.use( app.router ); });

app.get( '*', function ( request, response ) { ... });

everyauth.helpExpress( app );

app.listen( 3000 );

LOG Fri, 23 Mar 2012 02:22:57 GMT | GET > / | 200 in [25 ms] starting step - getRequestToken ...finished step starting step - storeRequestToken ...finished step starting step - redirectToProviderAuth ...finished step Fri, 23 Mar 2012 02:23:02 GMT | GET > /auth/twitter | 303 in [300 ms] starting step - extractTokenAndVerifier ...finished step starting step - getSession ...finished step starting step - rememberTokenSecret

node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Step rememberTokenSecret of twitter is promising: requestTokenSecret ; however, the step returns nothing. Fix the step by returning the expected values OR by returning a Promise that promises said values. at Object.exec (/home/cure/app/node_modules/everyauth/lib/step.js:68:11) at /home/cure/app/node_modules/everyauth/lib/stepSequence.js:26:38 at [object Object].fulfill (/home/cure/app/node_modules/everyauth/lib/promise.js:42:25) at /home/cure/app/node_modules/everyauth/lib/stepSequence.js:29:23 at [object Object].callback (/home/cure/app/node_modules/everyauth/lib/promise.js:13:12) at /home/cure/app/node_modules/everyauth/lib/stepSequence.js:28:23 at [object Object].fulfill (/home/cure/app/node_modules/everyauth/lib/promise.js:42:25) at /home/cure/app/node_modules/everyauth/lib/modules/oauth.js:154:15 at Array.0 (/home/cure/app/node_modules/express/node_modules/connect/lib/middleware/session/memory.js:75:11) at EventEmitter._tickCallback (node.js:192:40)