3

我尝试将 passport.js 与我的骨干应用程序一起使用来通过 Facebook 应用程序进行身份验证。我可以让路由 /auth/facebook 通过我客户端上的 href-tag 工作。

但我想通过ajax进行身份验证。我试图创建一个调用 authenticate 方法的 express.js 路由。似乎从未调用过身份验证。

express = require('express')
routes = require('./routes')
mongo = require('mongoskin')
crypto = require('crypto')
fs = require('fs')

passport = require('passport')

FBStrategy = require('passport-facebook').Strategy
LocalStrategy = require('passport-local').Strategy

passport.serializeUser (user,done)->
    done(null, user)

passport.deserializeUser (obj, done)->
    done null, obj

fb = new FBStrategy
    clientID: ''
    clientSecret: ''
    callbackURL: 'http://localhost:8811/auth/facebook/callback'
    (accessToken, refreshToken, profile, done) ->
        console.log 'accesstoken', accessToken
        console.log 'profile', profile
        user = {}
        done(null, user)


passport.use fb

logErrors = (err, req, res, next)->
    console.error(err.stack)
    next(err)

clientErrorHandler = (err, req, res, next)->
    if req.xhr
        res.send 500, error: 'something blew up'
    else
        next(err)

errorHandler = (err, req, res, next)->
    res.status 500
    res.render 'error', {error: err}

app.configure ()->
  app.set 'views', __dirname + '/views'
  app.set 'view engine', 'jade'
  app.set 'name','tapuna'
  app.use express.bodyParser({uploadDir: './uploads'})
  app.use express.methodOverride()
  app.use express.cookieParser()
  app.use express.session({secret: 'tapcat'})
  app.use passport.initialize()
  app.use passport.session()
  app.use express.favicon()
  app.use logErrors
  app.use clientErrorHandler
  app.use errorHandler
  app.use express.static(__dirname + '/public.optimized')
  app.use app.router


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

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


# this route works fine
app.get '/auth/facebook', passport.authenticate('facebook', {scope:['email']})
# this one also
app.get '/auth/facebook/callback', passport.authenticate('facebook', {successRedirect:'/', faiureRedirect: '/login'})

# the 'ajax route'
app.post('/fb_login',
    (req,res,next)->
        console.log 'fb_login called' # i see this one
        passport.authenticate('facebook',{scope:['email']}, (err,user,info)->
            console.log 'fb'  # but i never see this one
        )(req, res, next)
    ,
    (err, req,res,next)->
        console.log 'err', err
        res.send status: 'err', message: err
)

非常感谢您的帮助。

4

1 回答 1

0

您无法通过 ajax 进行身份验证。OAuth 要求将用户重定向到身份验证服务器 - 这是 OAuth 流程中的关键步骤。您的 ajax 调用会进行重定向,但用户不会看到它,因为它不在浏览器中。

于 2012-11-27T21:02:00.730 回答