-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
89 lines (70 loc) · 2.01 KB
/
index.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
const express = require('express');
const jwt = require('jsonwebtoken');
const JWT_SECRET = "ramdomharkiratilovekiara"
const app = express();
app.use(express.json());
const users = [];
app.post("/signup", function (req, res) {
const username = req.body.username;
const password = req.body.password;
users.push({
username: username,
password: password
})
res.json({
message: "You are signed up"
})
console.log(users)
})
app.post("/signin", function(req, res) {
const username = req.body.username;
const password = req.body.password;
// maps and filter
let foundUser = null;
for (let i = 0; i<users.length; i++) {
if (users[i].username == username && users[i].password == password) {
foundUser = users[i]
}
}
if (foundUser) {
const token = jwt.sign({
username: username,
password: password,
firstname,
lastName,
courses: []
}, JWT_SECRET) ;
// foundUser.token = token;
res.json({
token: token
})
} else {
res.status(403).send({
message: "Invalid username or password"
})
}
console.log(users)
})
app.get("/me", function(req, res) {
const token = req.headers.token // jwt
const decodedInformation = jwt.verify(token, JWT_SECRET); // {username: "harkirat@gmail.com"}
const unAuthDecodedinfo = jwt.decode(token,); // {username: "harkirat@gmail.com"}
const username = decodedInformation.username
let foundUser = null;
for (let i = 0; i < users.length; i++) {
if (users[i].username == username) {
foundUser = users[i]
}
}
if (foundUser) {
res.json({
username: foundUser.username,
password: foundUser.password
})
} else {
res.json({
message: "token invalid"
})
}
})
app.listen(3000);// that the http server is listening on port 3000