Created
February 21, 2014 04:15
-
-
Save krishagel/9128626 to your computer and use it in GitHub Desktop.
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 path = require('path'), | |
express = require('express'), | |
http = require('http'), | |
mongoose = require('mongoose'), | |
passport = require('passport'), | |
passportLocalMongoose = require('passport-local-mongoose'), | |
BasicStrategy = require('passport-http').BasicStrategy; | |
var app = express(); | |
// Configuration | |
app.use(express.logger()); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(passport.initialize()); | |
app.use(app.router); | |
// Configure passport | |
var AccountSchema = new mongoose.Schema({ | |
nickname: String, | |
birthdate: Date | |
}); | |
AccountSchema.plugin(passportLocalMongoose); | |
var Account = mongoose.model('Account', AccountSchema); | |
passport.use(new BasicStrategy(Account.authenticate())); | |
passport.serializeUser(Account.serializeUser()); | |
passport.deserializeUser(Account.deserializeUser()); | |
// Connect mongoose | |
mongoose.connect('mongodb://localhost/passport_local_mongoose_examples'); | |
app.post('/register', function(req, res) { | |
Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) { | |
if (err) { | |
console.log(err) | |
} | |
console.log(account); | |
res.send(JSON.stringify({"message": "account created"})); | |
}); | |
}); | |
app.post('/login', passport.authenticate('basic', { session: false }), | |
function(req, res) { | |
res.send(JSON.stringify({"message": req.body.comment})); | |
}); | |
http.createServer(app).listen(3000, '127.0.0.1', function() { | |
console.log("Express server listening on %s:%d in %s mode", '127.0.0.1', 3000, app.settings.env); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment