Skip to content

Commit

Permalink
Complete the Website
Browse files Browse the repository at this point in the history
change secrets page ( now everyone can see others secrets anonymously)
  • Loading branch information
yctsai116 committed Jul 9, 2020
1 parent dda2281 commit c362bcb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
44 changes: 42 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ mongoose.set("useCreateIndex", true);
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId:String
googleId: String,
secret: String
});

userSchema.plugin(passportLocalMongoose);
Expand Down Expand Up @@ -102,8 +103,47 @@ app.get("/register", function(req, res) {
})

app.get("/secrets", function(req, res) {
User.find({
"secret": {
$ne: null
}
},
function(err, foundUsers) {
if (err) {
console.log(err);
} else {
if (foundUsers) {
res.render("secrets", {
usersWithSecrets: foundUsers
});
}
}
}
);
});

app.post("/submit", function(req, res) {
const submittedSecret = req.body.secret;

// console.log(req.user.id);

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");
})
}
}
})
})

app.get("/submit", function(req, res) {
if (req.isAuthenticated()) {
res.render("secrets");
res.render("submit");
} else {
res.redirect("/login");
}
Expand Down
14 changes: 9 additions & 5 deletions views/secrets.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
<div class="container">
<i class="fas fa-key fa-6x"></i>
<h1 class="display-3">You've Discovered My Secret!</h1>
<p class="secret-text">Jack Bauer is my hero.</p>
<hr>

<a class="btn btn-light btn-lg" href="/logout" role="button">Log Out</a>
<a class="btn btn-dark btn-lg" href="/submit" role="button">Submit a Secret</a>

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

<hr>

<a class="btn btn-light btn-lg" href="/logout" role="button">Log Out</a>
<a class="btn btn-dark btn-lg" href="/submit" role="button">Submit a Secret</a>
</div>
</div>

Expand Down

0 comments on commit c362bcb

Please sign in to comment.