Skip to content

Commit

Permalink
debugging options, README examples for options
Browse files Browse the repository at this point in the history
  • Loading branch information
artwells committed Jan 14, 2015
1 parent 14df1e8 commit e2a32e3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ meteor add accounts-guest
```
and you are done!

If you've changed AccountsGuest.forced to false, in client-only code add0

```javascript
Meteor.loginVisitor()
```


optionally (to clean out old guest accounts) in server-only code
Expand All @@ -30,14 +26,6 @@ Accounts.removeOldGuests([time before]);

Now Meteor.userId() will be populated for each new visitor, including across reloads




##Options

* `AccountsGuest.enabled`, default true. enables "Meteor.loginVisitor()".
* `AccountsGuest.forced`, default true. Does not require "Meteor.loginVisitor()". Will force recently logged out accounts into guest mode.

##Examples

```javascript
Expand All @@ -53,6 +41,28 @@ before.setHours(before.getHours() - 2);
Accounts.removeOldGuests(before);
```



##Options

* `AccountsGuest.enabled`, default true. Automatically logs in all visitors.
* `AccountsGuest.forced`, default true. Will force recently logged out accounts into guest mode.

##Option Examples

In code available to server, to temporarily or conditionally disable guest login
```javascript
AccountsGuest.enabled = false
```

In code available to client, to temporarily or conditionally disable guest login after user logout
```javascript
AccountsGuest.enabled = true
```




##TODO
- tests for forced, and enabled options
- Allow guest session merged into new session if a visitor logs in
Expand Down
41 changes: 41 additions & 0 deletions accounts-guest-client-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,44 @@ Tinytest.add(
test.isTrue((typeof uid !== undefined && typeof uid !== null), 'no userId returned');
}
);
/*
Tinytest.addAsync(
'guest - guest after logout',
function (test,next) {
Meteor.loginWithPassword(AccountsGuest.testname, AccountsGuest.testpass, function(err){
if(err){
console.log('Error logging in '+err);
return false;
}
});
userid = Meteor.userId();
test.isTrue(typeof userid !== undefined, 'no userId returned');
test.isTrue(userid != 'null', 'no userId returned');
Meteor.logout(function(err){
if(err){
console.log('Error logging out '+err);
return false;
}
seconduid = Meteor.userId();
test.isNotNull(seconduid, 'uid null');
test.notEqual(userid, seconduid, 'new uid issued');
console.log(seconduid + uid +'uuuu');
});
next();
});
*/



Tinytest.add(
'guest - test disable',
function (test) {
AccountsGuest.enabled = false;
res = Meteor.call("createGuest");
uid = Meteor.userId();
test.isTrue((typeof uid !== undefined && typeof uid !== null), 'working');
}
);
18 changes: 10 additions & 8 deletions accounts-guest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ Meteor.user = function () {
//no non-logged in users
/* you might need to limit this to avoid flooding the user db */
Meteor.loginVisitor = function () {
/* if explicitly disabled, happily do nothing */
if (AccountsGuest.enabled === false){
return true;
}
AccountsGuest.forced = true;
if (!Meteor.userId()) {
Meteor.call('createGuest',function (error, result) {
if (error) {
console.log('Error in creating Guest ' + error);
return false;
}
/* if a simple "true" is returned, we are in a disabled mode */
if(result === true){
return true;
}
Meteor.loginWithPassword(result.email, result.password, function(err){
if(err){
console.log('Error logging in '+err);
Expand All @@ -45,13 +46,14 @@ Meteor.user = function () {
});
}
}
if (AccountsGuest.forced === true){

Deps.autorun(function () {

if (Meteor.userId()) {
// console.log('this is '+Meteor.userId());
} else {
Meteor.loginVisitor();
if (AccountsGuest.forced === true){
Meteor.loginVisitor();
}
}

});
}
6 changes: 5 additions & 1 deletion accounts-guest-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Accounts.removeOldGuests = function (before) {
Meteor.methods({
createGuest: function ()
{
/* if explicitly disabled, happily do nothing */
if (AccountsGuest.enabled === false){
return true;
}
count = Meteor.users.find().count() + 1
guestname = "guest-#" + count
guest = {
Expand All @@ -22,7 +26,7 @@ Meteor.methods({
password: Meteor.uuid(),
};
Accounts.createUser(guest);
console.log("createGuest" + guestname);
// console.log("createGuest" + guestname);
return guest;
}
});
2 changes: 1 addition & 1 deletion accounts-guest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ AccountsGuest = {};
if (typeof AccountsGuest.forced === "undefined") {
AccountsGuest.forced = true; /*default to making loginVisitor automatic, and on logout*/
}
if (typeof AccountsGuest.forced === "undefined") {
if (typeof AccountsGuest.enabled === "undefined") {
AccountsGuest.enabled = true; /* on 'false' Meteor.loginVisitor() will fail */
}
11 changes: 7 additions & 4 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ Package.onUse(function (api) {
api.use(['accounts-base', 'accounts-password','deps'], 'client');
api.use(['accounts-base'], 'server');
api.add_files('accounts-guest.js', ['client','server']);
api.add_files(['accounts-guest-server.js','accounts-guest.js'], 'server');
api.export('AccountsGuest');
api.add_files('accounts-guest-server.js', 'server');
api.add_files('accounts-guest-client.js', 'client');

});

Package.onTest(function (api) {
api.versionsFrom("METEOR@0.9.0");
api.use(['accounts-base', 'accounts-password', 'tinytest'], ['client','server']);
api.add_files(['accounts-guest-client.js','accounts-guest.js'], ['client','server']);
api.use(['accounts-base', 'accounts-password', 'tinytest','deps'], ['client','server']);
api.add_files('accounts-guest.js', ['client','server']);
api.add_files('accounts-guest-server.js', 'server');
api.add_files('accounts-guest-client-tests.js', 'client');
api.add_files('accounts-guest-client.js', 'client');
api.add_files('accounts-guest-server-tests.js', 'server');
api.add_files('accounts-guest-client-tests.js', 'client');
});

0 comments on commit e2a32e3

Please sign in to comment.