-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
104 lines (100 loc) · 2.67 KB
/
login.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
// this uses /login to authorize a user
var express = require("express");
var app = express();
const fetch = require("node-fetch");
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({}));
const authenticate = (req, res, next) => {
const authHeader = req.headers.authorization;
if (authHeader) {
if (authHeader == "passwordname") {
req.user = "name";
next();
}
} else {
res.sendStatus(401);
}
};
const authenticate2 = (req, res, next) => {
const url = req.url;
if (url) {
let path = url.split("=");
if (path[1] == "secret-token") {
req.user = "john";
next();
}
} else {
res.sendStatus(401);
}
};
// store contacts in an arrays
var contacts = [
{
name: "peter parker",
age: 21,
email: "[email protected]",
courses: [
{ number: "1.00", name: "engr comp" },
{ number: "3.00", name: "intro bio" },
],
},
{
name: "bruce wayne",
age: 32,
email: "[email protected]",
courses: [
{ number: "2.00", name: "intro ME" },
{ number: "3.00", name: "intro MS" },
],
},
{
name: "diana prince",
age: 25,
email: "[email protected]",
courses: [
{ number: "2.00", name: "intro arch" },
{ number: "1.00", name: "intro chem" },
],
},
];
app.get("/", function (req, res) {
res.send("<h1> Goodbye Routes: try POST to /contact and GET /contacts </h1>");
});
app.get("/login", (req, res) => {
// send back a login form
let form = `<form action="/auth" method="post">
<label for="name">Enter name: </label>
<input id="name" type="text" name="name" value="name">
<input id="password" type="text" name="password" value="password">
<input type="submit" value="OK">
</form>`;
res.send(form);
});
app.post("/auth", (req, res) => {
let { name, password } = req.body;
// check if user is in DB if so send back security token
let form = `<form action="/contacts" method="get">
<label for="name">Get Contacts </label>
<input id="token" type="hidden" name="token" value="secret-token">
<input type="submit" value="OK">
</form>`;
res.send(form);
});
// list all contacts
app.get("/contacts", authenticate2, function (req, res) {
res.json(contacts);
});
app.get("/contacts/:name/:email", (req, res) => {
res.send(`name: ${req.params.name}, email: ${req.params.email}`);
});
// add a contact
app.post("/contact", (req, res) => {
contacts.push({ name: req.body.name, email: req.body.email });
res.redirect("/contacts/" + req.body.name);
});
app.get("/contacts/:name", (req, res) => {
res.send("Redirect with " + req.params.name);
});
app.listen(3000);
console.log("Running on port 3000");