Compare commits
5 Commits
771f75ef08
...
dogsitters
| Author | SHA1 | Date | |
|---|---|---|---|
| 312cc229d8 | |||
| 11b1d670d0 | |||
|
|
522ea36bb9 | ||
|
|
8be391c8e1 | ||
|
|
ea80304c21 |
@@ -7,33 +7,64 @@ router.get("/users", (request, response) => {
|
||||
router.post("/auth", (request, response) => {
|
||||
const { phoneNumber, password } = request.body;
|
||||
console.log(phoneNumber, password);
|
||||
if (phoneNumber === "89999999999") {
|
||||
response.send(require("./json/auth/dogsitter.success.json"));
|
||||
} else if (phoneNumber === "89555555555") {
|
||||
response.status(400).send(require("./json/auth/error.json"));
|
||||
if (phoneNumber === "89999999999" || phoneNumber === "89559999999") {
|
||||
response.send(require("../json/auth/success.json"));
|
||||
} else {
|
||||
response.send(require("./json/auth/owner.success.json"));
|
||||
response.status(401).send(require("../json/auth/error.json"));
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/auth/2fa", (request, response) => {
|
||||
const { code } = request.body;
|
||||
if (code === "0000") {
|
||||
response.send(require("./json/2fa/success.json"));
|
||||
const { phoneNumber, code } = request.body;
|
||||
if (code === "0000" && phoneNumber === "89999999999") {
|
||||
response.send(require("../json/2fa/dogsitter.success.json"));
|
||||
} else if (code === "0000" && phoneNumber === "89559999999") {
|
||||
response.send(require("../json/2fa/owner.success.json"));
|
||||
} else {
|
||||
response.status(400).send(require("./json/2fa/error.json"));
|
||||
response.status(401).send(require("../json/2fa/error.json"));
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/register", (request, response) => {
|
||||
const { firstName, secondName, phoneNumber, password, role } = request.body;
|
||||
console.log(phoneNumber, password, role);
|
||||
if (phoneNumber === "89283244141" || phoneNumber === "89872855893") {
|
||||
response.status(400).send(require("./json/register/error.json"));
|
||||
if (phoneNumber === "89999999999" || phoneNumber === "89559999999") {
|
||||
response.status(401).send(require("../json/register/error.json"));
|
||||
} else if (role === "dogsitter") {
|
||||
response.send(require("./json/register/dogsitter.success.json"));
|
||||
response.send(require("../json/register/dogsitter.success.json"));
|
||||
} else {
|
||||
response.send(require("./json/register/owner.success.json"));
|
||||
response.send(require("../json/register/owner.success.json"));
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/auth/session", (request, response) => {
|
||||
const authHeader = request.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
return response.status(401).json({ error: "Authorization header missing" });
|
||||
}
|
||||
|
||||
// Берём сам токен из заголовка
|
||||
const token = authHeader.split(" ")[1];
|
||||
|
||||
if (!token) {
|
||||
return response.status(401).json({ error: "Bearer token missing" });
|
||||
}
|
||||
|
||||
const jwt = require("jsonwebtoken");
|
||||
const secretKey = "secret";
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, secretKey);
|
||||
|
||||
if (decoded.role === "dogsitter") {
|
||||
response.send(require("../json/role/dogsitter.success.json"));
|
||||
} else {
|
||||
response.send(require("../json/role/owner.success.json"));
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("token e:", e);
|
||||
return response.status(403).json({ error: "Invalid token" });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwicm9sZSI6ImRvZ3NpdHRlciIsImlhdCI6MTUxNjIzOTAyMn0.7q66wTNyLZp3TGFYF_JdU-yhlWViJulTxP_PCQzO4OI"
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
{
|
||||
"status": "error",
|
||||
"message": "Invalid code."
|
||||
"message": "Invalid code",
|
||||
"statusCode": 401
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Mywicm9sZSI6Im93bmVyIiwiaWF0IjoxNTE2MjM5MDIyfQ.sI9839YXveTpEWhdpr5QbCYllt6hHYO7NsrQDcrXZIQ"
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"status": "success",
|
||||
"message": "Two-factor authentication passed."
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"data": {
|
||||
"id": 1,
|
||||
"phoneNumber": 89283244141,
|
||||
"firstName": "Вася",
|
||||
"secondName": "Пупкин",
|
||||
"role": "dogsitter",
|
||||
"location": "Россия, республика Татарстан, Казань, улица Пушкина, 12",
|
||||
"price": 1500,
|
||||
"aboutMe": "Я люблю собак"
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
{
|
||||
"error": "Пользователь не найден"
|
||||
}
|
||||
"message": "Неверный логин или пароль",
|
||||
"error": "Unauthorized",
|
||||
"statusCode": 401
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"data": {
|
||||
"id": 3,
|
||||
"phoneNumber": 89872855893,
|
||||
"firstName": "Гадий",
|
||||
"secondName": "Петрович",
|
||||
"role": "owner"
|
||||
}
|
||||
}
|
||||
5
server/routers/dogsitters-finder/json/auth/success.json
Normal file
5
server/routers/dogsitters-finder/json/auth/success.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"status": "success",
|
||||
"message": "Первый фактор аутентификации пройден",
|
||||
"statusCode": 200
|
||||
}
|
||||
@@ -1,12 +1,3 @@
|
||||
{
|
||||
"data": {
|
||||
"id": 5,
|
||||
"phoneNumber": 89555555555,
|
||||
"firstName": "Масяня",
|
||||
"secondName": "Карлова",
|
||||
"role": "dogsitter",
|
||||
"location": "Россия, республика Татарстан, Казань, улица Пушкина, 12",
|
||||
"price": 100,
|
||||
"aboutMe": "Все на свете - собаки"
|
||||
}
|
||||
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NSwicm9sZSI6ImRvZ3NpdHRlciIsImlhdCI6MTUxNjIzOTAyMn0.T9V3-f3rD1deA5a2J-tYNw0cACEpzKHbhMPkc7gh8c0"
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
{
|
||||
"error": "Пользователь с таким номером телефона уже существует"
|
||||
"message": "Такой пользователь уже был зарегистрирован",
|
||||
"error": "Unauthorized",
|
||||
"statusCode": 401
|
||||
}
|
||||
@@ -1,9 +1,3 @@
|
||||
{
|
||||
"data": {
|
||||
"id": 6,
|
||||
"phoneNumber": 89888888888,
|
||||
"firstName": "Генадий",
|
||||
"secondName": "Паровозов",
|
||||
"role": "owner"
|
||||
}
|
||||
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6Niwicm9sZSI6Im93bmVyIiwiaWF0IjoxNTE2MjM5MDIyfQ.qgOhk9tNcaMRbarRWISTgvGx5Eq_X8fcA5lhdVs2tQI"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"id": 1,
|
||||
"role": "dogsitter"
|
||||
}
|
||||
5
server/routers/dogsitters-finder/json/role/error.json
Normal file
5
server/routers/dogsitters-finder/json/role/error.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"message": "Неверный jwt token",
|
||||
"error": "Forbidden",
|
||||
"statusCode": 403
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"id": 3,
|
||||
"role": "owner"
|
||||
}
|
||||
@@ -1,25 +1,24 @@
|
||||
[
|
||||
{ "id": 1, "description": "1000 часто используемых", "imageFilename": "kart1.jpg", "words": [0, 1] },
|
||||
{
|
||||
"id": 2,
|
||||
"id": 1,
|
||||
"description": "10 слов в Data Science",
|
||||
"imageFilename": "kart1.jpg",
|
||||
"words": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"id": 2,
|
||||
"description": "Job Interview",
|
||||
"imageFilename": "kart1.jpg",
|
||||
"words": [13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"id": 3,
|
||||
"description": "ReactJS",
|
||||
"imageFilename": "kart1.jpg",
|
||||
"words": [23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"id": 4,
|
||||
"description": "NodeJS",
|
||||
"imageFilename": "kart1.jpg",
|
||||
"words": [33, 34, 35, 36, 37, 38, 39, 40, 41, 42]
|
||||
|
||||
@@ -5,15 +5,16 @@ const router = require('express').Router();
|
||||
module.exports = router;
|
||||
|
||||
const data = require('./units.json');
|
||||
const users = require('../users/users.json');
|
||||
router.get('/', (req, res) => {
|
||||
// for every data set author from users and save it to authoredData variable
|
||||
const users = require('../users/users.json');
|
||||
const authoredData = data.map((unit) => {
|
||||
const user = users.find((user) => user.public_id == unit.author);
|
||||
let authoredUnit = undefined;
|
||||
if (user) {
|
||||
unit.author = user;
|
||||
authoredUnit = { ...unit, author: user };
|
||||
}
|
||||
return unit;
|
||||
return authoredUnit;
|
||||
});
|
||||
|
||||
res.send(authoredData);
|
||||
@@ -39,9 +40,8 @@ router.post('/:id', (req, res) => {
|
||||
|
||||
data.splice(index, 1);
|
||||
|
||||
data.push(updatedUnit);
|
||||
data.push({...updatedUnit, author: updatedUnit.author.public_id});
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.status(200).send(data);
|
||||
});
|
||||
|
||||
@@ -61,12 +61,8 @@ router.put('/', (req, res) => {
|
||||
}
|
||||
|
||||
const newId = data.length + 1;
|
||||
// const filename = newUnit.name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
||||
// fs.writeFileSync(path.join(__dirname, 'data', `${filename}.md`), newUnit.content);
|
||||
data.push({ ...newUnit, id: newId });
|
||||
|
||||
data.push({ ...unit, id: newId });
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.status(200).send(data);
|
||||
});
|
||||
|
||||
@@ -79,11 +75,11 @@ router.delete('/:id', (req, res) => {
|
||||
}
|
||||
|
||||
data.splice(index, 1);
|
||||
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
|
||||
res.send({ message: `Unit with ID ${id} deleted` });
|
||||
});
|
||||
|
||||
router.get('/:id', (req, res) => {
|
||||
const users = require('../users/users.json');
|
||||
const id = parseInt(req.params.id);
|
||||
const unit = data.find((unit) => unit.id === id);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ const fs = require('fs');
|
||||
|
||||
module.exports = router;
|
||||
|
||||
const data = require('./users.json');
|
||||
let data = require('./users.json');
|
||||
const path = require('path');
|
||||
router.get('/', (req, res) => {
|
||||
res.send(data);
|
||||
@@ -11,20 +11,15 @@ router.get('/', (req, res) => {
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
const newUser = req.body;
|
||||
const updatedData = [...data, newUser];
|
||||
|
||||
console.log(updatedData);
|
||||
fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(updatedData));
|
||||
|
||||
res.send(updatedData);
|
||||
data.push(newUser);
|
||||
fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(data));
|
||||
res.send(data);
|
||||
});
|
||||
|
||||
router.post('/login', (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
console.log(email);
|
||||
console.log(req.body);
|
||||
const user = data.find((user) => user.email === email && user.password === password);
|
||||
console.log(user);
|
||||
|
||||
if (!user) {
|
||||
res.status(404).send('Пользователь не найден');
|
||||
@@ -34,13 +29,11 @@ router.post('/login', (req, res) => {
|
||||
|
||||
router.get('/account', (req, res) => {
|
||||
const { public_id } = req.query;
|
||||
console.log(public_id);
|
||||
const user = data.find((user) => user.public_id == public_id);
|
||||
|
||||
if (!user) {
|
||||
res.status(404).send('Пользователь не найден');
|
||||
}
|
||||
console.log(user);
|
||||
res.send({ ...user, id: -1 });
|
||||
});
|
||||
|
||||
@@ -54,7 +47,6 @@ router.post('/account/save', (req, res) => {
|
||||
}
|
||||
|
||||
data[index] = { ...data[index], ...updatedUser, id: data[index].id, password: data[index].password };
|
||||
|
||||
fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(data));
|
||||
|
||||
res.status(200);
|
||||
|
||||
Reference in New Issue
Block a user