-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from leoc/master
Add callback to loginVisitor
- Loading branch information
Showing
1 changed file
with
55 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,65 @@ | ||
/***************** | ||
* special anonymous behavior so that visitors can | ||
* manipulate their work | ||
* | ||
*/ | ||
|
||
* special anonymous behavior so that visitors can | ||
* manipulate their work | ||
* | ||
*/ | ||
|
||
// If our app has a Blaze, override the {{currentUser}} helper to deal guest logins | ||
if (Package.blaze) { | ||
/** | ||
* @global | ||
* @name currentUser | ||
* @isHelper true | ||
* @summary Calls [Meteor.user()](#meteor_user). Use `{{#if currentUser}}` to check whether the user is logged in. | ||
* Where "logged in" means: The user has has authenticated (e.g. through providing credentials) | ||
*/ | ||
Package.blaze.Blaze.Template.registerHelper('currentUser', function () { | ||
var user = Meteor.user(); | ||
if (user && | ||
typeof user.profile !== 'undefined' && | ||
typeof user.profile.guest !== 'undefined' && | ||
user.profile.guest){ | ||
// a guest login is not a real login where the user is authenticated. | ||
// This allows the account-base "Sign-in" to still appear | ||
return null; | ||
}else{ | ||
return Meteor.user(); | ||
} | ||
}); | ||
/** | ||
* @global | ||
* @name currentUser | ||
* @isHelper true | ||
* @summary Calls [Meteor.user()](#meteor_user). Use `{{#if currentUser}}` to check whether the user is logged in. | ||
* Where "logged in" means: The user has has authenticated (e.g. through providing credentials) | ||
*/ | ||
Package.blaze.Blaze.Template.registerHelper('currentUser', function () { | ||
var user = Meteor.user(); | ||
if (user && | ||
typeof user.profile !== 'undefined' && | ||
typeof user.profile.guest !== 'undefined' && | ||
user.profile.guest){ | ||
// a guest login is not a real login where the user is authenticated. | ||
// This allows the account-base "Sign-in" to still appear | ||
return null; | ||
} else { | ||
return Meteor.user(); | ||
} | ||
}); | ||
} | ||
|
||
//no non-logged in users | ||
/* you might need to limit this to avoid flooding the user db */ | ||
Meteor.loginVisitor = function (email, callback) { | ||
AccountsGuest.forced = true; | ||
if (!Meteor.userId()) { | ||
Meteor.call('createGuest', email, function (error, result) { | ||
if (error) { | ||
console.log('Error in creating Guest ' + error); | ||
return callback && callback(error); | ||
} | ||
|
||
//no non-logged in users | ||
/* you might need to limit this to avoid flooding the user db */ | ||
Meteor.loginVisitor = function (email) { | ||
AccountsGuest.forced = true; | ||
if (!Meteor.userId()) { | ||
Meteor.call('createGuest', email, 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); | ||
return false; | ||
} | ||
}); | ||
return true; | ||
}); | ||
} | ||
} | ||
/* if a simple "true" is returned, we are in a disabled mode */ | ||
if(result === true) return callback && callback(); | ||
|
||
Meteor.startup(function(){ | ||
Deps.autorun(function () { | ||
Meteor.loginWithPassword(result.email, result.password, function(error) { | ||
if(error) { | ||
console.log('Error logging in ' + error); | ||
callback && callback(error); | ||
} else { | ||
callback && callback(); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
if (Meteor.userId()) { | ||
// console.log('this is '+Meteor.userId()); | ||
} else { | ||
if (AccountsGuest.forced === true){ | ||
Meteor.loginVisitor(); | ||
} | ||
} | ||
}); | ||
Meteor.startup(function(){ | ||
Deps.autorun(function () { | ||
if (!Meteor.userId()) { | ||
if (AccountsGuest.forced === true) { | ||
Meteor.loginVisitor(); | ||
} | ||
} | ||
}); | ||
}); |