Skip to content

Instantly share code, notes, and snippets.

@miyamotodev123
miyamotodev123 / auth_set_up_and_testing.config.database.js
Last active January 8, 2016 03:41
auth set up and testing config/database.js
// 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
};
// 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) {
// 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,
// app/routes.js
module.exports = function (app, passport) {
// LOGOUT ==============================
app.post('/logout', function(req, res) {
req.logout();
res.json({ redirect: '/logout' });
});
{
"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",
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
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
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 {
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
// 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
...