Last active
April 13, 2020 11:56
-
-
Save bastien227/a341bd4f3f77254badb75822ada0ab24 to your computer and use it in GitHub Desktop.
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
// index.js | |
/** | |
* Required External Modules | |
*/ | |
const express = require("express"); | |
const path = require("path"); | |
const expressSession = require("express-session"); | |
const passport = require("passport"); | |
const Auth0Strategy = require("passport-auth0"); | |
require("dotenv").config(); | |
const authRouter = require("./auth"); | |
/** | |
* App Variables | |
*/ | |
const app = express(); | |
const port = process.env.PORT || "8000"; | |
/** | |
* App Configuration | |
*/ | |
app.set("views", path.join(__dirname, "views")); | |
app.set("view engine", "pug"); | |
app.use(express.static(path.join(__dirname, "public"))); | |
app.use(expressSession(session)); | |
passport.use(strategy); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.serializeUser((user, done) => { | |
done(null, user); | |
}); | |
passport.deserializeUser((user, done) => { | |
done(null, user); | |
}); | |
// Creating custom middleware with Express | |
app.use((req, res, next) => { | |
res.locals.isAuthenticated = req.isAuthenticated(); | |
next(); | |
}); | |
// Router mounting | |
app.use("/", authRouter); | |
/** | |
* Routes Definitions | |
*/ | |
app.get("/", (req, res) => { | |
res.render("index", { title: "Home" }); | |
}); | |
app.get("/", (req, res) => { | |
res.status(200).send("TEST"); | |
}); | |
app.get("/user", (req, res) => { | |
res.render("user", { title: "Profile", userProfile: { nickname: "Auth0" } }); | |
}); | |
/** | |
* Server Activation | |
*/ | |
app.listen(port, () => { | |
console.log(`Listening to requests on http://localhost:${port}`); | |
}); | |
/** | |
* Session Configuration | |
*/ | |
const session = { | |
secret: "LoxodontaElephasMammuthusPalaeoloxodonPrimelephas", | |
cookie: {}, | |
resave: false, | |
saveUninitialized: false | |
}; | |
if (app.get("env") === "production") { | |
// Serve secure cookies, requires HTTPS | |
session.cookie.secure = true; | |
} | |
/** | |
* Passport Configuration | |
*/ | |
const strategy = new Auth0Strategy( | |
{ | |
domain: process.env.AUTH0_DOMAIN, | |
clientID: process.env.AUTH0_CLIENT_ID, | |
clientSecret: process.env.AUTH0_CLIENT_SECRET, | |
callbackURL: | |
process.env.AUTH0_CALLBACK_URL || "http://localhost:3000/callback" | |
}, | |
function(accessToken, refreshToken, extraParams, profile, done) { | |
/** | |
* Access tokens are used to authorize users to an API | |
* (resource server) | |
* accessToken is the token to call the Auth0 API | |
* or a secured third-party API | |
* extraParams.id_token has the JSON Web Token | |
* profile has all the information from the user | |
*/ | |
return done(null, profile); | |
} | |
); |
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": "Control_Review_App", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "nodemon ./index.js", | |
"ui": "browser-sync start --port 3000 --proxy=localhost:8000 --files='**/*.css, **/*.pug, **/*.js' --ignore=node_modules --reload-delay 10 --no-ui --no-notify" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"browser-sync": "^2.26.7", | |
"browsersync": "0.0.1-security", | |
"nodemon": "^2.0.3" | |
}, | |
"dependencies": { | |
"body-parser": "^1.19.0", | |
"dotenv": "^8.2.0", | |
"express": "^4.17.1", | |
"express-session": "^1.17.0", | |
"passport": "^0.4.1", | |
"passport-auth0": "^1.3.2", | |
"path": "^0.12.7", | |
"pug": "^2.0.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment