-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
77 lines (65 loc) · 2.42 KB
/
app.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
import express from "express"; // Importing express for the web framework
import bodyParser from "body-parser"; // Importing bodyParser for parsing request bodies
import ejsLayouts from "express-ejs-layouts"; // Importing express-ejs-layouts for layout support
import path from "path"; // Importing express-ejs-layouts for layout support
import dotenv from "dotenv"; // Importing dotenv to load environment variables
import session from "express-session"; // Importing express-session for session management
import passport from "passport"; // Importing passport for authentication
import { Strategy as GoogleStrategy } from "passport-google-oauth20"; // Importing Google OAuth 2.0 strategy for passport
import { connectUsingMongoose } from "./config/mongodb.js"; // Importing MongoDB connection function
import router from "./routes/routes.js"; // Importing main application routes
import authrouter from "./routes/authRoutes.js"; // Importing authentication routes
dotenv.config(); // Loading environment variables from .env file
const app = express(); // Initializing express application
//SESSION
app.use(
session({
secret: "SecretKey",
resave: false,
saveUninitialized: true,
cookie: { secure: false },
})
);
//MIDDLEWARE
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Passport
app.use(passport.initialize());
app.use(passport.session());
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL:
"https://nodejs-authentication-system-l2pu.onrender.com/auth/google/callback",
scope: ["profile", "email"],
},
function (accessToken, refreshToken, profile, callback) {
callback(null, profile);
}
)
);
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
// Set Templates
app.set("view engine", "ejs"); // Define template engine
app.use(ejsLayouts); // Use base template
app.set("views", path.join(path.resolve(), "views")); // Define template directory
// DB Connection
connectUsingMongoose();
//ROUTES
app.get("/", (req, res) => {
res.send("Hey Ninja ! Go to /user/signin for the login page.");
});
app.use("/user", router);
app.use("/auth", authrouter);
app.use(express.static("public"));
//LISTEN
app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`);
});