在 CoffeeScript 中使用express连接身份验证

问题描述 投票:0回答:1

我运行了 connect-auth 和 Google OAuth2 的示例,但无法在我自己的代码中运行它。不知何故,我从 Google 获得了 access_token,但是当我尝试使用访问令牌通过curl 获取数据时,出现 401 - 您的请求中存在错误。这与我使用示例代码时从 Google 得到的响应相同,但令牌一定有问题?

我想使用中间件,因为我需要在整个项目中进行身份验证。另外,我使用的是快速路由器而不是连接路由器。这是代码:

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 路由:

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) );
  }
};
node.js express authentication coffeescript node.js-connect
1个回答
1
投票

我不懂咖啡脚本,但是:

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()

应该是:

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`

否则 下一个()

我认为最大的问题是你要发送一个“范围”,尝试:

    auth.Google2({  appId : keys.google2Id, appSecret: keys.google2Secret, callback: keys.google2CallbackAddress})
© www.soinside.com 2019 - 2024. All rights reserved.