Start work on optional ads
Seva Luchianov
4 years ago
12 | 12 | "bufferutil": "^4.0.1", |
13 | 13 | "clamscan": "^1.3.1", |
14 | 14 | "command-line-args": "^5.1.1", |
15 | "cookie-parser": "^1.4.5", | |
15 | 16 | "dompurify": "^2.2.2", |
16 | 17 | "dotenv": "^8.2.0", |
17 | 18 | "express": "^4.17.1", |
2840 | 2841 | "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==", |
2841 | 2842 | "engines": { |
2842 | 2843 | "node": ">= 0.6" |
2844 | } | |
2845 | }, | |
2846 | "node_modules/cookie-parser": { | |
2847 | "version": "1.4.5", | |
2848 | "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz", | |
2849 | "integrity": "sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw==", | |
2850 | "dependencies": { | |
2851 | "cookie": "0.4.0", | |
2852 | "cookie-signature": "1.0.6" | |
2853 | }, | |
2854 | "engines": { | |
2855 | "node": ">= 0.8.0" | |
2843 | 2856 | } |
2844 | 2857 | }, |
2845 | 2858 | "node_modules/cookie-signature": { |
12157 | 12170 | "version": "0.4.0", |
12158 | 12171 | "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz", |
12159 | 12172 | "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==" |
12173 | }, | |
12174 | "cookie-parser": { | |
12175 | "version": "1.4.5", | |
12176 | "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz", | |
12177 | "integrity": "sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw==", | |
12178 | "requires": { | |
12179 | "cookie": "0.4.0", | |
12180 | "cookie-signature": "1.0.6" | |
12181 | } | |
12160 | 12182 | }, |
12161 | 12183 | "cookie-signature": { |
12162 | 12184 | "version": "1.0.6", |
36 | 36 | "bufferutil": "^4.0.1", |
37 | 37 | "clamscan": "^1.3.1", |
38 | 38 | "command-line-args": "^5.1.1", |
39 | "cookie-parser": "^1.4.5", | |
39 | 40 | "dompurify": "^2.2.2", |
40 | 41 | "dotenv": "^8.2.0", |
41 | 42 | "express": "^4.17.1", |
88 | 89 | "webpack-dev-server": "^3.11.2" |
89 | 90 | }, |
90 | 91 | "export-symbol": "extension.exports" |
91 | }⏎ | |
92 | } |
233 | 233 | ).catch(utils.handle_err.mailgun(res)); |
234 | 234 | }).then(function() { |
235 | 235 | req.logout(); |
236 | res.cookie('login_message', JSON.stringify({ | |
236 | cookies.set(res, 'login_message', { | |
237 | 237 | message: "Account Deleted" |
238 | }), cookies.options({ | |
238 | }, { | |
239 | 239 | httpOnly: false |
240 | })); | |
240 | }); | |
241 | 241 | res.send(utils.ok()); |
242 | 242 | }).catch(utils.handle_err.res(res, "Error deleting your account, try again later")); |
243 | 243 | }, |
1 | 1 | const upload_session = require('../utils/upload_session'); |
2 | 2 | const cookies = require('../utils/cookies'); |
3 | 3 | |
4 | module.exports = { | |
5 | get: { | |
6 | "": function(req, res) { | |
7 | // Any time a user visits this page it | |
8 | // is loading for the first time. | |
9 | // Hence they have no upload session. | |
10 | let current_upload_session = upload_session.get(req.user); | |
11 | if (current_upload_session) { | |
12 | upload_session.delete(req.user); | |
13 | utils.rimraf(current_upload_session.temp_dir).catch(res.locals.log.general.error); | |
14 | } | |
4 | function get_user_prefs(req, res) { | |
5 | const default_prefs = { | |
6 | ads_enabled: 0 | |
7 | }; | |
8 | ||
9 | if (!req.user) { | |
10 | return utils.resolve(default_prefs); | |
11 | } | |
12 | ||
13 | return utils.query( | |
14 | "SELECT ads_enabled FROM user_prefs WHERE user_id=?", | |
15 | [req.user.user_id] | |
16 | ).then(([user_prefs]) => user_prefs).catch( | |
17 | utils.handle_err.sql(res, default_prefs) | |
18 | ).then(function(user_prefs) { | |
19 | res.locals.log.v("Prefs for", req.user.email, "-", user_prefs); | |
20 | return user_prefs; | |
21 | }); | |
22 | } | |
23 | ||
24 | const routes = [ | |
25 | ['login'], | |
26 | ['register'], | |
27 | ['confirm-email'], | |
28 | ['password-reset'] | |
29 | ].reduce(function(request_handlers, [endpoint]) { | |
30 | const title = endpoint.split('-').map(word => utils.captialize(word)).join(' '); | |
31 | return Object.assign(request_handlers, { | |
32 | [endpoint]: [{ | |
33 | auth: false | |
34 | }, function(req, res) { | |
35 | get_user_prefs(req, res).then(function(prefs) { | |
36 | res.render(endpoint, { | |
37 | captcha: true, | |
38 | ads_enabled: prefs.ads_enabled, | |
39 | title: ' - ' + title | |
40 | }); | |
41 | }); | |
42 | }] | |
43 | }); | |
44 | }, { | |
45 | "": function(req, res) { | |
46 | // Any time a user visits this page it | |
47 | // is loading for the first time. | |
48 | // Hence they have no upload session. | |
49 | let current_upload_session = upload_session.get(req.user); | |
50 | if (current_upload_session) { | |
51 | upload_session.delete(req.user); | |
52 | utils.rimraf(current_upload_session.temp_dir).catch(res.locals.log.general.error); | |
53 | } | |
54 | get_user_prefs(req, res).then(function(prefs) { | |
15 | 55 | res.render('home', { |
16 | 56 | title: "", |
57 | ads_enabled: prefs.ads_enabled, | |
17 | 58 | helpers: { |
18 | 59 | current_year: () => new Date().getFullYear() |
19 | 60 | } |
20 | 61 | }); |
21 | }, | |
22 | "logout": function(req, res) { | |
23 | req.logout(); | |
24 | res.cookie('login_message', JSON.stringify({ | |
62 | }) | |
63 | }, | |
64 | "logout": function(req, res) { | |
65 | if (cookies.get(req, 'cookie_consent')) { | |
66 | cookies.set(res, 'login_message', { | |
25 | 67 | message: "Logged Out Successfully" |
26 | }), cookies.options({ | |
68 | }, { | |
27 | 69 | httpOnly: false |
28 | })); | |
29 | res.redirect("/login"); | |
30 | }, | |
31 | "login": [{ | |
32 | auth: false | |
33 | }, function(req, res) { | |
34 | res.render('login', { | |
35 | captcha: true, | |
36 | title: " - Login" | |
37 | 70 | }); |
38 | }], | |
39 | "register": [{ | |
40 | auth: false | |
41 | }, function(req, res) { | |
42 | res.render('register', { | |
43 | captcha: true, | |
44 | title: " - Register" | |
45 | }); | |
46 | }], | |
47 | "confirm-email": [{ | |
48 | auth: false | |
49 | }, function(req, res) { | |
50 | res.render('confirm-email', { | |
51 | captcha: true, | |
52 | title: " - Confirm Email" | |
53 | }); | |
54 | }], | |
55 | "password-reset": [{ | |
56 | auth: false | |
57 | }, function(req, res) { | |
58 | res.render('password-reset', { | |
59 | captcha: true, | |
60 | title: " - Password Reset" | |
61 | }); | |
62 | }] | |
71 | } | |
72 | req.logout(); | |
73 | res.redirect("/login"); | |
63 | 74 | } |
75 | }); | |
76 | ||
77 | module.exports = { | |
78 | get: routes | |
64 | 79 | };⏎ |
0 | function cookie_options(options) { | |
1 | return Object.assign({ | |
2 | sameSite: 'strict' | |
3 | }, process.env.DEBUG ? {} : { | |
4 | secure: true | |
5 | }, options || {}); | |
6 | } | |
7 | ||
0 | 8 | module.exports = { |
1 | options: function(options) { | |
2 | return Object.assign({ | |
3 | sameSite: 'strict' | |
4 | }, process.env.DEBUG ? {} : { | |
5 | secure: true | |
6 | }, options || {}); | |
7 | } | |
9 | get: function(req, name) { | |
10 | // returns "" if no cookie stored | |
11 | return JSON.parse(req.cookies[name] || '""'); | |
12 | }, | |
13 | set: function(res, name, data, options) { | |
14 | res.cookie(name, JSON.stringify(data), cookie_options(options)); | |
15 | }, | |
16 | options: cookie_options | |
8 | 17 | }⏎ |
90 | 90 | ext_log.general.error(error.stack); |
91 | 91 | |
92 | 92 | if (non_fatal) { |
93 | return new Promise(r => r()); | |
93 | return new Promise(r => r(non_fatal)); | |
94 | 94 | } |
95 | 95 | return new Promise((_, r) => r(message(error))); |
96 | 96 | } |
118 | 118 | }, |
119 | 119 | enumify: enumify, |
120 | 120 | status: STATUS_CODES_ENUM, |
121 | captialize: function(string) { | |
122 | if (!string) { | |
123 | return ""; | |
124 | } | |
125 | return string[0].toUpperCase() + string.slice(1); | |
126 | }, | |
121 | 127 | uuid: uuid, |
122 | 128 | tiny_id: promisify(crypto.randomBytes)(3).then(buf => buf.toString('hex').toUpperCase()), |
123 | 129 | ok: function(data) { |
31 | 31 | const app = express(); |
32 | 32 | const exphbs = require("express-handlebars"); |
33 | 33 | const rate_limit = require("express-rate-limit"); |
34 | const cookie_parser = require('cookie-parser') | |
34 | 35 | const session = require('express-session'); |
35 | 36 | const MySQLStore = require('express-mysql-session')(session); |
36 | 37 | const passport = require('passport'); |
62 | 63 | app.use(express.urlencoded({ |
63 | 64 | extended: true |
64 | 65 | })); |
66 | app.use(cookie_parser()); | |
65 | 67 | |
66 | 68 | app.use(express.static("./dist")); |
67 | 69 | app.use(express.static("./public")); |