Javascript Facebook登录和节点js服务器
我正在尝试创建一个类似Tinder的android应用程序 到目前为止,我使用的是facebook登录,它的工作方式如下:用户将访问令牌发送到我的服务器(节点js中的writen),然后它使用paspport facebook令牌库授权他 现在我需要创建一个使用mongoose的用户数据库。我应该怎么做,在哪里做?如果我只有facebook登录,如何保存用户?我需要根据什么来保存用户 这是我的server routes.js代码:Javascript Facebook登录和节点js服务器,javascript,android,node.js,facebook,Javascript,Android,Node.js,Facebook,我正在尝试创建一个类似Tinder的android应用程序 到目前为止,我使用的是facebook登录,它的工作方式如下:用户将访问令牌发送到我的服务器(节点js中的writen),然后它使用paspport facebook令牌库授权他 现在我需要创建一个使用mongoose的用户数据库。我应该怎么做,在哪里做?如果我只有facebook登录,如何保存用户?我需要根据什么来保存用户 这是我的server routes.js代码: module.exports = function(ap
module.exports = function(app) {
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.end("Node-Android-Chat-Project");
});
passport.use(new FacebookStrategy({
clientID: *****1246,
clientSecret: "******",
callbackURL: "http://localhost:8080/auth/facebook/callback"
},
function(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)
}
));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback
app.post('/auth/facebook/token', function(req, res, next) {
passport.authenticate(['facebook-token'], function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
console.log(req.user);
return res.send({ success : false, message : 'authentication failed' });
}
console.log("Success!!");
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);
});
/*app.post('/auth/facebook/token',
passport.authenticate('facebook-token'),
function (req, res) {
console.log(req)
// do something with req.user
res.send(req.user? 200 : 401);
}
); */
//app.get('/auth/facebook', passport.authenticate('facebook'));
//app.get(, );
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
};
您几乎准备好了,现在需要做的就是将该用户保存到数据库中。我为mongoose使用了一个名为findOneOrCreate的漂亮小插件,它提供了一个快捷方式来执行FindModify,它实际上是连续查找和创建的(请查看真正的findAndModify。您可以使用upsert选项来获得此行为) 以下代码将初始化用户mongoose模式。把它放在NodeJS服务器的init部分
var mongoose = require('mongoose');
var findOneOrCreate = require('mongoose-find-one-or-create');
var User = new mongoose.Schema({
facebookId: {
type: 'String',
unique: true,
sparse: true
},
firstName: {
type: 'String',
match: mongo.nameRegex
},
lastName: {
type: 'String',
match: mongo.nameRegex
},
birthday: {type: 'Date'}
}, { collection: 'users' });
User.plugin(findOneOrCreate);
然后使用以下代码:
passport.use(new FacebookStrategy({
clientID: *****1246,
clientSecret: "******",
callbackURL: "http://localhost:8080/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
console.log("accesToken ", accessToken)
console.log ("refreshToken", refreshToken)
console.log ("profile", profile)
var user = {
facebookId: profile.id,
firstName: profile._json.first_name,
lastName: profile._json.last_name,
birthday: profile._json.birthday
};
//the same User variable from above
User.findOneOrCreate({facebookId: profile.id}, user, function (err, user) {
return done(err, user, {type: 'facebook'});
});
}
));
之后,在路由处理程序中,您将可以访问当前在req.user
中经过身份验证的用户
例:
Facebook登录已转移到新的GraphQLAPI。您可以在此处看到如何将Facebook登录与Node.js服务器集成:谢谢!为什么您的Ex.代码将返回当前用户?请签出passport。我遗漏了需要保护
/me
端点的部分。
app.get('/me', passport.authenticate('facebook-token'), function(req, res) {
res.status(200).send(req.user);
});