node.jsexpressauthenticationcoffeescriptnode.js-connect

connect-auth with express in CoffeeScript


I got the example for connect-auth and Google OAuth2 running, but can't get it working in my own code. Somehow, I get an access_token from Google, but when I try to get data via curl with the access token, there's a 401 - There was an error in your request. It's the same response I get from Google when I use the example code, but something must be wrong with the token?

I'd like to use the middleware, as I need authentication in the whole project. Also, I'm using the express router instead of the connect router. Here's the code:

express     = require 'express'
app         = module.exports = express.createServer()
connect     = require 'connect'
auth        = require 'connect-auth'
store       = require('connect-redis')(express)
keys        = require './keys_file'
routes      = require("./routes")

auth_middleware = ->
  (req, res, next) ->
    urlp = url.parse(req.url, true)
    if urlp.query.login_with
      req.authenticate [ urlp.query.login_with ], (error, authenticated) ->
        if error
          console.log error
          res.end()
        else
          next()  unless authenticated is `undefined`
    else
      next()

app.configure ->
  app.set 'views', __dirname + '/views'
  app.set 'view engine', 'jade'
  app.use connect.logger()
  app.use express.bodyParser()
  app.use express.cookieParser()
  app.use express.session({ secret: 'keyboard cat', store: new store })
  app.use express.static(__dirname + '/public')
  app.use auth({strategies: [
    auth.Twitter({  consumerKey: keys.twitterConsumerKey, consumerSecret: keys.twitterConsumerSecret}),
    auth.Google2({  appId : keys.google2Id, appSecret: keys.google2Secret, scope: "email", callback: keys.google2CallbackAddress})
  ], trace: true})
  app.use auth_middleware
  app.use app.router

app.configure "development", ->
  app.use express.errorHandler(
    dumpExceptions: true
    showStack: true
  )

app.configure "production", ->
  app.use express.errorHandler()

app.dynamicHelpers
  session: (req, res) ->
    return req.session

app.dynamicHelpers
  messages: require 'express-messages'

app.get /.*/, routes.index

app.listen 3000

console.log 'Express server listening on port %d in %s mode', app.address().port, app.settings.env

index.js routing:

exports.index = function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'})
  if( req.isAuthenticated() ) {
    res.end( authenticatedContent.replace("#USER#", JSON.stringify( req.getAuthDetails().user )  ) );
  } else {
     res.end( unAuthenticatedContent.replace("#PAGE#", req.url) );
  }
};

Solution

  • I don't know coffeescript, but :

    auth_middleware = ->
      (req, res, next) ->
        urlp = url.parse(req.url, true)
        if urlp.query.login_with
          req.authenticate [ urlp.query.login_with ], (error, authenticated) ->
            if error
              console.log error
              res.end()
            else
              next()  unless authenticated is `undefined`
       else
          next()
    

    should be :

    auth_middleware = (req, res, next)->
      urlp = url.parse(req.url, true)
      if urlp.query.login_with
        req.authenticate [ urlp.query.login_with ], (error, authenticated) ->
          if error
            console.log error
            res.end()
       else
        next()  unless authenticated is `undefined`
    

    else next()

    I think the big issue though is you're sending a 'scope' up, try:

        auth.Google2({  appId : keys.google2Id, appSecret: keys.google2Secret, callback: keys.google2CallbackAddress})