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
// config/database.js | |
module.exports = { | |
'url' : 'your-settings-here' // mine looks like 'mongodb://localhost:27017/auth_set_up_and_testing' | |
// create whatever name you want for your database | |
// e.g. mongodb://localhost:27017/whatever_name_i_want | |
}; |
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
// config/passport.js | |
// load all the things we need | |
var LocalStrategy = require('passport-local').Strategy; | |
// load up the user model | |
var User = require('../app/models/user'); | |
// expose this function to our app using module.exports | |
module.exports = function(passport) { |
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
// app/models/user.js | |
// load the things we need | |
var mongoose = require('mongoose'); | |
var bcrypt = require('bcrypt-nodejs'); | |
// define the schema for our user model | |
var userSchema = mongoose.Schema({ | |
local : { | |
email : String, | |
password : String, |
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
// app/routes.js | |
module.exports = function (app, passport) { | |
// LOGOUT ============================== | |
app.post('/logout', function(req, res) { | |
req.logout(); | |
res.json({ redirect: '/logout' }); | |
}); |
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
{ | |
"name": "authentication-set-up-and-testing", | |
"main": "server.js", | |
"dependencies" : { | |
"express" : "~4.8.8", | |
"mongoose" : "~3.8.1", | |
"passport" : "~0.1.17", | |
"passport-local": "~1.0.0", | |
"bcrypt-nodejs" : "latest", | |
"morgan": "~1.3.0", |
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 app = require('../../server.js'), | |
should = require('should'), | |
request = require('supertest'), | |
mongoose = require('mongoose'), | |
User = mongoose.model('User'), | |
api = request.agent(app); | |
describe('testing user authentication endpoints', function () { | |
// clear users out from the test database |
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 app = require('../../server.js'), | |
should = require('should'), | |
request = require('supertest'), | |
api = request(app); | |
describe('testing user authentication endpoints', function () { | |
it('successfully created a user', function (done) { | |
// make a post reqeust to the /signup route | |
api.post('/signup') | |
// send an email and password as part of the request body |
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
User.findOne({ 'local.email' : email }, function(err, user) { | |
// if there are any errors, return the error | |
if (err) | |
return done(err); | |
// check to see if theres already a user with that email | |
if (user) { | |
return done(null, { error: 'That email is already taken.' }); | |
} else { |
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 app = require('../../server.js'), | |
should = require('should'), | |
request = require('supertest'), | |
mongoose = require('mongoose'), | |
User = mongoose.model('User'), | |
api = request(app); | |
describe('testing user authentication endpoints', function () { | |
// clear users out from the test database |
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
// server.js | |
... | |
var session = require('express-session'); | |
var path = require('path'); // add path into our required list | |
... | |
app.use(passport.session()); // persistent login sessions | |
app.use(express.static(path.join(__dirname, '/public'))); //Expose /public | |
... |