notherbase-fs 2.0.3 → 3.0.0
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 +57 -59
- package/controllers/spirit-world.js +55 -74
- package/controllers/spirits/contact.js +17 -10
- package/controllers/spirits/user.js +144 -112
- package/controllers/spirits/util.js +23 -31
- package/models/index.js +54 -117
- package/models/item.js +80 -0
- package/models/send-mail.js +2 -0
- package/models/spirit.js +77 -115
- package/models/user.js +189 -0
- package/notherbase-fs.js +1 -0
- package/package.json +1 -1
- package/public/styles/main.css +12 -3
- package/test/{explorer/coast/tall-beach/nono-cove/styles → coast/tall-beach/nono-cove}/index.css +0 -0
- package/test/coast/tall-beach/nono-cove/index.ejs +29 -0
- package/test/{explorer/coast/tall-beach/nono-cove/views/nono-og.ejs → coast/tall-beach/nono-cove/nono-og/index.ejs} +2 -2
- package/test/{explorer/coast/tall-beach/nono-cove/styles → coast/tall-beach/nono-cove/nono-og}/nono.css +0 -0
- package/test/{explorer/coast/tall-beach/nono-cove/local-scripts → coast/tall-beach/nono-cove/nono-og}/nono.js +1 -1
- package/test/{explorer/coast/tall-beach/nono-cove/server-scripts → test-page}/emailTime.js +0 -0
- package/test/{pages/test.ejs → test-page/index.ejs} +0 -0
- package/test/the-front/add-gold.js +35 -0
- package/test/the-front/check/add-more-gold.js +3 -0
- package/test/the-front/check/emailTime.js +11 -0
- package/test/the-front/check/index.ejs +13 -0
- package/test/the-front/check/subtract-gold.js +3 -0
- package/test/the-front/checks.js +11 -0
- package/test/the-front/incTranslation.js +9 -0
- package/test/the-front/index.ejs +107 -0
- package/test/void/index.ejs +4 -0
- package/test/void/void.css +3 -0
- package/views/explorer.ejs +32 -41
- package/views/menu/account.ejs +39 -0
- package/views/menu/inventory.ejs +8 -0
- package/views/menu/more.ejs +5 -0
- package/views/menu/player.ejs +3 -0
- package/views/menu.ejs +20 -62
- package/views/scripts/base.js +314 -0
- package/controllers/spirits/attribute.js +0 -47
- package/controllers/spirits/inventory.js +0 -71
- package/controllers/spirits/item.js +0 -41
- package/controllers/spirits/serve.js +0 -33
- package/public/js/commune.js +0 -22
- package/public/js/establishment.js +0 -44
- package/public/js/memories.js +0 -44
- package/test/explorer/coast/tall-beach/nono-cove/local-scripts/game.js +0 -7
- package/test/explorer/coast/tall-beach/nono-cove/local-scripts/items.js +0 -162
- package/test/explorer/coast/tall-beach/nono-cove/server-scripts/getTimer.js +0 -31
- package/test/explorer/coast/tall-beach/nono-cove/styles/items-floor.css +0 -92
- package/test/explorer/coast/tall-beach/nono-cove/views/index.ejs +0 -130
- package/test/pages/server-scripts/emailTime.js +0 -15
- package/test/the-front/server-scripts/emailTime.js +0 -15
- package/test/the-front/server-scripts/getTimer.js +0 -31
- package/test/the-front/views/check.ejs +0 -93
- package/test/the-front/views/index.ejs +0 -113
- package/test/void/styles/void.css +0 -0
- package/views/account.ejs +0 -122
- package/views/inventory.ejs +0 -117
- package/views/more.ejs +0 -16
- package/views/player.ejs +0 -60
package/controllers/creation.js
CHANGED
|
@@ -5,30 +5,75 @@ export default class Creation {
|
|
|
5
5
|
constructor() {
|
|
6
6
|
this.router = express.Router();
|
|
7
7
|
|
|
8
|
+
//home
|
|
8
9
|
this.router.get("/", function(req, res) { res.redirect("/the-front"); });
|
|
9
|
-
|
|
10
|
-
this.router.get(`/the-front
|
|
11
|
-
this.router.get(
|
|
12
|
-
|
|
13
|
-
this.router.get(`/:
|
|
14
|
-
|
|
10
|
+
//the-front
|
|
11
|
+
this.router.get(`/the-front`, this.explore);
|
|
12
|
+
this.router.get(`/the-front/:frontDetail`, this.explore);
|
|
13
|
+
//pages
|
|
14
|
+
this.router.get(`/:page`, this.explore);
|
|
15
|
+
//explorer
|
|
16
|
+
this.router.get(`/:region/:area/:poi`, this.lock, this.explore);
|
|
17
|
+
this.router.get(`/:region/:area/:poi/:detail`, this.lock, this.explore);
|
|
18
|
+
//void
|
|
19
|
+
this.router.use(function(req, res) {
|
|
20
|
+
console.log(req.path);
|
|
21
|
+
res.redirect("/void");
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
lock = (req, res, next) => {
|
|
26
|
+
req.lock = true;
|
|
27
|
+
next();
|
|
15
28
|
}
|
|
16
29
|
|
|
17
|
-
explore = async (
|
|
30
|
+
explore = async (req, res, next) => {
|
|
31
|
+
let main = `${req.contentPath}`;
|
|
32
|
+
let siteTitle = `NotherBase - `;
|
|
33
|
+
|
|
34
|
+
if (req.params.frontDetail) {
|
|
35
|
+
main += `/the-front/${req.params.frontDetail}/index`;
|
|
36
|
+
siteTitle += req.params.frontDetail;
|
|
37
|
+
}
|
|
38
|
+
else if (req.params.detail) {
|
|
39
|
+
main += `/${req.params.region}/${req.params.area}/${req.params.poi}/${req.params.detail}/index`;
|
|
40
|
+
siteTitle += req.params.detail;
|
|
41
|
+
}
|
|
42
|
+
else if (req.params.poi) {
|
|
43
|
+
main += `/${req.params.region}/${req.params.area}/${req.params.poi}/index`;
|
|
44
|
+
siteTitle += req.params.poi;
|
|
45
|
+
}
|
|
46
|
+
else if (req.params.page) {
|
|
47
|
+
main += `/${req.params.page}/index`;
|
|
48
|
+
siteTitle = req.params.page;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
main += `/the-front/index`;
|
|
52
|
+
siteTitle += "the-front";
|
|
53
|
+
}
|
|
54
|
+
|
|
18
55
|
try {
|
|
19
56
|
if (fs.existsSync(main + ".ejs")) {
|
|
20
|
-
let user =
|
|
21
|
-
|
|
57
|
+
let user = await req.db.User.recallOne(req.session.currentUser);
|
|
58
|
+
|
|
59
|
+
let userStuff = {
|
|
60
|
+
userID: null,
|
|
61
|
+
user: null,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (user) userStuff = {
|
|
65
|
+
userID: user.id,
|
|
66
|
+
user: user.memory.data,
|
|
67
|
+
};
|
|
22
68
|
|
|
23
69
|
let context = {
|
|
70
|
+
...userStuff,
|
|
24
71
|
siteTitle: siteTitle,
|
|
25
|
-
userID: user._ID,
|
|
26
|
-
user: userData,
|
|
27
72
|
main: main,
|
|
28
73
|
query: req.query,
|
|
29
74
|
dir: req.frontDir,
|
|
30
75
|
route: req.path,
|
|
31
|
-
requireUser:
|
|
76
|
+
requireUser: req.lock
|
|
32
77
|
}
|
|
33
78
|
|
|
34
79
|
res.render(`explorer`, context);
|
|
@@ -40,51 +85,4 @@ export default class Creation {
|
|
|
40
85
|
res.status(500).end();
|
|
41
86
|
}
|
|
42
87
|
}
|
|
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
88
|
}
|
|
@@ -1,62 +1,12 @@
|
|
|
1
1
|
import express from "express";
|
|
2
2
|
import { stripHtml } from "string-strip-html";
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import attribute from "./spirits/attribute.js";
|
|
8
|
-
import user from "./spirits/user.js";
|
|
9
|
-
import { check, findUser, loginCheck, success } from "./spirits/util.js";
|
|
3
|
+
import User from "./spirits/user.js";
|
|
4
|
+
import Contact from "./spirits/contact.js";
|
|
5
|
+
import { success, fail } from "./spirits/util.js";
|
|
6
|
+
import fs from 'fs';
|
|
10
7
|
|
|
11
8
|
export default class SpiritWorld {
|
|
12
|
-
|
|
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
|
-
console.log(req.body);
|
|
32
|
-
|
|
33
|
-
/* req.body {
|
|
34
|
-
action: "getUserBasic",
|
|
35
|
-
route: "/something" (opt),
|
|
36
|
-
service: "something" (opt),
|
|
37
|
-
scope: "local" (opt),
|
|
38
|
-
parent: "id" (opt),
|
|
39
|
-
_lastUpdate: 0 (opt),
|
|
40
|
-
data: {} (opt)
|
|
41
|
-
} */
|
|
42
|
-
|
|
43
|
-
try {
|
|
44
|
-
if (this[req.body.action]) result = await this[req.body.action](req);
|
|
45
|
-
else result = {
|
|
46
|
-
status: "failed",
|
|
47
|
-
message: `No function with the name ${req.body.action}`,
|
|
48
|
-
isUpToDate: true,
|
|
49
|
-
data: {}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
res.send(result);
|
|
53
|
-
} catch (error) {
|
|
54
|
-
console.log(error);
|
|
55
|
-
res.send(error);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
setupChat = (socket) => {
|
|
9
|
+
#setupChat = (socket) => {
|
|
60
10
|
socket.join(socket.handshake.query.room);
|
|
61
11
|
|
|
62
12
|
this.io.to(socket.handshake.query.room).emit("chat message", {
|
|
@@ -82,31 +32,62 @@ export default class SpiritWorld {
|
|
|
82
32
|
});
|
|
83
33
|
}
|
|
84
34
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
35
|
+
constructor(io) {
|
|
36
|
+
this.io = io;
|
|
37
|
+
this.router = express.Router();
|
|
38
|
+
this.user = new User();
|
|
39
|
+
this.contact = new Contact();
|
|
91
40
|
|
|
92
|
-
|
|
93
|
-
|
|
41
|
+
this.router.post("/serve", this.serve);
|
|
42
|
+
this.router.get("/load", this.load);
|
|
43
|
+
this.router.use("/user", this.user.router);
|
|
44
|
+
this.router.use("/contact-nother", this.contact.router);
|
|
94
45
|
|
|
95
|
-
|
|
46
|
+
this.io.on('connection', this.#setupChat);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
load = async (req, res) => {
|
|
50
|
+
let user = await req.db.User.recallOne(req.session.currentUser);
|
|
51
|
+
|
|
52
|
+
let parent = null;
|
|
53
|
+
if (req.query.scope === "local" && user) parent = user.id;
|
|
54
|
+
|
|
55
|
+
let spirit = await req.db.Spirit.recallOne({
|
|
56
|
+
route: req.query.route,
|
|
57
|
+
service: req.query.service,
|
|
58
|
+
scope: req.query.scope,
|
|
59
|
+
parent: parent
|
|
60
|
+
});
|
|
96
61
|
|
|
97
|
-
|
|
62
|
+
if (!spirit) spirit = await req.db.Spirit.create({
|
|
63
|
+
route: req.query.route,
|
|
64
|
+
service: req.query.service,
|
|
65
|
+
scope: req.query.scope,
|
|
66
|
+
parent: parent
|
|
67
|
+
}, {});
|
|
68
|
+
|
|
69
|
+
if (!spirit.memory.data) spirit.memory.data = {};
|
|
70
|
+
|
|
71
|
+
res.send(spirit.memory.data);
|
|
98
72
|
}
|
|
99
73
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
74
|
+
serve = async (req, res) => {
|
|
75
|
+
try {
|
|
76
|
+
let scriptPath = `${req.contentPath}${req.body.route}/${req.body.data.script}.js`;
|
|
77
|
+
|
|
78
|
+
let script, result = null;
|
|
106
79
|
|
|
107
|
-
|
|
108
|
-
|
|
80
|
+
if (fs.existsSync(scriptPath)) {
|
|
81
|
+
let user = await req.db.User.recallOne(req.session.currentUser);
|
|
109
82
|
|
|
110
|
-
|
|
83
|
+
script = await import(scriptPath);
|
|
84
|
+
result = await script.default(req, user);
|
|
85
|
+
success(res, "Served.", result);
|
|
86
|
+
}
|
|
87
|
+
else fail(res, `Script not found: ${req.body.data.script} at ${scriptPath}`);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.log(error);
|
|
90
|
+
fail(res, "Server error");
|
|
91
|
+
}
|
|
111
92
|
}
|
|
112
93
|
}
|
|
@@ -1,16 +1,23 @@
|
|
|
1
|
+
import express from "express";
|
|
1
2
|
import { success } from "./util.js";
|
|
2
3
|
|
|
3
|
-
export default {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let contact = new req.db.Spirit(req.body);
|
|
4
|
+
export default class Contact {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.router = express.Router();
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
this.router.post(`/`, this.contactNother);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
contactNother = async function(req, res) {
|
|
12
|
+
let user = await User.recallOne(req.session.currentUser);
|
|
13
|
+
|
|
14
|
+
let spirit = await req.db.Spirit.create({
|
|
15
|
+
route: "/",
|
|
16
|
+
service: "nother-contacts",
|
|
17
|
+
scope: "local",
|
|
18
|
+
parent: user.id
|
|
19
|
+
}, req.body);
|
|
13
20
|
|
|
14
|
-
|
|
21
|
+
success(res, "Message sent.");
|
|
15
22
|
}
|
|
16
23
|
}
|
|
@@ -1,130 +1,162 @@
|
|
|
1
|
+
import express from "express";
|
|
1
2
|
import bcrypt from "bcrypt";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export default {
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { check, success, fail, loginCheck } from "./util.js";
|
|
4
|
+
|
|
5
|
+
export default class User {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.router = express.Router();
|
|
8
|
+
|
|
9
|
+
this.router.post("/logout", this.logout);
|
|
10
|
+
this.router.post("/sendPasswordReset", this.sendPasswordReset);
|
|
11
|
+
this.router.post("/changePassword", this.changePassword);
|
|
12
|
+
this.router.post("/register", this.register);
|
|
13
|
+
this.router.post("/login", this.login);
|
|
14
|
+
this.router.post("/changeEmail", this.changeEmail);
|
|
15
|
+
this.router.post("/changeUsername", this.changeUsername);
|
|
16
|
+
this.router.post("/deletePermanently", this.deletePermanently);
|
|
17
|
+
this.router.post("/getInventory", this.getInventory);
|
|
18
|
+
this.router.post("/getAttributes", this.getAttributes);
|
|
19
|
+
}
|
|
7
20
|
|
|
21
|
+
logout = async (req, res) => {
|
|
8
22
|
await req.session.destroy();
|
|
9
23
|
|
|
10
|
-
|
|
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);
|
|
24
|
+
success(res, "Logged out.");
|
|
25
|
+
}
|
|
82
26
|
|
|
83
|
-
|
|
84
|
-
|
|
27
|
+
sendPasswordReset = async (req, res) => {
|
|
28
|
+
let spirit = await req.db.User.recallOne(req.body.data.email);
|
|
85
29
|
|
|
86
|
-
|
|
30
|
+
if (spirit) {
|
|
31
|
+
let token = Math.floor(Math.random() * 9999);
|
|
87
32
|
|
|
88
|
-
|
|
89
|
-
},
|
|
90
|
-
changeUserEmail: async (req) => {
|
|
91
|
-
loginCheck(req);
|
|
33
|
+
if (req.body.data.test) console.log("token: " + token);
|
|
92
34
|
|
|
93
|
-
|
|
94
|
-
|
|
35
|
+
spirit.memory.data.resetToken = token;
|
|
36
|
+
spirit.memory.data.resetExp = Date.now() + (1000 * 60 * 10);
|
|
37
|
+
await spirit.commit();
|
|
38
|
+
|
|
39
|
+
req.db.SendMail.passwordReset(req.body.data.email, token);
|
|
40
|
+
|
|
41
|
+
success(res, "Password reset token sent.");
|
|
42
|
+
}
|
|
43
|
+
else fail(res, "User not found.");
|
|
44
|
+
}
|
|
95
45
|
|
|
96
|
-
|
|
46
|
+
changePassword = async (req, res) => {
|
|
47
|
+
if (check(res, req.body.data.token, "No token provided!")){
|
|
48
|
+
let spirit = await req.db.User.recallOne(req.body.data.email);
|
|
49
|
+
|
|
50
|
+
if (check(res, spirit, "User not found!") &&
|
|
51
|
+
check(res, spirit.memory.data.resetToken == req.body.data.token, "Reset token not valid!") &&
|
|
52
|
+
check(res, spirit.memory.data.resetExp > Date.now(), "Reset token expired!") &&
|
|
53
|
+
check(res, req.body.data.password === req.body.data.confirmation, "Passwords must match!"))
|
|
54
|
+
{
|
|
55
|
+
spirit.memory.data.resetExp = -1;
|
|
56
|
+
|
|
57
|
+
const salt = await bcrypt.genSalt(10);
|
|
58
|
+
const hash = await bcrypt.hash(req.body.data.password, salt);
|
|
59
|
+
|
|
60
|
+
spirit.memory.data.password = hash;
|
|
61
|
+
await spirit.commit();
|
|
62
|
+
|
|
63
|
+
success(res, "Password changed successfully!");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
97
67
|
|
|
98
|
-
|
|
68
|
+
register = async (req, res) => {
|
|
69
|
+
if (check(res, req.body.data.password.length > 7, "Password too short.") &&
|
|
70
|
+
check(res, req.body.data.email.length > 7, "Email too short.") &&
|
|
71
|
+
check(res, req.body.data.username.length > 2, "Username too short."))
|
|
72
|
+
{
|
|
73
|
+
let spirit = await req.db.User.recallOne(req.body.data.email);
|
|
74
|
+
|
|
75
|
+
if (check(res, !spirit, "Email already in use!")) {
|
|
76
|
+
spirit = await req.db.User.create(req.body.data.username, req.body.data.password, req.body.data.email);
|
|
77
|
+
|
|
78
|
+
success(res, "Registration successful!");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
99
82
|
|
|
100
|
-
|
|
101
|
-
await
|
|
83
|
+
login = async (req, res) => {
|
|
84
|
+
let spirit = await req.db.User.recallOne(req.body.data.email);
|
|
85
|
+
if (check(res, spirit, "User not found.")) {
|
|
86
|
+
let passResult = await bcrypt.compare(req.body.data.password, spirit.memory.data.password);
|
|
87
|
+
|
|
88
|
+
if (check(res, passResult, "Password doesn't match the email.")) {
|
|
89
|
+
req.session.currentUser = req.body.data.email;
|
|
90
|
+
|
|
91
|
+
success(res, "Logged in.", spirit.memory.data.username);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
102
95
|
|
|
103
|
-
|
|
96
|
+
changeEmail = async (req, res) => {
|
|
97
|
+
if (loginCheck(req, res)) {
|
|
98
|
+
let spirit = await req.db.User.recallOne(req.body.data.email);
|
|
99
|
+
|
|
100
|
+
if (check(res, !spirit, "Email already in use!")) {
|
|
101
|
+
spirit = await req.db.User.recallOne(req.session.currentUser);
|
|
102
|
+
|
|
103
|
+
spirit.memory.data.email = req.body.data.email;
|
|
104
|
+
await spirit.commit();
|
|
105
|
+
|
|
106
|
+
req.session.currentUser = req.body.data.email;
|
|
107
|
+
|
|
108
|
+
success(res, "Email changed.");
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
104
112
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
+
changeUsername = async (req, res) => {
|
|
114
|
+
if (loginCheck(req, res)) {
|
|
115
|
+
let spirit = await req.db.User.recallOne(null, req.body.data.username);
|
|
116
|
+
|
|
117
|
+
if (check(res, !spirit, "Username already in use!")) {
|
|
118
|
+
spirit = await req.db.User.recallOne(req.session.currentUser);
|
|
119
|
+
|
|
120
|
+
spirit.memory.data.username = req.body.data.username;
|
|
121
|
+
await spirit.commit();
|
|
122
|
+
|
|
123
|
+
success(res, "Username changed");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
113
127
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
},
|
|
121
|
-
deleteUserPermanently: async (req) => {
|
|
122
|
-
loginCheck(req);
|
|
128
|
+
deletePermanently = async (req, res) => {
|
|
129
|
+
if (loginCheck(req, res)) {
|
|
130
|
+
let deleted = await req.db.User.delete(req.session.currentUser);
|
|
131
|
+
|
|
132
|
+
if (check(res, deleted > 0, "No account deleted")) {
|
|
133
|
+
await req.session.destroy();
|
|
123
134
|
|
|
124
|
-
|
|
125
|
-
|
|
135
|
+
success(res, "Account deleted.");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
126
139
|
|
|
127
|
-
|
|
140
|
+
getInventory = async (req, res) => {
|
|
141
|
+
if (loginCheck(req, res)) {
|
|
142
|
+
let spirit = await req.db.User.recallOne(req.session.currentUser);
|
|
143
|
+
if (spirit.memory._lastUpdate > req.body._lastUpdate) {
|
|
144
|
+
let inv = spirit.memory.data.inventory;
|
|
145
|
+
|
|
146
|
+
success(res, "Inventory found", inv, spirit.memory._lastUpdate);
|
|
147
|
+
}
|
|
148
|
+
else fail(res, "Inventory up to date.");
|
|
149
|
+
}
|
|
128
150
|
}
|
|
129
|
-
}
|
|
130
151
|
|
|
152
|
+
getAttributes = async (req, res) => {
|
|
153
|
+
if (loginCheck(req, res)) {
|
|
154
|
+
let user = await req.db.User.recallOne(req.session.currentUser);
|
|
155
|
+
|
|
156
|
+
if (user.memory._lastUpdate > req.body._lastUpdate) {
|
|
157
|
+
success(res, "Attributes found", user.memory.data.attributes);
|
|
158
|
+
}
|
|
159
|
+
else fail(res, "Attributes up to date.");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -1,39 +1,31 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
}
|
|
1
|
+
export const check = (res, checkee, failMsg = "Failed!") => {
|
|
2
|
+
if (!checkee) {
|
|
3
|
+
fail(res, failMsg);
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
else {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
22
10
|
|
|
23
|
-
export
|
|
24
|
-
|
|
11
|
+
export const success = (res, msg = "Success!", data = null, lastUpdate = 0) => {
|
|
12
|
+
res.send({
|
|
25
13
|
status: "success",
|
|
26
14
|
message: msg,
|
|
27
|
-
|
|
15
|
+
lastUpdate: lastUpdate,
|
|
28
16
|
data: data
|
|
29
|
-
};
|
|
30
|
-
}
|
|
17
|
+
});
|
|
18
|
+
};
|
|
31
19
|
|
|
32
|
-
export
|
|
33
|
-
|
|
20
|
+
export const fail = (res, msg = "Failed!") => {
|
|
21
|
+
res.send({
|
|
34
22
|
status: "failed",
|
|
35
23
|
message: msg,
|
|
36
|
-
|
|
37
|
-
data:
|
|
38
|
-
}
|
|
24
|
+
lastUpdate: 0,
|
|
25
|
+
data: null
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const loginCheck = (req, res) => {
|
|
30
|
+
return check(res, req.session.currentUser, "Please login first.");
|
|
39
31
|
}
|