This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
123 lines (97 loc) · 2.36 KB
/
index.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
const axios = require('axios');
const fs = require('fs');
const Koa = require('koa');
const Router = require('@koa/router');
const bodyParser = require('koa-bodyparser');
const r = require('rethinkdb');
(async () => {
const conn = await require('./lib/connection');
const app = new Koa();
const router = new Router();
router.get('/keys', ctx => {
ctx.redirect('/');
});
router.post('/api/sign-up', async ctx => {
const { email, satelliteAddress, password } = ctx.request.body;
const cursor = await r.table('accounts2')
.orderBy('tempEmail')
.filter({
satelliteAddress
})
.filter(function (row) {
return row.hasFields({ userEmail: true }).not()
})
.run(conn);
const [account] = await cursor.toArray();
await r.table("accounts2")
.filter({
tempEmail: account.tempEmail,
satelliteAddress
})
.update({
userEmail: email,
signupTime: new Date()
})
.run(conn);
let token = (await axios.post(`https://${satelliteAddress}/api/v0/auth/token`, {
email: account.tempEmail,
password: account.password
})).data;
console.log({ token });
try {
await axios.post(`https://${satelliteAddress}/api/v0/auth/account/change-email`, {
newEmail: email
}, {
headers: {
Cookie: `_tokenKey=${token};`
}
});
} catch(err) {
if(err.response.status === 409) {
ctx.body = {
error: 'Email already in use'
};
}
return;
}
console.log('changed email');
token = (await axios.post(`https://${satelliteAddress}/api/v0/auth/token`, {
email,
password: account.password
})).data;
console.log('reauthenticated');
console.log({
email,
currentPassword: account.password,
newPassword: password
});
await axios.post(`https://${satelliteAddress}/api/v0/auth/account/change-password`, {
password: account.password,
newPassword: password
}, {
headers: {
Cookie: `_tokenKey=${token};`
}
});
await r.table("accounts2")
.filter({
tempEmail: account.tempEmail,
satelliteAddress
})
.update({
flipped: true
})
.run(conn);
ctx.body = {
apiKey: account.apiKey,
satelliteAddress: account.satelliteAddress
}
});
app.use(require('koa-static')('dist'));
app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log("Wormhole has started :)");
})();