banner-data
This commit is contained in:
3
src/config.ts
Normal file
3
src/config.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { config } from 'dotenv';
|
||||
|
||||
config();
|
||||
16
src/main.ts
16
src/main.ts
@@ -1,22 +1,18 @@
|
||||
import express from 'express';
|
||||
import { config } from 'dotenv';
|
||||
import cookieSession from 'cookie-session';
|
||||
|
||||
import './config';
|
||||
import { errorHandle } from './utils/error-handling';
|
||||
|
||||
config();
|
||||
import { router } from './routes';
|
||||
|
||||
const app = express();
|
||||
|
||||
const port = process.env.RED_CODER_BH_PORT;
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('hello')
|
||||
})
|
||||
|
||||
app.get('/error', (req, res) => {
|
||||
throw new Error('some mistake')
|
||||
})
|
||||
app.use(express.json());
|
||||
app.use(cookieSession({ secret: process.env.COOKIE_SESSION }));
|
||||
|
||||
app.use(router);
|
||||
|
||||
app.use(errorHandle);
|
||||
app.listen(port, () => {
|
||||
|
||||
76
src/routes/auth.ts
Normal file
76
src/routes/auth.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { Router } from 'express';
|
||||
import pbkdf2Password from 'pbkdf2-password';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import jwtMiddleware from 'express-jwt';
|
||||
|
||||
const makeHash = pbkdf2Password();
|
||||
|
||||
export const authRouter = Router();
|
||||
|
||||
const requiredFields = (fields: string[]) => (req, res, next) => {
|
||||
for (const fieldName of fields) {
|
||||
if (!req.body[fieldName]) {
|
||||
throw new Error(`Field ${fieldName} does\'t set`)
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
const users: any[] = [];
|
||||
|
||||
|
||||
authRouter.get('/users', jwtMiddleware({ secret: process.env.JWT_SECRET_STRING, algorithms: ['HS256'] }), (req, res) => {
|
||||
res.send(users);
|
||||
});
|
||||
|
||||
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, ...cleanUser } = user
|
||||
|
||||
req.session.user = cleanUser;
|
||||
const token = jwt.sign(cleanUser, process.env.JWT_SECRET_STRING, {
|
||||
|
||||
});
|
||||
|
||||
return res.send({ token, user: cleanUser })
|
||||
}
|
||||
|
||||
res.status(400).send({ error: 'Login or password does\'t match' });
|
||||
});
|
||||
});
|
||||
|
||||
authRouter.post('/sign-up', requiredFields(['password', 'login', 'email']), (req, res, next) => {
|
||||
const { password, login, ...rest } = req.body;
|
||||
|
||||
makeHash({ password }, function (err, pass, salt, hash) {
|
||||
if (err) throw err;
|
||||
|
||||
const newUser = {
|
||||
id: uuid(),
|
||||
...rest,
|
||||
login,
|
||||
salt,
|
||||
hash
|
||||
}
|
||||
|
||||
users.push(newUser);
|
||||
|
||||
const { hash: _hash, salt: _salt, ...cleanUser } = newUser
|
||||
|
||||
res.send(cleanUser);
|
||||
});
|
||||
});
|
||||
19
src/routes/banner/data.json
Normal file
19
src/routes/banner/data.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"data": [
|
||||
{
|
||||
"title": "Подсказка на сегодня",
|
||||
"body": "Подберите задачки под свой уровень сложности, решите её и наращивайте навык",
|
||||
"icon": "allGood"
|
||||
},
|
||||
{
|
||||
"title": "Подсказка на завтра",
|
||||
"body": "Не забудь сутра выпить кофе",
|
||||
"icon": "coding"
|
||||
},
|
||||
{
|
||||
"title": "Подсказка на вчера",
|
||||
"body": "Забыл сутра выпить кофе?",
|
||||
"icon": "askingQuestion"
|
||||
}
|
||||
]
|
||||
}
|
||||
9
src/routes/banner/index.ts
Normal file
9
src/routes/banner/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Router } from 'express';
|
||||
|
||||
import BannerData from './data.json';
|
||||
|
||||
export const bannerRouter = Router();
|
||||
|
||||
bannerRouter.get('/banner-data', (req, res) => {
|
||||
res.send(BannerData)
|
||||
})
|
||||
9
src/routes/index.ts
Normal file
9
src/routes/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Router } from 'express';
|
||||
|
||||
import { authRouter } from './auth';
|
||||
import { bannerRouter } from './banner';
|
||||
|
||||
export const router = Router();
|
||||
|
||||
router.use(bannerRouter);
|
||||
router.use('/auth', authRouter);
|
||||
Reference in New Issue
Block a user