notherbase-fs 1.5.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/controllers/creation.js +90 -0
- package/controllers/spirit-world.js +110 -0
- package/controllers/spirits/attribute.js +47 -0
- package/controllers/spirits/contact.js +16 -0
- package/controllers/spirits/inventory.js +71 -0
- package/controllers/spirits/item.js +41 -0
- package/controllers/spirits/serve.js +33 -0
- package/controllers/spirits/user.js +130 -0
- package/controllers/spirits/util.js +39 -0
- package/models/index.js +130 -14
- package/models/spirit.js +159 -0
- package/notherbase-fs.js +23 -73
- package/package.json +2 -2
- package/public/js/commune.js +22 -0
- package/public/js/establishment.js +44 -0
- package/public/js/memories.js +29 -30
- package/public/styles/main.css +9 -99
- package/test/explorer/coast/tall-beach/nono-cove/server-scripts/emailTime.js +7 -3
- package/test/explorer/coast/tall-beach/nono-cove/views/index.ejs +38 -60
- package/test/pages/server-scripts/emailTime.js +15 -0
- package/test/pages/test.ejs +30 -3
- package/test/the-front/server-scripts/emailTime.js +7 -3
- package/test/the-front/server-scripts/migrateBig.js +3 -0
- package/test/the-front/views/check.ejs +88 -2
- package/test/the-front/views/index.ejs +43 -101
- package/test-index.js +5 -0
- package/views/account.ejs +25 -36
- package/views/explorer.ejs +48 -1
- package/views/inventory.ejs +41 -99
- package/views/menu.ejs +8 -2
- package/views/more.ejs +8 -14
- package/views/player.ejs +37 -24
- package/controllers/authCheck.js +0 -18
- package/controllers/contact.js +0 -22
- package/controllers/explorer.js +0 -150
- package/controllers/game.js +0 -59
- package/controllers/index.js +0 -10
- package/controllers/inventory.js +0 -116
- package/controllers/item.js +0 -70
- package/controllers/pages.js +0 -33
- package/controllers/the-front.js +0 -70
- package/controllers/user.js +0 -413
- package/controllers/void.js +0 -16
- package/models/chat.js +0 -9
- package/models/contact.js +0 -14
- package/models/detail.js +0 -16
- package/models/game.js +0 -8
- package/models/inventory.js +0 -19
- package/models/item.js +0 -12
- package/models/page.js +0 -14
- package/models/user.js +0 -25
- package/test/test-index.js +0 -5
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
|
|
4
|
+
export default class Creation {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.router = express.Router();
|
|
7
|
+
|
|
8
|
+
this.router.get("/", function(req, res) { res.redirect("/the-front"); });
|
|
9
|
+
this.router.get(`/the-front`, this.front);
|
|
10
|
+
this.router.get(`/the-front/:detail`, this.frontDetail);
|
|
11
|
+
this.router.get(`/:page`, this.page);
|
|
12
|
+
this.router.get(`/:region/:area/:poi`, this.poi);
|
|
13
|
+
this.router.get(`/:region/:area/:poi/:detail`, this.detail);
|
|
14
|
+
this.router.use(this.void);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
explore = async (main, siteTitle, reqUser, req, res, next) => {
|
|
18
|
+
try {
|
|
19
|
+
if (fs.existsSync(main + ".ejs")) {
|
|
20
|
+
let user = new req.db.User("user", req.session.currentUser);
|
|
21
|
+
let userData = await user.recall();
|
|
22
|
+
|
|
23
|
+
let context = {
|
|
24
|
+
siteTitle: siteTitle,
|
|
25
|
+
userID: user._ID,
|
|
26
|
+
user: userData,
|
|
27
|
+
main: main,
|
|
28
|
+
query: req.query,
|
|
29
|
+
dir: req.frontDir,
|
|
30
|
+
route: req.path,
|
|
31
|
+
requireUser: reqUser
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
res.render(`explorer`, context);
|
|
35
|
+
}
|
|
36
|
+
else next();
|
|
37
|
+
}
|
|
38
|
+
catch(err) {
|
|
39
|
+
console.log(err);
|
|
40
|
+
res.status(500).end();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
page = async (req, res, next) => {
|
|
45
|
+
if (fs.existsSync(`${req.contentPath}/pages/${req.params.page}.ejs`)) {
|
|
46
|
+
let user = new req.db.User("user", req.session.currentUser);
|
|
47
|
+
let userData = await user.recall();
|
|
48
|
+
|
|
49
|
+
res.render(`${req.contentPath}/pages/${req.params.page}.ejs`, {
|
|
50
|
+
user: userData,
|
|
51
|
+
query: req.query
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
next();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
front = async (req, res, next) => {
|
|
60
|
+
let main = `${req.contentPath}/the-front/views/index`;
|
|
61
|
+
this.explore(main, `NotherBase - The Front`, false, req, res, next);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
frontDetail = async (req, res, next) => {
|
|
65
|
+
let main = `${req.contentPath}/the-front/views/${req.params.detail}`;
|
|
66
|
+
this.explore(main, `NotherBase - ${req.params.detail}`, false, req, res, next);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
poi = async (req, res, next) => {
|
|
70
|
+
let main = `${req.contentPath}/explorer${req.path}/views/index`;
|
|
71
|
+
this.explore(main, `NotherBase - ${req.params.poi}`, true, req, res, next);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
detail = async (req, res, next) => {
|
|
75
|
+
let main = `${req.contentPath}/explorer/${req.params.region}/${req.params.area}/${req.params.poi}/views/${req.params.detail}`;
|
|
76
|
+
this.explore(main, `NotherBase - ${req.params.detail}`, true, req, res, next);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
void = async (req, res) => {
|
|
80
|
+
res.render(`explorer`,
|
|
81
|
+
{
|
|
82
|
+
siteTitle: "NotherBase | The Void",
|
|
83
|
+
user: null,
|
|
84
|
+
inventory: null,
|
|
85
|
+
main: `${req.contentPath}/void/index`,
|
|
86
|
+
route: `/void`,
|
|
87
|
+
requireUser: false
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { stripHtml } from "string-strip-html";
|
|
3
|
+
import contact from "./spirits/contact.js";
|
|
4
|
+
import inventory from "./spirits/inventory.js";
|
|
5
|
+
import item from "./spirits/item.js";
|
|
6
|
+
import serve from "./spirits/serve.js";
|
|
7
|
+
import attribute from "./spirits/attribute.js";
|
|
8
|
+
import user from "./spirits/user.js";
|
|
9
|
+
import { check, findUser, loginCheck, success } from "./spirits/util.js";
|
|
10
|
+
|
|
11
|
+
export default class SpiritWorld {
|
|
12
|
+
constructor(io) {
|
|
13
|
+
this.io = io;
|
|
14
|
+
this.router = express.Router();
|
|
15
|
+
|
|
16
|
+
this.router.post(`/`, this.do);
|
|
17
|
+
|
|
18
|
+
this.io.on('connection', this.setupChat);
|
|
19
|
+
|
|
20
|
+
Object.assign(this, contact);
|
|
21
|
+
Object.assign(this, inventory);
|
|
22
|
+
Object.assign(this, item);
|
|
23
|
+
Object.assign(this, serve);
|
|
24
|
+
Object.assign(this, attribute);
|
|
25
|
+
Object.assign(this, user);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
do = async (req, res) => {
|
|
29
|
+
let result = null;
|
|
30
|
+
|
|
31
|
+
/* req.body {
|
|
32
|
+
action: "getUserBasic",
|
|
33
|
+
route: "/something" (opt),
|
|
34
|
+
service: "something" (opt),
|
|
35
|
+
scope: "local" (opt),
|
|
36
|
+
parent: "id" (opt),
|
|
37
|
+
_lastUpdate: 0 (opt),
|
|
38
|
+
data: {} (opt)
|
|
39
|
+
} */
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
if (this[req.body.action]) result = await this[req.body.action](req);
|
|
43
|
+
else result = {
|
|
44
|
+
status: "failed",
|
|
45
|
+
message: `No function with the name ${req.body.action}`,
|
|
46
|
+
isUpToDate: true,
|
|
47
|
+
data: {}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
res.send(result);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.log(error);
|
|
53
|
+
res.send(error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setupChat = (socket) => {
|
|
58
|
+
socket.join(socket.handshake.query.room);
|
|
59
|
+
|
|
60
|
+
this.io.to(socket.handshake.query.room).emit("chat message", {
|
|
61
|
+
name: "Server",
|
|
62
|
+
time: Date.now(),
|
|
63
|
+
text: `${socket.handshake.query.name} has joined the room.`
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
socket.on("chat message", (msg) => {
|
|
67
|
+
this.io.to(socket.handshake.query.room).emit("chat message", {
|
|
68
|
+
name: msg.name,
|
|
69
|
+
time: msg.time,
|
|
70
|
+
text: stripHtml(msg.text).result
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
socket.on('disconnect', () => {
|
|
75
|
+
this.io.to(socket.handshake.query.room).emit("chat message", {
|
|
76
|
+
name: "Server",
|
|
77
|
+
time: Date.now(),
|
|
78
|
+
text: `${socket.handshake.query.name} has left the room.`
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
recall = async (req) => {
|
|
84
|
+
if (req.body.scope === "local") {
|
|
85
|
+
loginCheck(req);
|
|
86
|
+
let user = await findUser(req);
|
|
87
|
+
req.body.parent = user.memory._id;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let spirit = new req.db.Spirit(req.body);
|
|
91
|
+
let spiritData = await spirit.recall();
|
|
92
|
+
|
|
93
|
+
check(spiritData, "No spirit found.");
|
|
94
|
+
|
|
95
|
+
return success("Spirit found.", spiritData);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
commit = async (req) => {
|
|
99
|
+
if (req.body.scope === "local") {
|
|
100
|
+
loginCheck(req);
|
|
101
|
+
let user = await findUser(req);
|
|
102
|
+
req.body.parent = user.memory._id;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let spirit = new req.db.Spirit(req.body);
|
|
106
|
+
await spirit.commit(req.body.data);
|
|
107
|
+
|
|
108
|
+
return success("Spirit updated.");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {loginCheck, check, findUser, fail, success} from "./util.js";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
attributes: async (req) => {
|
|
5
|
+
loginCheck(req);
|
|
6
|
+
|
|
7
|
+
let user = await findUser(req);
|
|
8
|
+
|
|
9
|
+
return success("Got user attributes.", user.memory.data.attributes);
|
|
10
|
+
},
|
|
11
|
+
checkAttribute: async (req) => {
|
|
12
|
+
loginCheck(req);
|
|
13
|
+
|
|
14
|
+
let user = await findUser(req);
|
|
15
|
+
let att = user.memory.data.attributes;
|
|
16
|
+
|
|
17
|
+
if (att[req.body.data.check] >= req.body.data.against) {
|
|
18
|
+
return success("Passed check.")
|
|
19
|
+
}
|
|
20
|
+
else return fail("Failed check.");
|
|
21
|
+
},
|
|
22
|
+
setAttribute: async (req) => {
|
|
23
|
+
loginCheck(req);
|
|
24
|
+
|
|
25
|
+
let user = await findUser(req);
|
|
26
|
+
|
|
27
|
+
user.memory.data.attributes[req.body.data.change] = req.body.data.to;
|
|
28
|
+
await user.commit();
|
|
29
|
+
|
|
30
|
+
return success("Attributes set.", user.memory.data.attributes);
|
|
31
|
+
},
|
|
32
|
+
incrementAttribute: async (req) => {
|
|
33
|
+
loginCheck(req);
|
|
34
|
+
|
|
35
|
+
let user = await findUser(req);
|
|
36
|
+
let att = user.memory.data.attributes;
|
|
37
|
+
|
|
38
|
+
if (att[req.body.data.change] < req.body.data.max) {
|
|
39
|
+
att[req.body.data.change]++;
|
|
40
|
+
await user.commit();
|
|
41
|
+
|
|
42
|
+
return success("Attribute incremented.", att[req.body.data.change]);
|
|
43
|
+
}
|
|
44
|
+
else return fail("Attribute maxed.", att[req.body.data.change]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { success } from "./util.js";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
contactNother: async function(req) {
|
|
5
|
+
req.body.service = "contact";
|
|
6
|
+
let contact = new req.db.Spirit(req.body);
|
|
7
|
+
|
|
8
|
+
await contact.commit({
|
|
9
|
+
user: req.session.currentUser,
|
|
10
|
+
location: req.body.data.route,
|
|
11
|
+
content: req.body.data.content
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return success();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { success, check, loginCheck, findUser } from "./util.js";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
getUserInventory: async (req) => {
|
|
5
|
+
loginCheck(req);
|
|
6
|
+
let user = await findUser(req);
|
|
7
|
+
let inv = user.memory.data.inventory;
|
|
8
|
+
|
|
9
|
+
check(inv, "User inventory not found.");
|
|
10
|
+
|
|
11
|
+
return success("User inventory found.", inv);
|
|
12
|
+
},
|
|
13
|
+
updateItemInInventory: async (req) => {
|
|
14
|
+
check(req.body.data.name && req.body.data.amount, `${req.body.data.name} ${req.body.data.amount} Check Input!`);
|
|
15
|
+
|
|
16
|
+
let item = new req.db.Item(req.body.data.name);
|
|
17
|
+
let itemData = await item.recall();
|
|
18
|
+
|
|
19
|
+
check(itemData, "Item not found in database.");
|
|
20
|
+
|
|
21
|
+
let user = await findUser(req);
|
|
22
|
+
let inv = user.memory.data.inventory;
|
|
23
|
+
|
|
24
|
+
let holding = false;
|
|
25
|
+
|
|
26
|
+
for (let j = 0; j < inv.length; j++) {
|
|
27
|
+
if (inv[j].name === req.body.data.name) {
|
|
28
|
+
holding = true;
|
|
29
|
+
|
|
30
|
+
if (inv[j].amount >= -Math.floor(req.body.data.amount)) {
|
|
31
|
+
inv[j].amount += Math.floor(req.body.data.amount);
|
|
32
|
+
|
|
33
|
+
if (inv[j].amount === 0) {
|
|
34
|
+
let empty = inv[j];
|
|
35
|
+
|
|
36
|
+
inv.splice(j, 1);
|
|
37
|
+
await user.commit();
|
|
38
|
+
|
|
39
|
+
return success("Item emptied.", empty);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
await user.commit();
|
|
43
|
+
|
|
44
|
+
return success("Item offset.", inv[j]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
return fail(`Unable to remove ${req.body.data.amount} ${req.body.data.name}
|
|
49
|
+
from inventory because the inventory has only ${inv[j].amount}.` );
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!holding) {
|
|
55
|
+
if (req.body.data.amount > 0) {
|
|
56
|
+
inv.push({
|
|
57
|
+
name: req.body.data.name,
|
|
58
|
+
amount: req.body.data.amount
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await user.commit();
|
|
62
|
+
|
|
63
|
+
return success("Item offset.", inv[inv.length - 1]);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return fail(`Unable to remove ${req.body.data.amount} ${req.body.data.name}
|
|
67
|
+
from inventory because the inventory has none.`);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { success, check } from "./util.js";
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
getAllItems: async (req) => {
|
|
5
|
+
let items = new req.db.Item();
|
|
6
|
+
let all = await items.getAll();
|
|
7
|
+
|
|
8
|
+
check(all, "Items not found.");
|
|
9
|
+
|
|
10
|
+
return success("Found items.", all);
|
|
11
|
+
},
|
|
12
|
+
getItem: async (req) => {
|
|
13
|
+
let item = new req.db.Item(req.body.data.name);
|
|
14
|
+
|
|
15
|
+
let itemData = await item.recall();
|
|
16
|
+
|
|
17
|
+
check(itemData, `Item not found: ${req.body.data.name}`);
|
|
18
|
+
|
|
19
|
+
return success("Found item.", itemData);
|
|
20
|
+
},
|
|
21
|
+
setItem: async (req) => {
|
|
22
|
+
let item = new req.db.Item(req.body.data.name);
|
|
23
|
+
|
|
24
|
+
await item.commit(req.body.data);
|
|
25
|
+
|
|
26
|
+
return success();
|
|
27
|
+
},
|
|
28
|
+
newItem: async (req) => {
|
|
29
|
+
let item = new req.db.Item(req.body.data.name);
|
|
30
|
+
|
|
31
|
+
await item.create(req.body.data);
|
|
32
|
+
|
|
33
|
+
return success();
|
|
34
|
+
},
|
|
35
|
+
deleteItem: async (req) => {
|
|
36
|
+
let item = new req.db.Item(req.body.data.name);
|
|
37
|
+
item.delete();
|
|
38
|
+
|
|
39
|
+
return success("Item deleted.");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { findUser, success, fail } from "./util.js";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
|
|
4
|
+
let serve = async (req, scriptPath) => {
|
|
5
|
+
let script, result = null;
|
|
6
|
+
|
|
7
|
+
if (fs.existsSync(scriptPath)) {
|
|
8
|
+
let user = findUser(req);
|
|
9
|
+
|
|
10
|
+
script = await import(scriptPath);
|
|
11
|
+
result = await script.default(req, user);
|
|
12
|
+
return success("Served.", result);
|
|
13
|
+
}
|
|
14
|
+
else return fail(`Script not found: ${req.body.data.script} at ${scriptPath}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default {
|
|
18
|
+
serveExplorer: async (req) => {
|
|
19
|
+
let scriptPath = `${req.contentPath}/explorer${req.body.route}/server-scripts/${req.body.data.script}.js`;
|
|
20
|
+
|
|
21
|
+
return serve(req, scriptPath);
|
|
22
|
+
},
|
|
23
|
+
serveFront: async (req) => {
|
|
24
|
+
let scriptPath = `${req.contentPath}/the-front/server-scripts/${req.body.data.script}.js`;
|
|
25
|
+
|
|
26
|
+
return serve(req, scriptPath);
|
|
27
|
+
},
|
|
28
|
+
servePages: async (req) => {
|
|
29
|
+
let scriptPath = `${req.contentPath}/pages/server-scripts/${req.body.data.script}.js`;
|
|
30
|
+
|
|
31
|
+
return serve(req, scriptPath);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import bcrypt from "bcrypt";
|
|
2
|
+
import {loginCheck, check, findUser, fail, success} from "./util.js";
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
logout: async (req) => {
|
|
6
|
+
loginCheck(req);
|
|
7
|
+
|
|
8
|
+
await req.session.destroy();
|
|
9
|
+
|
|
10
|
+
return success("Logged out.");
|
|
11
|
+
},
|
|
12
|
+
sendPasswordReset: async (req) => {
|
|
13
|
+
let reset = new req.db.User("reset");
|
|
14
|
+
|
|
15
|
+
let token = Math.floor(Math.random() * 9999);
|
|
16
|
+
|
|
17
|
+
await reset.create({
|
|
18
|
+
email: req.body.data.email,
|
|
19
|
+
token: token,
|
|
20
|
+
tokenExp: Date.now() + (1000 * 60 * 30)
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
req.db.SendMail.passwordReset(req.body.data.email, token);
|
|
24
|
+
|
|
25
|
+
return success("Password reset.", {});
|
|
26
|
+
},
|
|
27
|
+
changePassword: async (req) => {
|
|
28
|
+
let reset = new req.db.User("reset");
|
|
29
|
+
let resetData = await reset.recallFromData("token", req.body.data.token);
|
|
30
|
+
|
|
31
|
+
check(resetData, "Reset token not valid!");
|
|
32
|
+
check(resetData.tokenExp < Date.now(), "Reset token expired!");
|
|
33
|
+
check(req.body.data.password !== req.body.data.confirmation, "Passwords must match!");
|
|
34
|
+
|
|
35
|
+
const salt = await bcrypt.genSalt(10);
|
|
36
|
+
const hash = await bcrypt.hash(req.body.data.password, salt);
|
|
37
|
+
|
|
38
|
+
let user = new req.db.User("user");
|
|
39
|
+
let userData = (await user.recallFromData("email", resetData.email)).data;
|
|
40
|
+
|
|
41
|
+
userData.password = hash;
|
|
42
|
+
await user.commit(userData);
|
|
43
|
+
|
|
44
|
+
await reset.delete();
|
|
45
|
+
|
|
46
|
+
return success("Password changed successfully!");
|
|
47
|
+
},
|
|
48
|
+
register: async (req) => {
|
|
49
|
+
check(req.body.data.password.length > 7, "Password too short.");
|
|
50
|
+
check(req.body.data.email.length > 7, "Email too short.");
|
|
51
|
+
check(req.body.data.username.length > 2, "Username too short.");
|
|
52
|
+
|
|
53
|
+
let user = new req.db.User("user", req.body.data.email);
|
|
54
|
+
let userData = await user.recall();
|
|
55
|
+
|
|
56
|
+
check(!userData, "Email already in use!");
|
|
57
|
+
|
|
58
|
+
const salt = await bcrypt.genSalt(10);
|
|
59
|
+
const hash = await bcrypt.hash(req.body.data.password, salt);
|
|
60
|
+
|
|
61
|
+
await user.create({
|
|
62
|
+
username: req.body.data.username,
|
|
63
|
+
password: hash,
|
|
64
|
+
email: req.body.data.email,
|
|
65
|
+
coin: 0,
|
|
66
|
+
home: "/",
|
|
67
|
+
authLevels: [ "Basic" ],
|
|
68
|
+
location: "/the-front",
|
|
69
|
+
attributes: {
|
|
70
|
+
translation: 0,
|
|
71
|
+
strength: 0,
|
|
72
|
+
agility: 0,
|
|
73
|
+
defense: 0
|
|
74
|
+
},
|
|
75
|
+
inventory: []
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return success("Registration successful!");
|
|
79
|
+
},
|
|
80
|
+
login: async (req) => {
|
|
81
|
+
let user = await findUser(req, req.body.data.email);
|
|
82
|
+
|
|
83
|
+
let passResult = await bcrypt.compare(req.body.data.password, user.memory.data.password);
|
|
84
|
+
check(passResult, "Password doesn't match.");
|
|
85
|
+
|
|
86
|
+
req.session.currentUser = req.body.data.email;
|
|
87
|
+
|
|
88
|
+
return success("Logged in.");
|
|
89
|
+
},
|
|
90
|
+
changeUserEmail: async (req) => {
|
|
91
|
+
loginCheck(req);
|
|
92
|
+
|
|
93
|
+
let user = new req.db.User("user", req.body.data.email);
|
|
94
|
+
let userData = await user.recall();
|
|
95
|
+
|
|
96
|
+
check(!userData, "Email already in use!");
|
|
97
|
+
|
|
98
|
+
user = await findUser(req);
|
|
99
|
+
|
|
100
|
+
user.memory.data.email = req.body.data.email;
|
|
101
|
+
await user.commit();
|
|
102
|
+
|
|
103
|
+
req.session.currentUser = req.body.data.email;
|
|
104
|
+
|
|
105
|
+
return success();
|
|
106
|
+
},
|
|
107
|
+
changeUsername: async (req) => {
|
|
108
|
+
loginCheck(req);
|
|
109
|
+
|
|
110
|
+
let user = new req.db.User("user");
|
|
111
|
+
let userData = await user.recallFromData("username", req.body.data.username);
|
|
112
|
+
check(!userData, "Username already in use!");
|
|
113
|
+
|
|
114
|
+
user = await findUser(req);
|
|
115
|
+
|
|
116
|
+
user.memory.data.username = req.body.data.username;
|
|
117
|
+
await user.commit();
|
|
118
|
+
|
|
119
|
+
return success();
|
|
120
|
+
},
|
|
121
|
+
deleteUserPermanently: async (req) => {
|
|
122
|
+
loginCheck(req);
|
|
123
|
+
|
|
124
|
+
await user.findOneAndDelete().where("data.email").equals(req.session.currentUser);
|
|
125
|
+
await req.session.destroy();
|
|
126
|
+
|
|
127
|
+
return success("Account deleted.");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export let loginCheck = (req) => {
|
|
2
|
+
check(req.session.currentUser, "Please login first.");
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export let findUser = async (req, email = req.session.currentUser) => {
|
|
6
|
+
let user = new req.db.User("user", email);
|
|
7
|
+
let userData = await user.recall();
|
|
8
|
+
|
|
9
|
+
check(userData, "User not found.");
|
|
10
|
+
|
|
11
|
+
return user;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export let check = (checkee, failMsg) => {
|
|
15
|
+
if (!checkee) throw {
|
|
16
|
+
status: "failed",
|
|
17
|
+
message: failMsg,
|
|
18
|
+
isUpToDate: true,
|
|
19
|
+
data: null
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export let success = (msg = "Update successful.", data = null, isUpToDate = false) => {
|
|
24
|
+
return {
|
|
25
|
+
status: "success",
|
|
26
|
+
message: msg,
|
|
27
|
+
isUpToDate: isUpToDate,
|
|
28
|
+
data: data
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export let fail = (msg, data = null, isUpToDate = true) => {
|
|
33
|
+
return {
|
|
34
|
+
status: "failed",
|
|
35
|
+
message: msg,
|
|
36
|
+
isUpToDate: isUpToDate,
|
|
37
|
+
data: data
|
|
38
|
+
}
|
|
39
|
+
}
|