-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
229 lines (204 loc) · 5.54 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
import "dotenv/config";
import bcrypt from "bcrypt";
import session from "express-session";
import passport from "passport";
import { Strategy } from "passport-local";
import GoogleStrategy from "passport-google-oauth20";
const db = new pg.Client({
user: process.env.DB_USER,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
host: process.env.DB_HOST,
});
db.connect();
const app = express();
const port = 3000;
const saltRounds = 10;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(
session({
secret: process.env.SECRET_SESSION,
resave: false,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.get("/", (req, res) => {
res.render("home.ejs");
});
app.get("/secrets", async (req, res) => {
if (req.isAuthenticated()) {
console.log("user:--->");
console.log(req.user);
console.log("User is authenticated!");
try {
const result = await db.query("SELECT * FROM users WHERE email = $1", [req.user.email]);
const secrets = result.rows[0].secrets;
console.log(secrets);
if(secrets){
res.render("secrets.ejs", { secrets: secrets });
}else{
res.render("secrets.ejs", { secrets: "Jack Baur is my hero"});
}
} catch (error) {
console.log(error);
}
} else {
res.redirect("/login");
}
});
app.get("/login", (req, res) => {
res.render("login.ejs");
});
app.get("/register", (req, res) => {
res.render("register.ejs");
});
app.get("/logout", (req, res) => {
req.logout((err) => {
if (err) console.log(err);
res.redirect("/");
});
});
app.get("/submit", (req, res) => {
console.log(req.user);
if (req.isAuthenticated()) {
res.render("submit.ejs");
} else {
res.redirect("/login");
}
});
app.get(
"/auth/google",
passport.authenticate("google", {
scope: ["profile", "email"],
})
);
app.get(
"/auth/google/secrets",
passport.authenticate("google", {
successRedirect: "/secrets",
failureRedirect: "/login",
})
);
app.post("/submit", async (req, res) => {
const secret = req.body.secret;
const email = req.user.email;
console.log(email);
try {
const newSecret = await db.query("UPDATE users SET secrets = $1 WHERE email = $2", [
secret,
email,
]);
res.redirect("/secrets");
} catch (error) {
console.log(error);
}
});
app.post("/register", async (req, res) => {
const email = req.body.username;
const password = req.body.password;
try {
const checkEmail = await db.query("SELECT * FROM users WHERE email=$1", [email]);
if (checkEmail.rowCount > 0) {
res.render("register.ejs");
} else {
bcrypt.hash(password, saltRounds, async (err, hash) => {
if (err) {
res.send("Error on hashing the password!");
} else {
const result = await db.query(
"INSERT INTO users (email, password) VALUES ($1, $2) RETURNING email",
[email, hash]
);
console.log("Successfully Added new user to DATABASE!");
const user = result.rows[0];
req.login(user, (err) => {
console.log("Sucess");
res.redirect("/secrets");
});
}
});
}
} catch (error) {
console.log(error.message);
res.render("register.ejs");
}
});
app.post(
"/login",
passport.authenticate("local", { successRedirect: "/secrets", failureRedirect: "/login" })
);
passport.use(
"local",
new Strategy(async function verify(username, password, cb) {
try {
const result = await db.query("SELECT * FROM users where email=$1", [username]);
if (result.rowCount === 0) {
res.render("login.ejs", { message: "Email is not registered. Please Register first." });
} else {
const storedHashedPassword = result.rows[0].password;
bcrypt.compare(password, storedHashedPassword, (err, result) => {
if (err) {
return cb(err);
} else {
if (!result) {
return cb(null, false);
} else {
console.log("Sucessfully Logged In!");
return cb(null, username);
}
}
});
}
} catch (error) {
return cb("user not found");
}
})
);
passport.use(
"google",
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secrets",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
},
async (accessToken, refreshToken, profile, cb) => {
console.log(profile);
console.log(profile.emails[0].value);
try {
const result = await db.query("SELECT * FROM users WHERE email = $1", [
profile.emails[0].value,
]);
if (result.rows.length === 0) {
const newUser = await db.query("INSERT INTO users (email, password) VALUES ($1, $2)", [
profile.emails[0].value,
"google",
]);
return cb(null, newUser.rows[0]);
} else {
return cb(null, result.rows[0]);
}
} catch (error) {
return cb(error);
}
}
)
);
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((user, cb) => {
cb(null, user);
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
console.log(`http://localhost:${port}`);
});