-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
125 lines (108 loc) · 3.9 KB
/
Copy pathapp.js
File metadata and controls
125 lines (108 loc) · 3.9 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
const express = require('express')
const path = require('path')
const favicon = require('serve-favicon')
const logger = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const session = require('express-session')
const FileStore = require('session-file-store')(session)
const index = require('./routes/index')
const firebase = require('./firebase')
// const hooks = require('./hooks/index')
const app = express()
const moment = require('moment')
require('moment-timezone')
require('moment/locale/tr')
moment.globalLocale = 'tr'
app.locals.moment = moment
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.json())
app.use(session({
secret: '2018CCDersIm',
saveUninitialized: true,
store: new FileStore({path: '/tmp/sessions', secret: '2018CCDersIm'}),
resave: false,
rolling: true,
httpOnly: true,
cookie: { maxAge: 604800000 } // week
}))
app.use('/', index)
async function verify (token) {
try {
let decodedToken = await firebase.auth().verifyIdToken(token)
let user = await firebase.firestore().doc(`/user/${decodedToken.user_id}`).get()
user = user.data()
decodedToken.isAdmin = user.isAdmin
decodedToken.slug = user.slug
return decodedToken
} catch (e) {
console.log(e)
throw new Error(e)
}
}
app.use((req, res, next) => {
console.log('MIDDLEWARE', req.session.downloadLimit);
req.firebaseServer = firebase
if (req.session.first) {
req.session.first != req.session.first
} else {
req.session.first = true
// req.session.downloadLimit = 1
}
next()
})
app.post('/api/login', async (req, res) => {
if (!req.body) return res.sendStatus(400)
try {
let decodedToken = await verify(req.body.token)
req.session.decodedToken = decodedToken
req.session.downloadLimit = 1
res.json({ status: true, decodedToken, limit: req.session.downloadLimit })
} catch (e) {
res.json({ status: false, error: e })
}
})
app.post('/api/logout', (req, res) => {
req.session.decodedToken = null
// req.session.downloadLimit = req.session.downloadLimit || 10
console.log('LOGOUT', req.session.downloadLimit);
res.json({ status: true, limit: req.session.downloadLimit, message: 'I know you bro, you don\'t like limits but this isn\'t for you. You can bypass this easily. But who cares? I do not.' })
})
app.get('/api/download', (req, res) => {
if (req.session.decodedToken) {
res.json({ status: true, limit: req.session.downloadLimit, message: 'You are logged in, that\'s the point. You got this friend. Here\'s your document.' })
} else {
req.session.downloadLimit = --req.session.downloadLimit || 0
if (req.session.downloadLimit < 0) {
req.session.downloadLimit = 0
}
res.json({ status: true, limit: req.session.downloadLimit, message: 'I know you bro, you don\'t like limits but this isn\'t for you. You can bypass this easily. But who cares? I do not.' })
}
})
app.get('/api/gain', (req, res) => {
req.session.downloadLimit += 1
res.json({ status: true, limit: req.session.downloadLimit, message: 'I know you bro, you don\'t like limits but this isn\'t for you. You can bypass this easily. But who cares? I do not.' })
})
app.get('/api/limit', (req, res) => {
req.session.downloadLimit = req.session.downloadLimit || 0
res.json({ status: true, limit: req.session.downloadLimit })
})
app.use((req, res, next) => {
const err = new Error('Not Found')
err.status = 404
next(err)
})
app.use((err, req, res, next) => {
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
res.status(err.status || 500)
res.render('error')
})
module.exports = app