public
Last active

Use everyauth authentication via JSON REST API only

  • Download Gist
User.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Promise = mongoose.Promise,
mongooseAuth = require('mongoose-auth'),
 
UserSchema = new Schema({}, { strict: true }),
User;
 
UserSchema.plugin(mongooseAuth, {
 
everymodule: {
everyauth: {
 
User: function () {
return User;
},
 
handleLogout: function (req, res) {
req.logout();
res.contentType('application/json');
res.send(JSON.stringify({ user: null }));
}
}
},
 
password: {
everyauth: (function () {
 
var registerPath = '/register',
loginPath = '/login';
 
function respondToGetMethod (req, res) {
respond(res, { errors: ['Unsupported HTTP method.'] });
}
 
function respondToSucceed (res, user) {
if (!user) return;
respond(res, { user: user });
}
 
function respondToFail (req, res, errors) {
if (!errors || !errors.length) return;
respond(res, { errors: errors });
}
 
function respond (res, output) {
res.contentType('application/json');
res.send(JSON.stringify(output));
}
 
return {
getRegisterPath: registerPath,
displayRegister: respondToGetMethod,
postRegisterPath: registerPath,
respondToRegistrationSucceed: respondToSucceed,
respondToRegistrationFail: respondToFail,
getLoginPath: loginPath,
displayLogin: respondToGetMethod,
postLoginPath: loginPath,
respondToLoginSucceed: respondToSucceed,
respondToLoginFail: respondToFail
};
 
})()
}
});
 
User = mongoose.model('User', UserSchema);
 
module.exports = UserSchema;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.