小编lag*_*lex的帖子

使用facebook策略使用护照进行NodeJS身份验证

我正在NodeJS中的一个项目,我想使用用户名/密码或使用Facebook登录.我搜索过,发现有一个名为http://passportjs.org/的 NodeJS模块,它集成了不同社交平台(facebook,twitter等)的不同策略.

登录后我还想访问有关用户的不同信息,例如朋友列表,在墙上发布的可能性等等.从我看到的facebook护照策略不支持这一点.

我的第二个选择是使用https://github.com/amachang/facebook-node-sdk.这允许我登录,并获取朋友列表,在用户墙上发布等.

可以同时使用两者吗?使用来自护照的facebook策略登录,然后将令牌传递给facebook sdk api以使用它在用户墙上发布.

什么是最好的方法?你怎么看到与Facebook的整合?

非常感谢您的回答

sdk facebook login node.js passport.js

3
推荐指数
1
解决办法
4336
查看次数

检查链接是否是图像

我需要检查URL是否是图像.

答案在这里

/(jpg|gif|png)$/.test(...
Run Code Online (Sandbox Code Playgroud)

当我的(特殊)情况下,当非图像URL中存在扩展时,会引发误报

http://www.example.com/jpg/bpgpage/
Run Code Online (Sandbox Code Playgroud)

(PS:不能使用jquery,只能使用javascript/regex)

javascript regex url image

3
推荐指数
2
解决办法
5258
查看次数

PassportJS重定向循环

以下是我的代码

restify = require("restify")
passport = require("passport")
GoogleStrategy = require("passport-google").Strategy

jsonContentType = (req, res, next) ->
    res.setHeader("content-type", "application/json")
    next(req, res, next)

server = restify.createServer(
    name: "Sparked API"
)

passport.use(new GoogleStrategy({
    returnURL: "http://localhost:8080/auth/google/return"
    realm: "http://localhost:8080/"
}, (id, profile, done) ->
    done()
))

server.use(jsonContentType)

server.get("/", (req, res, next) -> 
    res.send(
        message: "hello world!"
    )
)

server.get("/auth/google", passport.authenticate("google"))

server.get("/auth/google/return", passport.authenticate("google", {
    successRedirect: "/"
    failureRedirect: "/"
}))

server.listen(8080, -> console.log("restify listening on 8080"))
Run Code Online (Sandbox Code Playgroud)

看来我即使使用非常简化的版本也会获得重定向循环

server.get("/auth/google/return", passport.authenticate("google", {
    successRedirect: "/"
    failureRedirect: "/"
}))
Run Code Online (Sandbox Code Playgroud)

/是未经身份验证的URL,这是如何导致重定向循环的?我也试过加入 …

javascript node.js coffeescript restify passport.js

3
推荐指数
1
解决办法
1256
查看次数

node.js用护照登录

我正在使用护照实现基本登录系统,但是当我使用正确的凭据登录时,我仍然收到此错误:

Express 500 TypeError:无法读取undefined的属性'passport'

但是,当我使用错误的凭据登录时,通过重定向回登录页面可以正常工作.救命?

app.js

 //dependencies
 var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
var passport = require('./auth');

var app = express();
server = require('http').createServer(app),
    io = require('socket.io').listen(server);
// all environments
app.use(passport.initialize());
app.use(passport.session());
....
app.use(app.router);
app.use(require('less-middleware')({src: path.join(__dirname, 'public')}));
 app.use(express.static(path.join(__dirname, 'public')));
 ......
 //routes

 app.get('/login', routes.login);
app.post('/login', passport.authenticate('local', {
failureRedirect: '/login',
successRedirect: '/user'
}));

 app.get('/user', routes.user);
Run Code Online (Sandbox Code Playgroud)

index.js

    exports.login = function(req, res) {
        res.render('login', {title: 'Log in'});
    };

     exports.user = …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express passport.js

3
推荐指数
1
解决办法
4989
查看次数

即使身份验证成功,PassportJS Facebook登录isAuthenticated也会返回false

出于某种原因,在我的NodeJS Express应用程序上,当使用Facebook通过PassportJS库进行身份验证时,无论身份验证成功并返回配置文件数据,调用request.isAuthenticated()始终返回false.

我已经声明我的身份验证回调将成功重定向到/profile路由,它成功完成:

app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/profile', failureRedirect: '/' }));
Run Code Online (Sandbox Code Playgroud)

使用在确定是否继续处理请求之前验证身份验证的函数声明此路由:

app.get('/profile', ensureAuthenticated, user.list);

该功能的定义如下:

function ensureAuthenticated(req, res, next) {
    if (req.isAuthenticated()) { return next(); }
    return res.redirect('/')
}
Run Code Online (Sandbox Code Playgroud)

你可以看到一切都很简单.我这里没有做任何特别特别的事.

逐步通过PassportJS的代码显示它不会在请求中存储用户数据,除非您assignProperty在声明身份验证中间件时指定选项dict中.这是它在调用时尝试访问的同一属性isAuthenticated(),因为它从不存储这些数据,它总是声称我没有经过身份验证.

不幸的是,指定此密钥会搞砸Express的路由匹配,这会导致404错误处理回调URL,因为检查的代码会assignProperty立即转移到处理下一个可用路由.

我已经将代码全部添加到了pastie中.我很感激任何人都能提供的任何帮助.

javascript node.js passport.js

3
推荐指数
1
解决办法
2048
查看次数

使用Passport.SocketIO cookie解析器错误

我使用以下内容:

"dependencies": {
    "express": "^4.0.0",
    "socket.io": "0.9.16",
    "mongoose": "^3.8.8",
    "passport": "^0.2.0",
    "passport-local": "^1.0.0",
    "express-session": "^1.0.3",
    "cookie-parser": "^1.0.1",
    "body-parser": "^1.0.2",
    "session-mongoose": "git://github.com/danpe/session-mongoose.git#master",
    "passport.socketio": "^3.0.1"
  }
Run Code Online (Sandbox Code Playgroud)

设置我的socket.io授权:

io.set("authorization", passportSocketIo.authorize({
    passport : passport,
    cookieParser: cookieParser(),
    key:    settings.sessionKey,  //the cookie where express (or connect) stores its session id.
    secret: settings.sessionSecret,       //the session secret to parse the cookie
    store:  sessionStore,  //the session store that express uses
    fail: function(data, accept) {
        console.log("failed");
        // console.log(data);// *optional* callbacks on success or fail
        accept(null, false);             // second param takes …
Run Code Online (Sandbox Code Playgroud)

authentication node.js express socket.io passport.js

3
推荐指数
1
解决办法
2131
查看次数

Jade中的Flash消息

以下示例:http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local,除Flash消息部分外,一切正常.

我已经将视图引擎更改为Jade而不是EJS,但我似乎无法显示flash消息.

ejs视图中提供的代码是:

<% if (message.length > 0) { %>
  <div class="alert alert-danger"><%= message %></div>
<% } %>
Run Code Online (Sandbox Code Playgroud)

而我本以为我可以为Jade做这件事:

if #{message}
  p message
Run Code Online (Sandbox Code Playgroud)

甚至:

if message
  p message
Run Code Online (Sandbox Code Playgroud)

但没有成功.知道我做错了什么吗?

注意我按照教程包含了所有必需的库

node.js express passport.js pug

3
推荐指数
1
解决办法
7508
查看次数

什么是\ u001b [9 ...选择控制台上出现的彩色文字的语法,如何添加更多颜色?

我正在调试debugcolors.js以获得比有限的4-6种颜色更多的颜色,但我仍然坚持要弄清楚这种着色语法

args[0] = ' \u001b[9' + c + 'm' + name + ' '
  + '\u001b[3' + c + 'm\u001b[90m'
  + args[0] + '\u001b[3' + c + 'm'
  + ' +' + exports.humanize(ms) + '\u001b[0m';
Run Code Online (Sandbox Code Playgroud)
'blue' : ['\x1B[34m', '\x1B[39m'],
'cyan' : ['\x1B[36m', '\x1B[39m'],
'green' : ['\x1B[32m', '\x1B[39m'],
'magenta' : ['\x1B[35m', '\x1B[39m'],
'red' : ['\x1B[31m', '\x1B[39m'],
'yellow' : ['\x1B[33m', '\x1B[39m'],
Run Code Online (Sandbox Code Playgroud)

我知道Windows控制台允许的颜色比这六个更多,如图color /?所示

0 = Black       8 = Gray
1 = Blue        9 = Light Blue …
Run Code Online (Sandbox Code Playgroud)

console colors windows-console node.js

3
推荐指数
1
解决办法
2921
查看次数

将变量从jade传递到ng-init不起作用

我正试图将一个对象从jade传递给ng-init

这个:不起作用:

ng-init='tables=!{JSON.stringify(tables)}'
Run Code Online (Sandbox Code Playgroud)

这个:扩展但是,

ng-init='tables=#{JSON.stringify(tables)}'
Run Code Online (Sandbox Code Playgroud)

输出未转义并填充&quot;s

ng-init="tables={&quot;12&quot;:{&quot;id&quot;:....
Run Code Online (Sandbox Code Playgroud)

并且在任何一种情况下都不更新视图.这篇文章暗示第一个应该工作,但就像我说的,它甚至没有扩展,

ng-init='tables=!{JSON.stringify(tables)}'
Run Code Online (Sandbox Code Playgroud)

源代码在HTML源代码中显示完全相同

ng-init='tables=!{JSON.stringify(tables)}'
Run Code Online (Sandbox Code Playgroud)

node.js express angularjs angularjs-ng-init pug

3
推荐指数
1
解决办法
1919
查看次数

只获得total_count的朋友 - facebook-graph-api

me?fields=friends

{
  "friends": {
    ...
    "summary": {
      "total_count": 72
    }
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

我只是想要total_count,但它似乎不是我可以放入查询的有效子字段

me?fields=friends{summary}me?fields=friends{total_count}不起作用.

不是绝对必要,只是好奇为什么不是或是否可能?

facebook-graph-api

3
推荐指数
1
解决办法
2185
查看次数