a bit timeout

This commit is contained in:
Primakov Alexandr Alexandrovich 2022-04-18 21:42:20 +03:00
parent 2e159814e4
commit 544046f43d
14 changed files with 149 additions and 1 deletions

1
dist/config.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export {};

4
dist/config.js vendored Normal file
View File

@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv_1 = require("dotenv");
(0, dotenv_1.config)();

1
dist/main.d.ts vendored Normal file
View File

@ -0,0 +1 @@
import './config';

19
dist/main.js vendored Normal file
View File

@ -0,0 +1,19 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const cookie_session_1 = __importDefault(require("cookie-session"));
require("./config");
const error_handling_1 = require("./utils/error-handling");
const routes_1 = require("./routes");
const app = (0, express_1.default)();
const port = process.env.RED_CODER_BH_PORT;
app.use(express_1.default.json());
app.use((0, cookie_session_1.default)({ secret: process.env.COOKIE_SESSION }));
app.use(routes_1.router);
app.use(error_handling_1.errorHandle);
app.listen(port, () => {
console.log(`listen on http://localhost:${port}`);
});

1
dist/routes/auth.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare const authRouter: import("express-serve-static-core").Router;

68
dist/routes/auth.js vendored Normal file
View File

@ -0,0 +1,68 @@
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.authRouter = void 0;
const express_1 = require("express");
const pbkdf2_password_1 = __importDefault(require("pbkdf2-password"));
const uuid_1 = require("uuid");
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const express_jwt_1 = __importDefault(require("express-jwt"));
const makeHash = (0, pbkdf2_password_1.default)();
exports.authRouter = (0, express_1.Router)();
const requiredFields = (fields) => (req, res, next) => {
for (const fieldName of fields) {
if (!req.body[fieldName]) {
throw new Error(`Field ${fieldName} does\'t set`);
}
}
next();
};
const users = [];
exports.authRouter.get('/users', (0, express_jwt_1.default)({ secret: process.env.JWT_SECRET_STRING, algorithms: ['HS256'] }), (req, res) => {
res.send(users);
});
exports.authRouter.post('/sign-in', requiredFields(['password', 'login']), (req, res) => {
const { password, login } = req.body;
const user = users.find(u => u.login === login);
if (!user) {
res.status(400).send({ error: 'Login or password does\'t match' });
return;
}
makeHash({ password, salt: user.salt }, (err, pass, salt, hash) => {
if (err)
throw err;
if (user.hash === hash) {
const { hash: _hash, salt: _salt } = user, cleanUser = __rest(user, ["hash", "salt"]);
req.session.user = cleanUser;
const token = jsonwebtoken_1.default.sign(cleanUser, process.env.JWT_SECRET_STRING, {});
return res.send({ token, user: cleanUser });
}
res.status(400).send({ error: 'Login or password does\'t match' });
});
});
exports.authRouter.post('/sign-up', requiredFields(['password', 'login', 'email']), (req, res, next) => {
const _a = req.body, { password, login } = _a, rest = __rest(_a, ["password", "login"]);
makeHash({ password }, function (err, pass, salt, hash) {
if (err)
throw err;
const newUser = Object.assign(Object.assign({ id: (0, uuid_1.v4)() }, rest), { login,
salt,
hash });
users.push(newUser);
const { hash: _hash, salt: _salt } = newUser, cleanUser = __rest(newUser, ["hash", "salt"]);
res.send(cleanUser);
});
});

19
dist/routes/banner/data.json vendored Normal file
View File

@ -0,0 +1,19 @@
{
"data": [
{
"title": "Подсказка на сегодня",
"body": "Подберите задачки под свой уровень сложности, решите её и наращивайте навык",
"icon": "allGood"
},
{
"title": "Подсказка на завтра",
"body": "Не забудь сутра выпить кофе",
"icon": "coding"
},
{
"title": "Подсказка на вчера",
"body": "Забыл сутра выпить кофе?",
"icon": "askingQuestion"
}
]
}

1
dist/routes/banner/index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare const bannerRouter: import("express-serve-static-core").Router;

12
dist/routes/banner/index.js vendored Normal file
View File

@ -0,0 +1,12 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bannerRouter = void 0;
const express_1 = require("express");
const data_json_1 = __importDefault(require("./data.json"));
exports.bannerRouter = (0, express_1.Router)();
exports.bannerRouter.get('/banner-data', (req, res) => {
res.send(data_json_1.default);
});

1
dist/routes/index.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare const router: import("express-serve-static-core").Router;

9
dist/routes/index.js vendored Normal file
View File

@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.router = void 0;
const express_1 = require("express");
const auth_1 = require("./auth");
const banner_1 = require("./banner");
exports.router = (0, express_1.Router)();
exports.router.use(banner_1.bannerRouter);
exports.router.use('/auth', auth_1.authRouter);

1
dist/utils/error-handling.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare const errorHandle: (error: any, req: any, res: any, next: any) => void;

9
dist/utils/error-handling.js vendored Normal file
View File

@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorHandle = void 0;
const errorHandle = (error, req, res, next) => {
res.status(500).send({
error: error.message || 'some error'
});
};
exports.errorHandle = errorHandle;

View File

@ -5,5 +5,7 @@ import BannerData from './data.json';
export const bannerRouter = Router();
bannerRouter.get('/banner-data', (req, res) => {
res.send(BannerData)
setTimeout(() => {
res.send(BannerData)
}, 3000)
})