Skip to content

Commit 2998059

Browse files
committed
first commit 🎉
0 parents  commit 2998059

10 files changed

+1475
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# package directories
2+
node_modules
3+
jspm_packages
4+
5+
# Serverless directories
6+
.serverless

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
"standard.enable": true,
4+
"eslint.enable": false
5+
}

api/_cleanBody.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = function cleanBody (body) {
2+
const res = {
3+
githubId: body.githubId
4+
}
5+
if (body.email) {
6+
res.email = body.email
7+
}
8+
if (body.login) {
9+
res.login = body.login
10+
}
11+
return res
12+
}

api/getOne.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const storage = require('../storage')()
2+
3+
module.exports.handler = (event, context, callback) => {
4+
storage
5+
.findOne(event.githubId)
6+
.then(item => {
7+
if (!item) {
8+
callback(new Error('[404] Not found'))
9+
return
10+
}
11+
callback(null, item)
12+
})
13+
.catch(callback)
14+
}

api/login.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const storage = require('../storage')()
2+
const cleanBody = require('./_cleanBody')
3+
4+
module.exports.handler = (event, context, callback) => {
5+
let parsedBody
6+
try {
7+
parsedBody = JSON.parse(event.body || {})
8+
} catch (e) {
9+
callback(new Error('[400] Could not parse the body'))
10+
return
11+
}
12+
13+
const body = cleanBody(parsedBody)
14+
if (!body.githubId) {
15+
callback(new Error('[400] Missing github ID'))
16+
return
17+
}
18+
19+
storage
20+
.findOne(body.githubId)
21+
.then(found => {
22+
if (found) {
23+
return callback(null, {
24+
ok: true,
25+
message: 'Already logged in'
26+
})
27+
}
28+
return storage.create(body).then(() => callback(null, {
29+
ok: true,
30+
message: 'Logged in'
31+
}))
32+
})
33+
.catch(callback)
34+
}

api/unlock.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const stripe = require('stripe')(process.env.STRIPE_SECRET)
2+
const storage = require('../storage')()
3+
const cleanBody = require('./_cleanBody')
4+
5+
module.exports.handler = (event, context, callback) => {
6+
let parsedBody
7+
try {
8+
parsedBody = JSON.parse(event.body || {})
9+
} catch (e) {
10+
callback(new Error('[400] Could not parse the body'))
11+
return
12+
}
13+
14+
const body = cleanBody(parsedBody)
15+
if (!body.githubId) {
16+
callback(new Error('[400] Missing github ID'))
17+
return
18+
}
19+
20+
const token = parsedBody.token
21+
let method
22+
let bailout = false
23+
24+
storage
25+
.findOne(body.githubId)
26+
.then(found => {
27+
if (found) {
28+
if (found.valid) {
29+
callback(new Error('[403] Already unlocked'))
30+
bailout = true
31+
return
32+
}
33+
method = 'update'
34+
return
35+
}
36+
method = 'create'
37+
})
38+
.then(() => {
39+
if (bailout) { return }
40+
return stripe.customers.create({
41+
email: body.email,
42+
source: token
43+
})
44+
})
45+
.then(customer => {
46+
if (bailout) { return }
47+
console.log(customer)
48+
body.stripeId = customer.id
49+
return stripe.subscriptions.create({
50+
customer: customer.id,
51+
plan: 'kactus-1-month'
52+
})
53+
})
54+
.then(res => {
55+
if (bailout) { return }
56+
console.log(res)
57+
body.valid = true
58+
})
59+
.then(() => {
60+
if (bailout) { return }
61+
return storage[method](body)
62+
})
63+
.then(res => {
64+
if (bailout) { return }
65+
callback(null, {
66+
message: 'Unlocked full access'
67+
})
68+
})
69+
.catch(callback)
70+
}

0 commit comments

Comments
 (0)