Skip to content

Commit

Permalink
Merge pull request #128 from Yash-Ainapure/patch5
Browse files Browse the repository at this point in the history
fixed secrets overwrite each other problem
  • Loading branch information
Kritika30032002 authored Dec 19, 2023
2 parents 67681fa + 453c2c5 commit c3f71b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
64 changes: 35 additions & 29 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ const userSchema = new mongoose.Schema({
password: String,
googleId: String,
facebookId: String,
secret: String,
secret: [String],
});

userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);

const User = mongoose.model("User", userSchema);
const User = mongoose.model("User", userSchema);

passport.use(User.createStrategy());

passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
Expand All @@ -78,15 +78,15 @@ passport.use(
clientSecret: process.env.FACEBOOK_APP_SECRET,
callbackURL: `${process.env.PUBLIC_BASENAME}auth/google/secrets`,
},
function(accessToken, refreshToken, profile, cb) {
User.findOrCreate({ facebookId: profile.id }, function(err, user) {
function (accessToken, refreshToken, profile, cb) {
User.findOrCreate({ facebookId: profile.id }, function (err, user) {
return cb(err, user);
});
}
)
);

app.get("/", function(req, res) {
app.get("/", function (req, res) {
res.render("home");
});

Expand All @@ -98,7 +98,7 @@ app.get(
app.get(
"/auth/google/secrets",
passport.authenticate("google", { failureRedirect: "/login" }),
function(req, res) {
function (req, res) {
res.redirect("/secrets");
}
);
Expand All @@ -108,37 +108,37 @@ app.get("/auth/facebook", passport.authenticate("facebook"));
app.get(
"/auth/facebook/secrets",
passport.authenticate("facebook", { failureRedirect: "/login" }),
function(req, res) {
function (req, res) {
res.redirect("/secrets");
}
);

app.get("/login", function(req, res) {
app.get("/login", function (req, res) {
res.render("login");
});

app.get("/about", function(req, res) {
app.get("/about", function (req, res) {
res.render("about");
});

app.get("/contact", function(req, res) {
app.get("/contact", function (req, res) {
res.render("contact.ejs");
});

app.get("/register", function(req, res) {
app.get("/register", function (req, res) {
res.render("register");
});

app.post("/register", function(req, res) {
app.post("/register", function (req, res) {
User.register(
{ username: req.body.username },
req.body.password,
function(err, user) {
function (err, user) {
if (err) {
console.log(err);
res.send({ success: false, message: err.message });
} else {
passport.authenticate("local")(req, res, function() {
passport.authenticate("local")(req, res, function () {
res.send({
success: true,
message: "Registration Successful, Login to continue",
Expand All @@ -149,8 +149,8 @@ app.post("/register", function(req, res) {
);
});

app.post("/login", function(req, res, next) {
passport.authenticate("local", function(err, user, info) {
app.post("/login", function (req, res, next) {
passport.authenticate("local", function (err, user, info) {
// Check for errors during authentication
if (err) {
return next(err);
Expand All @@ -161,7 +161,7 @@ app.post("/login", function(req, res, next) {
res.send({ success: false, message: info.message });
}
// If authentication succeeded, log in the user
req.logIn(user, function(err) {
req.logIn(user, function (err) {
if (err) {
return next(err);
}
Expand All @@ -171,8 +171,8 @@ app.post("/login", function(req, res, next) {
})(req, res, next);
});

app.get("/secrets", function(req, res) {
User.find({ secret: { $ne: null } }, function(err, foundUsers) {
app.get("/secrets", function (req, res) {
User.find({ secret: { $ne: null } }, function (err, foundUsers) {
if (err) {
console.log(err);
} else {
Expand All @@ -183,31 +183,37 @@ app.get("/secrets", function(req, res) {
});
});

app.get("/submit-secret-form", function(req, res) {
app.get("/submit-secret-form", function (req, res) {
if (req.isAuthenticated()) {
res.render("secret-form");
} else {
res.redirect("/login");
}
});

app.post("/submit-secret-form", function(req, res) {
app.post("/submit-secret-form", function (req, res) {
const submittedSecret = req.body.secret;
User.findById(req.user.id, function(err, foundUser) {
User.findById(req.user.id, function (err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
foundUser.secret = submittedSecret;
foundUser.save(function() {
res.redirect("/secrets");

foundUser.secret.push(submittedSecret);

foundUser.save(function (err) {
if (err) {
console.log(err)
} else {
res.redirect("/secrets");
}
});
}
}
});
});

app.get("/logout", function(req, res) {
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
Expand Down
5 changes: 4 additions & 1 deletion views/secrets.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
<h1 class="display-3">You've Discovered My Secret!</h1>

<% usersWithSecrets.forEach(function(user){ %>
<p class="secret-text"><%=user.secret%></p>
<% user.secret.forEach(function(secret){ %>
<p class="secret-text"><%= secret %></p>
<% }) %>
<% }) %>


<hr />
<div class="d-flex justify-content-center">
Expand Down

0 comments on commit c3f71b9

Please sign in to comment.