Created
February 5, 2015 21:45
-
-
Save adamrneary/e95f4d82fcbdde357766 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // This post-save hook middleware allows us to publish create and update events | |
| // to which the mediator service can subscribe. | |
| UserSchema.post('save', function (doc) { | |
| // Determine the current event | |
| var eventName = (this.wasNew) ? "user:create" : "user:update"; | |
| if (doc.deleted) {eventName = 'user:delete'} | |
| // Create an authenticate a Firebase ref and update accordingly | |
| var firebaseRef, userRef; | |
| if ((process.env.FIREBASE_URL != null) && (process.env.FIREBASE_SECRET != null)) { | |
| firebaseRef = new Firebase(process.env.FIREBASE_URL); | |
| firebaseRef.authWithCustomToken(process.env.FIREBASE_SECRET, function(error, authData) { | |
| var userRef; | |
| if (error) { | |
| console.log("Firebase login Failed!", error); | |
| } else { | |
| userRef = firebaseRef.child("users").child(doc.id); | |
| if (eventName === 'user:delete') { | |
| userRef.remove(); | |
| } else { | |
| userRef.set(doc.toCompleteJSON()); | |
| } | |
| } | |
| }); | |
| } | |
| JobService.publishEvent( | |
| eventName, | |
| {user_id: doc.id, hasChanged: this.hasChanged}, | |
| function (err) { | |
| if(err) console.log(err); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment