Created
June 15, 2012 20:14
-
-
Save furf/2938492 to your computer and use it in GitHub Desktop.
Use everyauth authentication via JSON REST API only
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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment