-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separating express auth and passport auth
- Loading branch information
Showing
45 changed files
with
2,282 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
**/node_modules | ||
.DS_Store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.DS_Store |
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
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; |
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
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.
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
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); |
Oops, something went wrong.