我到处找遍了,但对这个问题找不到令人满意的答案。
我有一个流星网站,用户登录和创建内容。我还想创建一个手机应用程序,它能够与网站互动,我希望用户登录到手机应用程序,并访问相同的内容在网站上。很正常。
我已经创建了一个使用陨石包HTTP.publish访问集合的基本REST。它在没有任何用户信息的情况下工作(没有auth),但是现在我想使用GET方法的userId和集合的Meteor.allow规则来访问当前用户。
因此,我目前正在努力解决如何在REST请求上告诉流星,用户的id,即使只是测试。我想我可以在浏览器中获得有效用户的Accounts._storedLoginToken,并使用它进行CURL测试。有点像
curl -H "X-Auth-Token: asdklfjasldfjlsadkjf" -H "Content-Type: application/json" -d '{"name":"A Name","description":"Testing description"}' http://localhost:3000/api/places
我试过这个,但是没有joy,我得到了一个403,这至少是好的。
我的问题是:
X-Auth-Token的使用方式?如果不是,我在卷发命令中做错了什么。例如,/api/login?user=shane&pwd=qwerty =>返回token,我可以在curl请求中使用。
我真的被困在这,所以任何指向我正确方向的东西都会被感激。我还注意到,http.publish还没有创建登录/注销方法,所以可能不是那么容易。
发布于 2014-07-06 17:25:56
几天前,我开始开发一个具有类似认证要求的应用程序。我发现support的RESTstop2最近在0.6.0版本中升级了他们的身份验证支持,以支持在Meteor中新添加的Bcrypt加密。
您只需将用户名和密码发送为URL参数或正文,如下所示:
curl --data "password=testpassword&user=test" http://localhost:3000/api/login/服务器将返回以下内容(如果凭据正确):
{ success: true, loginToken: "f2KpRW7KeN9aPmjSZ", userId: fbdpsNf4oHiX79vMJ }在向服务器提出的每个请求中,都将loginToken和userId作为标头。
你应该去看看:
文档: http://github.differential.io/reststop2/
发布于 2014-07-07 12:28:20
另一个选项(除了在其他答案中提到的RESTstop2 ),您可以从大气中使用独立的api-密码包,这正是您所需要的:在服务器端验证REST调用。它还支持Meteor 0.8.2 (与bcrypt)。
服务器端的示例
try {
if (ApiPassword.isPasswordValid(username, password)) {
console.log('password is valid for this user');
} else {
console.log('password is not valid');
}
} catch (exc) {
console.log(exc.message);
// possible causes: 'User is not found', 'User has no password set'
}发布于 2015-01-07 06:02:37
我发布了一个用于在Meteor 0.9.0+中编写REST的包,该包支持身份验证。它的目的是取代RestStop2 (已接受的答案),现在它已被废弃,并且具有类似的API:
https://github.com/krose72205/meteor-restivus
它受到RestStop2的启发,并使用铁路由器的服务器端路由构建。
更新:--我只想为任何发现这一点的人提供一个代码示例。这是来自GitHub自述的Restivus快速启动示例:
Items = new Mongo.Collection 'items'
if Meteor.isServer
# API must be configured and built after startup!
Meteor.startup ->
# Global API configuration
Restivus.configure
useAuth: true
prettyJson: true
# Generates: GET, POST, DELETE on /api/items and GET, PUT, DELETE on
# /api/items/:id for Items collection
Restivus.addCollection Items
# Generates: GET, POST on /api/users and GET, DELETE /api/users/:id for
# Meteor.users collection
Restivus.addCollection Meteor.users,
excludedEndpoints: ['deleteAll', 'put']
routeOptions:
authRequired: true
endpoints:
post:
authRequired: false
delete:
roleRequired: 'admin'
# Maps to: /api/posts/:id
Restivus.addRoute 'posts/:id', authRequired: true,
get: ->
post = Posts.findOne @urlParams.id
if post
status: 'success', data: post
else
statusCode: 404
body: status: 'fail', message: 'Post not found'
post:
roleRequired: ['author', 'admin']
action: ->
post = Posts.findOne @urlParams.id
if post
status: "success", data: post
else
statusCode: 400
body: status: "fail", message: "Unable to add post"
delete:
roleRequired: 'admin'
action: ->
if Posts.remove @urlParams.id
status: "success", data: message: "Item removed"
else
statusCode: 404
body: status: "fail", message: "Item not found"https://stackoverflow.com/questions/24597145
复制相似问题