-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (191 loc) · 6.77 KB
/
index.js
File metadata and controls
208 lines (191 loc) · 6.77 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const express = require('express');
const app = express();
const axios = require('axios');
const appwrite = require('node-appwrite');
const PORT = parseInt(process.env.PORT) || 5955
const router = new express.Router();
const bcrypt = require('bcrypt');
var bodyParser = require('body-parser');
const saltRounds = 10;
const APPWRITE_ENDPOINT = process.env.APPWRITE_ENDPOINT;
const PROJECT_ID = process.env.APPWRITE_PROJ_ID;
const SECRET_KEY = process.env.APPWRITE_SECRET;
const COLL_ID = 'oneid';
const { Query } = require('node-appwrite');
const mail = require('./mail');
const cors = require('cors');
app.use(cors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}));
// Generate OTP
function generateOTP() {
// Declare a digits variable
// which stores all digits
var digits = '0123456789';
let OTP = '';
for (let i = 0; i < 6; i++) {
OTP += digits[Math.floor(Math.random() * 10)];
}
return OTP;
}
const sendOTP = async(email, otp) => {
return mail({
from: '"OneID Verification Team" <verify@oneid.ml>',
to: [email],
subject: `OneID Verification OTP: ${otp}`,
html: `Thanks for creating your OneID! It'll really benefit you alot!
<br> Your <b>OneID</b> One-Time-Password is <b>${otp}</b>.
<br> After filling this OTP, you can use your OneID!
<br> Please don't share this OTP with anyone.
<br> If you received this email by mistake, please ignore it.`
});
}
app.use(bodyParser.json())
// Init SDK
let client = new appwrite.Client();
let database = new appwrite.Database(client);
client
.setEndpoint(APPWRITE_ENDPOINT) // Your API Endpoint
.setProject(PROJECT_ID) // Your project ID
.setKey(SECRET_KEY) // Your secret API key
;
/*(async() => {
const hash = '$2b$10$ebMJ7xDSEcFRvi8cpKkyKu/d8HxZjA5ldZreJN.59DygNQ4kxkoGe';
// Load hash from your password DB.
const result1 = await bcrypt.compare(myPlaintextPassword, hash);
// result1 == true
console.log(result1);
const result2 = await bcrypt.compare(someOtherPlaintextPassword, hash);
// result2 == false
console.log(result2);
})();*/
app.listen(PORT, (success, err) => {
if (err) console.log(err);
console.log(`Server listening on: ${PORT}`)
})
app.post('/register', async(req, res) => {
const { name, oneid, email, pass } = req.body
if (!name || !email || !pass || !oneid) {
console.log('Missing parameters')
res.status(400).json({
message: 'Please fill all the fields'
})
} else {
if (!oneid.endsWith('.1id')) {
console.log(`OneID must end with .1id`)
return res.status(401).json({ error: 'OneID must end with .1id' })
} else {
const salt = await bcrypt.genSalt(saltRounds);
const hashedPass = await bcrypt.hash(pass, salt);
const user = {
name,
oneid,
email,
password: hashedPass,
data: JSON.stringify({}),
verified: false
}
try {
const data = await database.listDocuments(COLL_ID, [Query.equal('oneid', oneid)]);
if (data.documents.length == 0) {
const result = await database.createDocument(COLL_ID, user.oneid, user);
console.log(result);
return res.status(201).json({
message: 'User created successfully',
data: result
});
} else {
console.log(`OneID already exists`)
return res.status(401).json({ error: 'OneID already exists' })
}
} catch (err) {
console.log(err)
return res.status(500).json({ err: err });
}
}
}
})
app.post('/login', async(req, res) => {
const { oneid, pass } = req.body
if (!oneid || !pass) {
res.status(400).json({
message: 'Please fill all the fields'
})
} else {
if (!oneid.endsWith('.1id')) {
return res.status(401).json({ error: 'OneID must end with .1id' })
} else {
try {
const user = await database.getDocument(COLL_ID, oneid);
if (user) {
const result = await bcrypt.compare(pass, user.password);
if (result) {
if (user.verified) {
return res.status(200).json({
message: 'User logged in successfully',
data: user
});
} else {
return res.status(200).json({ error: 'User not verified' })
}
} else {
return res.status(401).json({ error: 'Invalid password' })
}
} else {
return res.status(401).json({ error: 'User not found' })
}
} catch (err) {
console.log(err);
return res.status(500).json({ err: err });
}
}
}
});
app.post('/verify-mail', async(req, res) => {
const { email } = req.body
if (!email) {
res.status(400).json({
message: 'Please fill all the fields'
})
} else {
const otp = generateOTP();
await sendOTP(email, otp);
console.log(`OTP ${otp} sent to ${email}`)
return res.status(200).json({
message: 'OTP sent successfully',
otp: otp
})
}
})
app.post('/verify-user', async(req, res) => {
const { oneid } = req.body;
if (!oneid) {
res.status(400).json({
message: 'Please fill all the fields'
})
} else {
try {
const users = await database.listDocuments(COLL_ID, [Query.equal('oneid', oneid)]);
if (users.length == 0) {
res.status(401).json({ error: 'OneID not found' })
console.log(`OneID not found`)
} else {
const user = users[0];
if (user.verified) {
console.log(`OneID already verified`)
return res.status(401).json({ error: 'OneID already verified' })
} else {
database.updateDocument(COLL_ID, user.id, { verified: true });
return res.status(200).json({ message: 'OneID verified successfully' })
}
}
} catch (err) {
console.log(err);
return res.status(500).json({ err: err });
}
}
})
app.use(router)