Features
- share items with multiple users
- creating account tokens with access to specific collections & items
- used for locking down public access to certain features.
- ability to add expiration for tokens
var mongoose = require("mongoose"),
step = require("step"),
Schema = mongoose.Schema,
ObjectId = Schema.Types.ObjectId;
var auth = require("auth").connect({
connection: mongoose.createConnection("mongodb://localhost/auth-test")
});
var Post = new Schema({
message: String
});
Post.plugin(auth.ownable);
step(
function() {
auth.signup({ email: "me@email.com", password: "password" }, this);
},
function(err, account) {
this.account = account;
var post = new Post({
message: "Hello World!"
});
account.own(post);
post.save(this);
},
function() {
Post.find(this.account.ownQuery(), this);
},
function(err, post) {
console.log(post.message);
}
);
auth API
auth auth.connect(options)
- options
connection
- mongodb connection
auth.Account.signup(account, onCreated)
creates a new user
auth.Account.login(credentals, onLogin)
Logs the user in with u/p, or a token
Example:
auth.Account.login({ token: tokenKey }, onLogin);
auth.Account.login({ email: "email", password: "password" }, onLogin);
Account API
account.getMainToken(callback)
returns the main access token with super privileges. No restrictions to collections & items.
user.getMainToken(function(null, token) {
console.log(token.key);
console.log(token.ttl);
console.log(token.scope);
})
account.createToken(options, callback)
options
- options for the token
item
- the item to grant access to (optional)collectionName
- the collectionttl
- time in MS for expirationaccess
- (array) scope access. default is access.all()
user.createToken({ item: Posts.collection.name, access: [access.POST] }, function(err, token) {
console.log(token.scope);
});
account.ownItem(item)
makes the account an owner of an item with SUPER privileges on item
var p = new Post({ message: "hello!" });
user.ownItem(p);
p.save();
account.shareItem(item, access)
Shares an item with another user
item
- item to ownaccess
- access level for the given item. Blank = ALL privileges.
var access = require("auth").access;
Post.findOne({message:"hello!"}, function(err, post) {
user2.shareItem(post, [access.GET]);
post.save();
});
account.authorized(item, access)
returns TRUE if the account has access to the item. Note that the result can be variable
depending if whether the given user logs in with a restricted login token. See below.
user2.authorized(post);
user2.authorized(post, [access.POST]);
user2.authorized(post, [access.GET]);
user2.authorized(post, [access.GET, access.POST]);
User.login({ token: aboveTokenKey }, function(err, user) {
user.authorized(post, [access.TRUE]);
user.authorized(post, [access.POST]);
})
Error account.unauthorized(callback)
Tiny flow-control utility.
account.addToSearch(query)
adds account to the given search. For example:
Post.findOne(user.addToSearch(), function(err, post) {
user.authorized(post);
})
## TODO
- make sub-schemas ownable
- sharing whole collections (job & timer)
- custom authentication schema
- validation of credentials (email/pass)
- Auth.lockdown - prevent models from being saved or serialized if unauthorized
- hooks with [passport](https: