-
-
Save Tanver-Hasan/f151eadace5690e221f6e7901651d245 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const express = require('express'); | |
const path = require('path'); | |
const favicon = require('serve-favicon'); | |
const logger = require('morgan'); | |
const cookieParser = require('cookie-parser'); | |
const bodyParser = require('body-parser'); | |
const session = require('express-session'); | |
const dotenv = require('dotenv'); | |
const passport = require('passport'); | |
const Auth0Strategy = require('passport-auth0'); | |
const flash = require('connect-flash'); | |
dotenv.load(); | |
const routes = require('./routes/index'); | |
const user = require('./routes/user'); | |
// This will configure Passport to use Auth0 | |
const strategy = new Auth0Strategy( | |
{ | |
domain: process.env.AUTH0_DOMAIN, | |
clientID: process.env.AUTH0_CLIENT_ID, | |
clientSecret: process.env.AUTH0_CLIENT_SECRET, | |
callbackURL: '/callback', | |
}, | |
function(accessToken, refreshToken, extraParams, profile, done) { | |
// accessToken is the token to call Auth0 API (not needed in the most cases) | |
// extraParams.id_token has the JSON Web Token | |
// profile has all the information from the user | |
console.log(JSON.stringify(profile)) | |
return done(null, profile); | |
} | |
); | |
passport.use(strategy); | |
// you can use this section to keep a smaller payload | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(user, done) { | |
done(null, user); | |
}); | |
const app = express(); | |
// view engine setup | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'pug'); | |
// uncomment after placing your favicon in /public | |
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use( | |
session({ | |
secret: process.env.SESSION_SECRET || "mostsecretsession123" , | |
resave: false, | |
saveUninitialized: true | |
}) | |
); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use(flash()); | |
// Handle auth failure error messages | |
app.use(function(req, res, next) { | |
if (req && req.query && req.query.error) { | |
req.flash("error", req.query.error); | |
} | |
if (req && req.query && req.query.error_description) { | |
req.flash("error_description", req.query.error_description); | |
} | |
next(); | |
}); | |
// Check logged in | |
app.use(function(req, res, next) { | |
res.locals.loggedIn = false; | |
if (req.session.passport && typeof req.session.passport.user != 'undefined') { | |
res.locals.loggedIn = true; | |
} | |
next(); | |
}); | |
app.use('/', routes); | |
app.use('/user', user); | |
// catch 404 and forward to error handler | |
app.use(function(req, res, next) { | |
const err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handlers | |
// development error handler | |
// will print stacktrace | |
if (app.get('env') === 'development') { | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
// production error handler | |
// no stacktraces leaked to user | |
app.use(function(err, req, res, next) { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
// module.exports = app; | |
app.listen(process.env.PORT || 3000); | |
console.log(`Express server @ http://localhost:${process.env.PORT || 3000}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment