diff --git a/dist/config.d.ts b/dist/config.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/config.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/config.js b/dist/config.js new file mode 100644 index 0000000..e82c6f7 --- /dev/null +++ b/dist/config.js @@ -0,0 +1,4 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const dotenv_1 = require("dotenv"); +(0, dotenv_1.config)(); diff --git a/dist/main.d.ts b/dist/main.d.ts new file mode 100644 index 0000000..c791e64 --- /dev/null +++ b/dist/main.d.ts @@ -0,0 +1 @@ +import './config'; diff --git a/dist/main.js b/dist/main.js new file mode 100644 index 0000000..4b2373c --- /dev/null +++ b/dist/main.js @@ -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}`); +}); diff --git a/dist/routes/auth.d.ts b/dist/routes/auth.d.ts new file mode 100644 index 0000000..0a49df6 --- /dev/null +++ b/dist/routes/auth.d.ts @@ -0,0 +1 @@ +export declare const authRouter: import("express-serve-static-core").Router; diff --git a/dist/routes/auth.js b/dist/routes/auth.js new file mode 100644 index 0000000..4fd2aeb --- /dev/null +++ b/dist/routes/auth.js @@ -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); + }); +}); diff --git a/dist/routes/banner/data.json b/dist/routes/banner/data.json new file mode 100644 index 0000000..90390ee --- /dev/null +++ b/dist/routes/banner/data.json @@ -0,0 +1,19 @@ +{ + "data": [ + { + "title": "Подсказка на сегодня", + "body": "Подберите задачки под свой уровень сложности, решите её и наращивайте навык", + "icon": "allGood" + }, + { + "title": "Подсказка на завтра", + "body": "Не забудь сутра выпить кофе", + "icon": "coding" + }, + { + "title": "Подсказка на вчера", + "body": "Забыл сутра выпить кофе?", + "icon": "askingQuestion" + } + ] +} diff --git a/dist/routes/banner/index.d.ts b/dist/routes/banner/index.d.ts new file mode 100644 index 0000000..666cae5 --- /dev/null +++ b/dist/routes/banner/index.d.ts @@ -0,0 +1 @@ +export declare const bannerRouter: import("express-serve-static-core").Router; diff --git a/dist/routes/banner/index.js b/dist/routes/banner/index.js new file mode 100644 index 0000000..c2e5a69 --- /dev/null +++ b/dist/routes/banner/index.js @@ -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); +}); diff --git a/dist/routes/index.d.ts b/dist/routes/index.d.ts new file mode 100644 index 0000000..c7f48f9 --- /dev/null +++ b/dist/routes/index.d.ts @@ -0,0 +1 @@ +export declare const router: import("express-serve-static-core").Router; diff --git a/dist/routes/index.js b/dist/routes/index.js new file mode 100644 index 0000000..dbc2584 --- /dev/null +++ b/dist/routes/index.js @@ -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); diff --git a/dist/utils/error-handling.d.ts b/dist/utils/error-handling.d.ts new file mode 100644 index 0000000..5bcbd3e --- /dev/null +++ b/dist/utils/error-handling.d.ts @@ -0,0 +1 @@ +export declare const errorHandle: (error: any, req: any, res: any, next: any) => void; diff --git a/dist/utils/error-handling.js b/dist/utils/error-handling.js new file mode 100644 index 0000000..1cb0fd5 --- /dev/null +++ b/dist/utils/error-handling.js @@ -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; diff --git a/src/routes/banner/index.ts b/src/routes/banner/index.ts index 7448970..72324fa 100644 --- a/src/routes/banner/index.ts +++ b/src/routes/banner/index.ts @@ -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) })