-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (92 loc) · 3.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require("dotenv").config();
const path = require("path");
const express = require("express");
const { config, engine } = require("express-edge");
const edge = require("edge.js");
const mongoose = require("mongoose");
const cors = require("cors");
const bodyParser = require("body-parser");
const Post = require("./database/models/Post");
const fileUpload = require("express-fileupload");
const session = require("express-session");
const { ExpressOIDC } = require("@okta/oidc-middleware");
//Declare Controllers
const getWhyController = require("./controllers/whyPage")
const createPostController = require("./controllers/createPost");
const homePageController = require("./controllers/homePage");
const storePostController = require("./controllers/storePost");
const getPostController = require("./controllers/getPost");
const getAdminController = require("./controllers/getAdmin");
const handlePostController = require('./controllers/handlePost');
const editPostController = require('./controllers/editPost');
const submitEditController = require('./controllers/submitEdit');
const app = express();
const port = 3000;
app.use(
session({
secret: process.env.RANDOM_SECRET_WORD,
resave: true,
saveUninitialized: false
})
);
const oidc = new ExpressOIDC({
issuer: `${process.env.OKTA_ORG_URL}/oauth2/default`,
client_id: process.env.OKTA_CLIENT_ID,
client_secret: process.env.OKTA_CLIENT_SECRET,
appBaseUrl: 'http://localhost:3000',
scope: "openid profile",
routes:{
logoutCallback: {
}
}
});
mongoose
.connect("mongodb://localhost:27017/node-blog", {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => "You are now connected to Mongo!")
.catch(err => console.error("Something went wrong", err));
app.use(oidc.router);
app.use(cors());
app.use(fileUpload());
app.use(express.static(__dirname + "/public/"));
app.use(engine);
app.set("views", `${__dirname}/views`);
app.use((req, res, next) => {
const result = req.userContext;
edge.global('auth', result);
next()
});
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
/* If I need to count documents
app.use((req, res, next) => {
Post.countDocuments({}, function(err, count){
console.log( "Number of docs: ", count );
});
*/
const storePost = require("./middleware/storePost");
app.use("/posts/store", storePost);
// Database
const connection = mongoose.connection;
// Controllers
app.get("/", homePageController);
app.get("/post/:id", getPostController);
app.get("/why", getWhyController);
app.get("/posts/new", oidc.ensureAuthenticated(), createPostController);
app.post("/posts/store", oidc.ensureAuthenticated(), storePostController);
app.get("/admin", oidc.ensureAuthenticated(), getAdminController);
app.post("/post/handle/", oidc.ensureAuthenticated(), handlePostController);
app.get("/post/handle/edit", oidc.ensureAuthenticated(), editPostController);
app.post("/post/handle/submit/edit", oidc.ensureAuthenticated(), submitEditController);
app.get("/home"), function(req,res){
res.redirect('/');
};
oidc.on("ready", () => {
app.listen(port, () => console.log(`My Blog App listening on port ${port}!`));
});