Skip to content

Instantly share code, notes, and snippets.

@solace
Last active February 20, 2016 06:47
Show Gist options
  • Save solace/24b874294bd7265244c9 to your computer and use it in GitHub Desktop.
Save solace/24b874294bd7265244c9 to your computer and use it in GitHub Desktop.
splendido:accounts-meld with mantra-sample-blog-app
/**
* Get `splendido:accounts-meld` working with the Mantra sample Blog App
*
* Create a file that looks something like this in your `/server`
*
* Caveat: No guarantees, negligible support, YMMV. If you have a better way, let me know.
*/
import {Meteor} from 'meteor/meteor';
import _ from 'lodash';
/**
* AccountsMeld config
*
* Add your config as you would normally.
*/
AccountsMeld.configure({
// ...
});
/**
* `checkForMelds` doesn't seem accessible. Created a replacement that also works with Astronomy which adds a bunch of
* helper attributes and functions which cause issues during the `queryFields` filter section (see comments later).
*
* Only relevant bits have been included here for annotation purposes, but basically take a copy of the original function
* and make the changes prefixed with `ADD` in the comments.
*/
export const doMeld = function(dstUser) {
/**
* ADD: Astronomy by default adds a transform to collection queries. Refetch the user object without the transform so
* that it is usable as a plain JSON object. Add this to the top of the function.
*/
dstUser = Meteor.users.findOne({_id: dstUser._id}, { transform: null });
// ...
// Picks up verified email addresses and creates a list like
// [
// {$elemMatch: {"address": addr1, "verified": true}},
// {$elemMatch: {"address": addr2, "verified": true}},
// ...
// ]
// That bit above is one of splendido's annotations. With Astronomy, the `email` part on line 56 is also going to have
// all of Astronomy's helper fields. So your query will always return empty. That's why you need the `findOne` without
// a transform.
var queryEmails = _.chain(dstUser.registered_emails)
.filter(function(email) {
return email.verified;
})
.map(function(email) {
// Works around Astronomy's transform
return {
"registered_emails": {
$elemMatch: email
}
};
})
.value();
// ...
/**
* ADD: Update the query to remove the transform here as well. Otherwise when the objects are used later you'll
* end up with Astronomy's helper fields in mongo as well.
*/
var users = Meteor.users.find(queryEmails, { transform: null });
// ...
};
/**
* **Note:** If you have an `Accounts.onLogin` somewhere else, you can include this in there, or just leave this here.
* I don't remember if this is one of the events you can attach callbacks to more than once.
*
* If you put this in another file, you may have to add:
* `import {doMeld} from './path/to/the/above/file';
*/
Accounts.onLogin(function(attempt) {
/**
* This event fires not just on login but on page reload. Leave the `attempt.type` check if you want to limit melds to
* just when an actual login occurs.
*/
if (attempt.type !== 'resume') {
doMeld(attempt.user);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment