-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (134 loc) · 3.61 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const jwt = require('jsonwebtoken')
const express = require('express')
const app = express()
const port = 3000
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://xuhuan:[email protected]/?retryWrites=true&w=majority"
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
// const user = await client.db("Week5").collection("idk").find({"username": "IVE"}).toArray();
const user = await client.db("Week5").collection("idk").insertOne({"username":"Chua2", "password":"chua3182"})
console.log(user);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
let dbUsers = [
{
username: "Wong",
password: "password",
name: "Wong Xu Huan",
email: "[email protected]"
},
{
username: "Chua",
password: "010uehs",
name: "CHua CF",
email: "[email protected]"
}
]
app.use(express.json());
function login(username, password) {
console.log("Someone try to login with", username, password)
let matched = dbUsers.find(element =>
element.username == username
)
if (matched){
if(matched.password == password) {
return matched
} else{
return "Password not matched"
}
} else{
return "Username not found"
}
}
function register(newusername, newpassword, newname, newemail){
//TODO: Check username if exist
let checking = dbUsers.find(element=>
element.username == newusername
)
if(checking){
console.log("This username has been registered")
}
else{
dbUsers.push({
username: newusername,
password: newpassword,
name: newname,
email: newemail
})
return ("Register successful")
}
}
//To generate JWT token
function generateToken(userProfile){
return jwt.sign(
userProfile
,'secret', { expiresIn: 60 * 60 });
}
//To verify JWT Token
function verifyToken(req, res, next){
let header = req.headers.authorization
console.log(header)
let token = header.split(' ')[1]
jwt.verify(token, 'secret', function(err, decoded) {
if(err) {
res.send("Invalid Token")
}
req.user = decoded
// console.log(decoded) // bar
next()
});
}
app.post('/register',(req,res) => {
let data = req.body
res.send(
register(
data.newusername,
data.newpassword,
data.newname,
data.newemail
)
);
});
// create a post for user to login
app.post('/login',(req,res)=> {
// get the username and password from the request body
const {username,password} = req.body;
//find the user in the database
const user = dbUsers.find(user => user.username === username && user.password === password);
//if user is found, return the user object
if(user){
res.send(user);
} else {
//if user is not found, return an error message
res.send({error : "User not found"})
}
})
app.post('/login2', (req, res) => {
let data = req.body
const user = login(data.username, data.password)
res.send(generateToken(user))
})
app.get('/bye', verifyToken ,(req, res) => {
console.log(req.user)
res.send('Bye Bye World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
// console.log(login("Wong", "password"))