nodejs passport总是会失败的URL

时间:2012-10-01 22:24:47

标签: node.js facebook coffeescript passport.js

我使用护照允许在我的网站上通过Facebook登录。一切似乎工作得很好,但Facebook的网站回调总是转到failureRedirect,即使我从Facebook获得有效令牌的所有数据。关于为什么会发生这种情况的任何想法?

express = require "express"
passport = require 'passport'
FBStrategy = require('passport-facebook').Strategy
Properties = require "./config.json"

FB_APP_ID = REMOVED
FB_APP_SECRET = REMOVED

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

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

fb = new FBStrategy  
  clientID: FB_APP_ID
  clientSecret: FB_APP_SECRET
  callbackURL: "http://localhost/auth/facebook/callback"
  (accessToken, refreshToken, profile, done) ->
    console.log "accesToken ", accessToken
    console.log "refreshToken", refreshToken
    console.log "profile", profile
    done()

passport.use fb

app = module.exports = express.createServer()
app.Properties = Properties

console.log "starting server"
app.configure ->
  app.set "views", "./views"
  app.set "view engine", "jade"
  app.set "view options", layout: false
  app.use express.methodOverride()
  app.use (express.static "#{__dirname}")
  app.use express.cookieParser()
  app.use express.bodyParser()
  app.use (express.session secret: REMOVED, key: REMOVED)
  app.use passport.initialize()
  app.use passport.session()
  app.use app.router

app.configure "development", ->
  app.listen Properties.Port


app.get "/", (request, response) -> 
  { session: Session } = request
  response.render "index"

app.get "/account", (request, response) -> 
  { session: Session } = request
  response.render "account"

app.get "/login", (request, response) -> 
  { session: Session } = request
  response.render "login", {user:request.user}

app.get "/auth/facebook", passport.authenticate 'facebook'

app.get "/auth/facebook/callback", passport.authenticate ('facebook', {failureRedirect: "/login"}), (req, res) ->
  res.redirect "/account"


console.log "server is ready and listening on port #{Properties.Port}"

1 个答案:

答案 0 :(得分:4)

您需要在策略的验证回调中提供用户。像这样:

fb = new FBStrategy  
  clientID: FB_APP_ID
  clientSecret: FB_APP_SECRET
  callbackURL: "http://localhost/auth/facebook/callback"
  (accessToken, refreshToken, profile, done) ->
    console.log "accesToken ", accessToken
    console.log "refreshToken", refreshToken
    console.log "profile", profile
    user = {} // find or create a user in your database
    done(null, user)

您可以在指南中详细了解验证回调:http://passportjs.org/guide/configuration.html