我正在使用 Facebook 登录作为我自己的 RESTful API我的记事本应用程序在这里 https://github.com/iliyan-trifonov/notepads-nodejs-angularjs-mongodb-bootstrap。我启动该应用程序时将其用作网页,但登录后的通信仍将通过 API 进行。
然后我决定创建一个同一应用程序的移动版本 https://github.com/iliyan-trifonov/notepads-ionic将使用 API。我决定这样做:移动应用程序通过 Facebook 登录,并将 facebook 用户 ID 和 FB 访问令牌发送到 API,API 调用 Facebook 的 API 来验证这些参数,如果成功注册新用户(或登录现有的)在我的应用程序的数据库中,为该用户创建一个自定义令牌并将其返回到移动应用程序。移动应用程序从此处发送此自定义令牌以使用 API 对应用程序进行身份验证。
这是一些代码:
API 中的身份验证(使用 fbgraph npm 模块):
var graph = require('fbgraph'),
Promise = require('bluebird')
...
Promise.promisify(graph.get);
...
var postAuthHandler = function (req, res) {
var fbUserId = req.body.fbId,
fbAccessToken = req.body.fbAccessToken,
accessToken = req.body.accessToken;
...
graph.setAppSecret(config.facebook.app.secret);
graph.setAccessToken(fbAccessToken);
var graphUser;
var p = graph.getAsync('me?fields=id,name,picture')
.then(function (fbGraphUser) {
//when the given fb id and token mismatch:
if (!fbGraphUser || fbGraphUser.id !== fbUserId) {
console.error("Invalid user from fbAccessToken!");
res.status(HttpStatus.FORBIDDEN).json({});
return p.cancel();
}
graphUser = fbGraphUser;
return User.fb(fbUserId);
})
.then(function (user) {
if (user) {
//user found by his FB access token
res.status(HttpStatus.OK).json({accessToken: user.accessToken});
//stop the promises chain here
return p.cancel();
}
...create the user, generate a custom token and return it as above...
https://github.com/iliyan-trifonov/notepads-nodejs-angularjs-mongodb-bootstrap/blob/6617a5cb418ba8acd6351ef9a9f69228f1047154/src/routes/users.js#L46 https://github.com/iliyan-trifonov/notepads-nodejs-angularjs-mongodb-bootstrap/blob/6617a5cb418ba8acd6351ef9a9f69228f1047154/src/routes/users.js#L46 .
用户模型:
var userSchema = new mongoose.Schema({
facebookId: { type: String, required: true, unique: true },
accessToken: { type: String, required: true, unique: true },
name: { type: String, required: true },
photo: { type: String, required: true },
categories: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Category' }],
notepads: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Notepad' }]
});
https://github.com/iliyan-trifonov/notepads-nodejs-angularjs-mongodb-bootstrap/blob/master/src/models/user.js#L9 https://github.com/iliyan-trifonov/notepads-nodejs-angularjs-mongodb-bootstrap/blob/master/src/models/user.js#L9 .
移动应用程序中的 Facebook 身份验证:
auth: function(fbId, fbAccessToken) {
return $http({
url: apiBase + '/users/auth',
data: {
fbId: fbId,
fbAccessToken: fbAccessToken
},
method: 'POST',
cache: false
});
},
...
https://github.com/iliyan-trifonov/notepads-ionic/blob/master/www/js/services.js#L33 https://github.com/iliyan-trifonov/notepads-ionic/blob/master/www/js/services.js#L33 .
移动应用程序随请求发送令牌:
notepads: {
list: function() {
return $http({
url: apiBase + '/notepads?insidecats=1' + '&token=' + User.get().accessToken/*gets the token from the local storage*/,
method: 'GET',
cache: false
});
},
这是一个 Ionic/Angular/Cordova 应用程序。从移动应用程序登录 Facebook 将启动安装在您手机上的 Facebook 应用程序或打开一个弹出窗口以登录 Facebook。然后回调将 Facebook 用户的 ID 和访问令牌返回到我的移动应用程序。
fbgraph npm 模块:https://github.com/criso/fbgraph https://github.com/criso/fbgraph