forked from ONLINE-GAME-STORE/Online-game-store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (117 loc) · 3.66 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv/config");
// ℹ️ Connects to the database
require("./db");
// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");
const app = express();
// ℹ️ This function is getting exported from the config folder. It runs most pieces of middleware
require("./config")(app);
// default value for title local
const capitalized = require("./utils/capitalized");
const projectName = "IronGames";
app.locals.appTitle = projectName;
// import bcryptjs for password hashing
const bcrypt = require("bcryptjs");
// Session initializing and config
const session = require("express-session");
const MongoStore = require("connect-mongo");
app.use(
session({
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
resave: true,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
}),
})
);
// passport import and configuration
const User = require("./models/User");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
User.findById(id)
.then((user) => {
done(null, user);
})
.catch((err) => {
done(err);
});
});
passport.use(
new LocalStrategy((username, password, done) => {
// this logic will be executed when we log in
User.findOne({ username: username }).then((user) => {
if (user === null) {
// username is not correct
done(null, false, { message: "Wrong Credentials" });
} else {
// THIS WORKS ! THIS CHECKS FOR THE PASSWORD AGAINST THE HASH
if (bcrypt.compareSync(password, user.password)) {
done(null, user);
} else {
done(null, false, { message: "Wrong Credentials" });
}
}
});
})
);
// DEFINE THE NEW GITHUB STRATEGY
const GithubStrategy = require("passport-github").Strategy;
passport.use(
new GithubStrategy(
{
// HERE YOU NEED TO GO TO GITHUB AND AUTHORIZE YOUR APP
// careful what you put here everything should match
clientID: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
callbackURL: "https://irongames.cyclic.app/auth/github/callback",
},
(accessToken, refreshToken, profile, done) => {
console.log(profile);
User.findOne({
githubId: profile.id,
}).then((user) => {
if (user !== null) {
// pass the user to passport to serialize it
done(null, user);
} else {
User.create({
githubId: profile.id,
username: profile.username,
profilePicPath: profile._json.avatar_url,
githubLink: profile._json.html_url,
}).then((user) => {
done(null, user);
});
}
});
}
)
);
// Use express-sessions and passport to handle user's sessions
app.use(passport.initialize());
app.use(passport.session());
//End
// 👇 Start handling routes here
const index = require("./routes");
app.use("/", index);
const auth = require("./routes/auth");
app.use("/auth", auth);
const games = require("./routes/games");
app.use("/games", games);
const search = require("./routes/search");
app.use("/search", search);
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);
module.exports = app;