Last active
August 6, 2020 17:35
-
-
Save epsi95/72303458a7fe36f305a445d7119b711c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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