Skip to content

Commit

Permalink
Separating express auth and passport auth
Browse files Browse the repository at this point in the history
  • Loading branch information
richa008 committed Dec 7, 2019
1 parent aa9fac6 commit b1bf81a
Show file tree
Hide file tree
Showing 45 changed files with 2,282 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
**/node_modules
.DS_Store
2 changes: 2 additions & 0 deletions Express-SessionAuth/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
91 changes: 91 additions & 0 deletions Express-SessionAuth/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
const passport = require('passport');
const authenticate = require('./authenticate');

const index = require('./routes/index');
const userRouter = require('./routes/userRouter');
const dishRouter = require('./routes/dishRouter');
const promoRouter = require('./routes/promoRouter');
const leaderRouter = require('./routes/leaderRouter');

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

// Connection URL
const url = 'mongodb://localhost:27017/conFusion';
const connect = mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
});

connect.then((db) => {
console.log("Connected correctly to server");
}, (err) => { console.log(err); });

var app = express();

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
name: "session-id",
secret: "12345-67890-09876-54321",
saveUninitialized: false,
resave: false,
store: new FileStore()
}));
app.use(passport.initialize());
app.use(passport.session()); // Required for persistent login sessions
app.use(express.static(path.join(__dirname, 'public'))); // serves static data from public folder

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

function auth(req, res, next) {
console.log(req.user);

if (!req.user) {
var err = new Error('You are not authenticated!');
err.status = 403;
next(err);
}
else {
next();
}
}

app.use('/', index);
app.use('/users', userRouter);

app.use(auth);
app.use('/dishes', dishRouter);
app.use('/promotions', promoRouter);
app.use('/leaders', leaderRouter);

// catch 404 and forward to error handler
app.use(function (request, response, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function (err, request, response, next) {
// set locals, only providing error in development
response.locals.message = err.message;
response.locals.error = request.app.get('env') === 'development' ? err : {};

// render the error page
response.status(err.status || 500);
response.render('error');
});

module.exports = app;
7 changes: 7 additions & 0 deletions Express-SessionAuth/authenticate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const User = require('./models/user');

passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions Express-SessionAuth/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');

const User = new Schema({
admin: {
type: Boolean,
default: false
}
});
User.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', User);
Loading

0 comments on commit b1bf81a

Please sign in to comment.