-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
188 lines (150 loc) · 4.15 KB
/
server.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
let { PORT, MONGODB, ASSETS, JWT_KEY, STRIPE_SK } = process.env;
import { join } from 'path'
import koa from 'koa'
import json from 'koa-json'
import jsonBody from 'koa-json-body'
import route from 'koa-route'
import serve from 'koa-static'
import jwt from 'koa-jwt'
import Joi from 'joi';
import monk from 'monk'
import wrap from 'co-monk'
import router from 'koa-joi-router'
let pswd = require('pswd')();
let auth = jwt({ secret: process.env.JWT_KEY });
let staticFiles = serve(join(__dirname, ASSETS), { defer: false })
let stripe = require('stripe')(process.env.STRIPE_SK);
/**
* Mongo DB Tables
*/
let db = monk(process.env.MONGODB);
let appsDb = wrap(db.get('apps'));
let usersDb = wrap(db.get('users'));
let stripeDb = wrap(db.get('stripe'));
/**
* Koa Application & Middleware
*/
function* errors(next) {
try {
yield next;
} catch (err) {
console.error(err);
this.status = err.status || 500;
this.body = {
error: err.message
}
this.app.emit('error', err, this);
}
}
function authorize(user) {
delete user.password;
return {
token: jwt.sign(user, process.env.JWT_KEY, { expiresInMinutes: 60 * 5 }),
user: user,
}
}
let loginRoute = {
method: 'post',
path: '/login',
validate: {
body: {
email: Joi.string().lowercase().email(),
password: Joi.string().max(100)
},
type: 'json'
},
handler: function* () {
var email = this.request.body.email;
var password = this.request.body.password;
var user = yield usersDb.findOne({ email: email });
this.assert(user, 401, 'Incorrect Email or Password');
var valid = yield pswd.compare(password, user.password);
this.assert(valid, 401, 'Incorrect Email or Password');
this.body = authorize(user);
}
};
let registerRoute = {
method: 'post',
path: '/register',
validate: {
body: {
email: Joi.string().lowercase().email(),
password: Joi.string().max(100)
},
type: 'json'
},
handler: function* () {
var email = this.request.body.email;
var password = this.request.body.password;
var duplicate = yield usersDb.findOne({ email: email });
this.assert(!duplicate, 400, 'Klouds ID already exists');
var hash = yield pswd.hash(password);
this.assert(hash, 500, 'Failed to hash password');
var user = yield usersDb.insert({
email: email,
password: hash
});
this.assert(user, 500, 'Failed to insert new user');
this.body = authorize(user);
}
}
let appsRoute = {
method: 'get',
path: '/apps',
handler: function*() {
this.body = yield appsDb.find({ disabled: { "$exists" : false }});;
}
}
let disabledRoute = {
method: 'get',
path: '/disabled',
handler: function*() {
this.body = yield appsDb.find({ disabled: true });
}
}
let subscribeRoute = {
method: 'post',
path: '/subscribe',
validate: { type: 'json' },
handler: [
auth,
function*() {
let createStripeCustomer = (customer) => function get_thunked_lol(cb) {
return stripe.customers.create(customer, cb)
}
var params = this.request.body;
var app = params.app;
var stripeToken = params.tok;
var customer = yield createStripeCustomer({
source: stripeToken,
plan: "web_application",
email: this.state.user.email
});
console.log('Stripe Customer', stripeCustomer);
this.assert(customer, 500, 'Stripe api call failed');
this.body = { customer: customer.id };
var inserted = yield stripeDb.insert(customer);
console.log(inserted);
}
]
}
let app = koa();
app.use(json());
// app.use(jsonBody({ limit: '10kb' }));
app.use(errors);
app.use(staticFiles);
let noAuth = router();
noAuth.route(loginRoute);
noAuth.route(registerRoute);
noAuth.route(appsRoute);
noAuth.route(disabledRoute);
app.use(noAuth.middleware());
let user = router();
user.route(subscribeRoute);
app.use(user.middleware());
/* You Shall Not Pass! */
// AUTH AUTH AUTH AUTH AUTH |UTH AUTH AUTH AUT| AUTH AUTH AUTH AUTH AUTH
// AUTH AUTH AUTH AUTH AUTH |UTH AUTH AUTH AUT| AUTH AUTH AUTH AUTH AUTH
app.use(auth);
console.log(`Listening on port ${PORT}`);
app.listen(PORT);