-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
224 lines (214 loc) · 8.68 KB
/
auth.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var mongoose = require('mongoose');
var userModel = require('./models/user');
var STRIPE_PRODUCTION = false;
mongoose.connect(process.env.MONGO_SRV, { useNewUrlParser: true, useUnifiedTopology: true });
if(process.env.PRODUCTION == "true") {
mongoose.connection.useDb('brainbase');
}
function generateRandomString(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw123456789";
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function generateSecretKey() {
return `${generateRandomString(6)}-${generateRandomString(6)}-${generateRandomString(6)}`
}
function setupFor(app) {
require('./images')(app);
app.get('/login', function(req, res) {
res.type('html');
res.end(require('fs').readFileSync('public/login.html', {encoding: 'utf-8'}));
});
app.use('/user/login', async function (req, res) {
var username = req.body.username;
var password = req.body.password;
await userModel.findOne({username: username})
.then(async function(user) {
if(user == null) {
await userModel.findOne({email: username})
.then(function(user) {
if(user == null) {
res.send({success: false, 'error': 'User not found'});
}else if(user.password != password) {
res.send({success: false, 'error': 'Invalid username, email, or password'});
}else {
res.send({success: true, key: user.secretKey});
}
});
}else if(user.password != password) {
res.send({success: false, 'error': 'Invalid username, email, or password'});
}else {
res.send({success: true, key: user.secretKey});
}
});
});
app.use('/reset-password/:email', (req, res) => {
var {Email} = require('./packages/smtp');
Email.send({
Host: "smtp.gmail.com",
Username: "[email protected]",
Password: process.env.KLASH_DEV_PASSWORD,
To: req.params.email,
From: "[email protected]",
Subject: "Sending Email using javascript",
Body: "Well that was easy!!",
})
.then(Result => {
res.end(JSON.stringify(Result));
})
})
app.use('/user/register', async function (req, res) {
var key = generateSecretKey();
res.type('application/json');
if(req.body.username == null || req.body.password == null || req.body.email == null || req.body == null ||
req.body.username == undefined || req.body.password == undefined || req.body.email == undefined || req.body == undefined ||
req.body.username.trim() == "" || req.body.password.trim() == "" || req.body.email.trim() == "") {
res.send({"error": 'Missing username, password, or email', "success": false});
} else {
var continueAfterFind = true;
userModel.findOne({email: req.body.email})
.then((doc) => {
if(doc != null) {
res.send({"error": 'Email already exists!', "success": false});
continueAfterFind = false;
return;
}
})
.then(() => {
if (!continueAfterFind) return;
userModel.findOne({username: req.body.username})
.then((doc) => {
if(doc != null) {
res.send({"error": 'Username already exists!', "success": false});
continueAfterFind = false;
return;
}
})
.then(async () => {
if (!continueAfterFind) return;
try {
var user = new userModel({
username: req.body.username,
password: req.body.password,
email: req.body.email,
secretKey: key
});
var done = false;
await mongoose.connection.collection('users').insertOne(user).catch(err => {
res.send({"error": "Email or password already exists", "success": false});
done = true;
return;
});
if(done == true) {
return;
} else {
res.send({
success: true,
secret: key
});
}
} catch(exc) {
res.send({"error": exc.message, success: false});
}
});
});
}
});
app.use('/user/check/premium', async function (req, res) {
var key = req.body.key;
if(key == null || key == undefined || key.trim() == "") {
res.send({success: false, error: "Missing key"});
} else {
await userModel.findOne({secretKey: key})
.then(function(user) {
if(user == null) {
res.send({success: false, error: "User not found", premium: false});
} else {
res.send({success: true, premium: user.premium});
}
});
}
});
app.use('/user/check/exists', async function (req, res) {
var key = req.body.key;
if(key == null || key == undefined || key.trim() == "") {
res.send({success: false, error: "Missing key"});
} else {
await userModel.findOne({secretKey: key})
.then(function(user) {
if(user == null) {
res.send({success: false, error: "User not found", exists: false});
} else {
res.send({success: true, exists: true});
}
});
}
});
// Route to serve the news.html file
app.use('/news', (req, res) => {
const fs = require('fs');
const newsHtml = fs.readFileSync('public/news.html', 'utf-8');
res.end(newsHtml);
});
// Route to serve the contents of news.json file
app.use('/listnews', (req, res) => {
const fs = require('fs');
const newsJson = fs.readFileSync('news.json', 'utf-8');
res.json(JSON.parse(newsJson));
});
app.use('/announce', async (req, res)=>{
var content = {
title: req.body.title,
body: require('marked').marked(req.body.content),
author: req.body.author
};
var fs = require('fs');
try {
let data = fs.readFileSync('news.json', 'utf8');
let news = JSON.parse(data);
news.push(content);
let json = JSON.stringify(news);
fs.writeFileSync('news.json', json, 'utf8');
console.log("Announcement saved successfully!");
} catch (err) {
console.log(err);
res.end('Failure');
return;
}
res.end('Success!');
});
const stripe = require('stripe');
var stripeApp = new stripe.Stripe(STRIPE_PRODUCTION ? process.env.STRIPE_SECRET : process.env.STRIPE_SECRET_TEST);
// Define a new route for processing payments
app.post('/stripe/create-intent', (req, res) => {
const amount = 1000; // amount in cents
const currency = req.body.currency;
stripeApp.paymentIntents.create({
amount: amount,
currency: currency,
payment_method_types: ['card']
}).then(intent => {
res.send({secret: intent.client_secret});
});
});
app.get('/stripe/config', (req, res)=>{res.json({pubKey: (STRIPE_PRODUCTION ? process.env.STRIPE_PUBLIC : process.env.STRIPE_PUBLIC_TEST)})})
app.use('/user/check/admin', async function (req, res) {
var key = req.body.key;
if(key == null || key == undefined || key.trim() == "") {
res.send({success: false, error: "Missing key"});
} else {
await userModel.findOne({secretKey: key})
.then(function(user) {
if(user == null) {
res.send({success: false, error: "User not found", admin: false});
} else {
res.send({success: true, admin: user.admin});
}
});
}
});
}
module.exports = setupFor;