Skip to content

Instantly share code, notes, and snippets.

@epsi95
Last active August 6, 2020 17:35
Show Gist options
  • Save epsi95/72303458a7fe36f305a445d7119b711c to your computer and use it in GitHub Desktop.
Save epsi95/72303458a7fe36f305a445d7119b711c to your computer and use it in GitHub Desktop.
<!-- register.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="/register" method="post">
<input type="email" name="email" required>
<input type="password" name="password" required>
<button type="submit">Register</button>
</form>
</body>
</html>
<!-- secret.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secret</title>
</head>
<body>
<h1>Cats are cool 😍</h1>
</body>
</html>
// server.js
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
mongoose.connect("mongodb://localhost:27017/userDB", {
useNewUrlParser: true,
useUnifiedTopology: true
});
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
});
const User = mongoose.model("User", UserSchema);
// urlencoded parser is used since the html form send data
// as urlencoded format with POST reqiest
app.use(bodyParser.urlencoded({ extended: true }));
// define the register GET route
app.get("/register", function(req, res) {
res.sendFile(__dirname + "/register.html");
});
// define the login GET route
app.get("/login", function(req, res) {
res.sendFile(__dirname + "/signin.html");
});
// define the register POST route
app.post("/register", function(req, res) {
const email = req.body.email;
const password = req.body.password;
const newUser = User({ email: email, password: password });
newUser.save(function(err) {
if (!err) {
res.redirect("/login");
} else {
res.send(err);
}
})
});
// define the login POST route
app.post("/login", function(req, res) {
const email = req.body.email;
const password = req.body.password;
User.findOne({ email: email }, function(err, user) {
if (err) {
res.send(err);
} else if (user) {
if (password === user.password) {
res.sendFile(__dirname + "/secret.html");
} else {
res.redirect("/login")
}
} else {
res.redirect("/login");
}
});
});
///////////////////This is out secret page/////////////////////
// define the secret route
app.get("/secret", function(req, res) {
res.sendFile(__dirname + "/secret.html");
});
app.listen(3000, function() {
console.log(`Server started at port ${3000}`);
});
<!-- signin.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signin</title>
</head>
<body>
<h1>Sign in</h1>
<form action="/login" method="post">
<input type="email" name="email" required>
<input type="password" name="password" required>
<button type="submit">Signin</button>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment