Get config variables in auth0 in env file

This commit is contained in:
2021-10-04 09:59:29 -05:00
parent 737be09b5f
commit cc44c409d7
10 changed files with 58 additions and 76 deletions

View File

@@ -1,17 +1,10 @@
const express = require("express");
const app = express();
const passport = require("passport");
const jwtAuthz = require("express-jwt-authz");
const asyncHandler = require("express-async-handler");
const { users } = require("./controllers");
const secure = require("./middleware/secure");
/**
* Configure Passport
*/
try {
require("./config/passport")(passport);
} catch (error) {
console.log(error);
}
const app = express();
/**
* Configure Express.js Middleware
@@ -25,19 +18,9 @@ app.use(function (req, res, next) {
next();
});
// Initialize Passport and restore authentication state, if any, from the session
app.use(passport.initialize());
app.use(passport.session());
// Enable JSON use
app.use(express.json());
// Since Express doesn't support error handling of promises out of the box,
// this handler enables that
const asyncHandler = (fn) => (req, res, next) => {
return Promise.resolve(fn(req, res, next)).catch(next);
};
/**
* Routes - Public
*/
@@ -46,10 +29,6 @@ app.options(`*`, (req, res) => {
res.status(200).send();
});
app.post(`/users/register`, asyncHandler(users.register));
app.post(`/users/login`, asyncHandler(users.login));
app.get(`/test/`, (req, res) => {
res.status(200).send("Request received");
});
@@ -58,10 +37,13 @@ app.get(`/test/`, (req, res) => {
* Routes - Protected
*/
app.post(
`/user`,
passport.authenticate("jwt", { session: false }),
asyncHandler(users.get)
app.get(
"/test-authorized/",
secure,
jwtAuthz(["read:stuff"]),
function (req, res) {
res.send("Secured Resource");
}
);
/**
@@ -69,7 +51,7 @@ app.post(
*/
app.get(`/*`, (req, res) => {
res.status(404).send("Route not found");
res.status(404).json({ error: "Not Found" });
});
/**