diff --git a/.gitignore b/.gitignore index 283bb88..52229bb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ src/database/prisma/client *.sqlite package-lock.json .vscode -*.db* \ No newline at end of file +*.db* +db.json \ No newline at end of file diff --git a/db.sample.json b/db.sample.json index 32fba31..ea8c5f7 100644 --- a/db.sample.json +++ b/db.sample.json @@ -1,495 +1,543 @@ { - "users":[ - { - "id":1, - "login":"admin", - "password":"$2b$09$pZ6oA0izrIg8hugEVZC.W.Osc6lXrF9bet8pvpIYf4eOAzen/sV/q" - } - ], - "stationsList": [ - { + "users": [ + { + "id": 1, + "login": "admin", + "password": "$2b$09$pZ6oA0izrIg8hugEVZC.W.Osc6lXrF9bet8pvpIYf4eOAzen/sV/q" + } + ], + "stationsList": [ + { + "id": 1, + "title": "Новокузнецк-Северный", + "isLoginStation": false + }, + { + "id": 2, + "title": "Томусинская", + "isLoginStation": false + }, + { + "id": 3, + "title": "Курегеш", + "isLoginStation": false + } + ], + "stationsData": [ + { + "station": { "id": 1, - "title": "Новокузнецк-Северный", - "isLoginStation": false + "name": "НОВОКУЗНЕЦК-СЕВЕРНЫЙ" }, - { - "id": 2, - "title": "Томусинская", - "isLoginStation": false - }, - { - "id": 3, - "title": "Курегеш", - "isLoginStation": false - } - ], - "stationsData": [ - { - "station": { - "id": 1, - "name": "НОВОКУЗНЕЦК-СЕВЕРНЫЙ" - }, - "park": { - "id": 10, - "name": "ТЭЦ" - }, - "way": { "id": 1, "name": "10A" }, - "maxCarriagesCount": 28, - "locomotives": [ - { - "id": 1, - "inventoryNumber": "422412", - "direction": "LEFT" - }, - { - "id": 2, - "inventoryNumber": "123456", - "direction": "RIGHT" - } - ], - "wagons": [ - { - "id": 1, - "inventoryNumber": "53532685", - "position": 1, - "type": "HalfCarriage", - "isSick": false, - "isLaden": true, - "state": "StandingBy", - "idleDaysLength": 5, - "owner": "НТС", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 42992, - "isDirty": false, - "note1": "TestNote1", - "note2": "TestNote2" - }, - { - "id": 2, - "inventoryNumber": "214121", - "position": 2, - "type": "HalfCarriage", - "isSick": false, - "isLaden": true, - "state": "InProgress", - "idleDaysLength": 11, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 75, - "daysToRepair": 96, - "kilometersLeft": 42992, - "isDirty": false, - "note1": "TestNote1", - "note2": "Note2" - }, - { - "id": 3, - "inventoryNumber": "4523122", - "position": 3, - "type": "HalfCarriage", - "isSick": false, - "isLaden": "Empty", - "state": "Finished", - "idleDaysLength": 5, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 212, - "isDirty": false, - "note1": "", - "note2": "" - }, - { - "id": 4, - "inventoryNumber": "4523122", - "position": 4, - "type": "HalfCarriage", - "isSick": false, - "isLaden": "Empty", - "state": "NotArrived", - "idleDaysLength": 5, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 212, - "isDirty": false, - "note1": "", - "note2": "" - } - ] - - }, - { - "station": { - "id": 1, - "name": "НОВОКУЗНЕЦК-СЕВЕРНЫЙ" - }, - "park": { - "id": 10, - "name": "ТЭЦ" - }, - "way": { "id": 2, "name": "8B" }, - "maxCarriagesCount": 134, - "locomotives": [ - { - "id": 3, - "inventoryNumber": "432121", - "direction": "RIGHT" - } - ], - "wagons": [ - { - "id": 5, - "inventoryNumber": "321312", - "position": 1, - "type": "HalfCarriage", - "isSick": false, - "isLaden": true, - "state": "StandingBy", - "idleDaysLength": 5, - "owner": "НТС", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 42992, - "isDirty": false, - "note1": "TestNote1", - "note2": "TestNote2" - }, - { - "id": 6, - "inventoryNumber": "324123", - "position": 2, - "type": "HalfCarriage", - "isSick": false, - "isLaden":true, - "state": "InProgress", - "idleDaysLength": 11, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 75, - "daysToRepair": 96, - "kilometersLeft": 42992, - "isDirty": false, - "note1": "TestNote1", - "note2": "Note2" - }, - { - "id": 7, - "inventoryNumber": "453112", - "position": 3, - "type": "CoveredCarriage", - "isSick": false, - "isLaden": "Empty", - "state": "Finished", - "idleDaysLength": 5, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 212, - "isDirty": false, - "note1": "", - "note2": "" - }, - { - "id": 8, - "inventoryNumber": "786766", - "position": 4, - "type": "CoveredCarriage", - "isSick": false, - "isLaden": "Empty", - "state": "NotArrived", - "idleDaysLength": 5, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 212, - "isDirty": false, - "note1": "", - "note2": "" - } - ] - - }, - { - "station": { - "id": 1, - "name": "НОВОКУЗНЕЦК-СЕВЕРНЫЙ" - }, - "park": { - "id": 10, - "name": "ТЭЦ" - }, - "way": { "id": 2, "name": "3С" }, - "maxCarriagesCount": 134, - "locomotives": [ - { - "id": 3, - "inventoryNumber": "432121", - "direction": "RIGHT" - } - ], - "wagons": [ - { - "id": 9, - "inventoryNumber": "534287", - "position": 1, - "type": "Hopper", - "isSick": false, - "isLaden": true, - "state": "StandingBy", - "idleDaysLength": 5, - "owner": "НТС", - "isWithHatch": false, - "loadCapacity": 69.5, - "daysToRepair": 96, - "kilometersLeft": 42992, - "isDirty": false, - "note1": "TestNote1", - "note2": "TestNote2" - }, - { - "id": 10, - "inventoryNumber": "2212438", - "position": 2, - "type": "Tank", - "isSick": false, - "isLaden": true, - "state": "InProgress", - "idleDaysLength": 11, - "owner": "ГК", - "isWithHatch": false, - "loadCapacity": 75, - "daysToRepair": 96, - "kilometersLeft": 42992, - "isDirty": false, - "note1": "TestNote1", - "note2": "Note2" - } - ] - - }, - { - "station": { - "id": 2, - "name": "Томусинская" - }, - "park": { - "id": 1231, - "name": "3D" - }, - "way": { "id": 3123, "name": "21" }, - "maxCarriagesCount": 134, - "locomotives": [], - "wagons": [] - - }, - { - "station": { - "id": 2, - "name": "Курегеш" - }, - "park": { - "id": 5423, - "name": "1f" - }, - "way": { "id": 232, "name": "8" }, - "maxCarriagesCount": 134, - "locomotives": [], - "wagons": [] - - } - ], - "wagonTypeList": [ - "HalfCarriage", - "Platform", - "CoveredCarriage", - "UsoPlatform", - "Hopper", - "Tank" - ], - "ownersList": [ - "НТС", - "ГК", - "АТЛ", - "ПГК", - "МОД", - "РЖД", - "НПК", - "ФГК", - "МЕЧ", - "Контрагенты", - "Прочие" - ], - "operationsTypesNorms": [ - { "operationId": 1, "minutesNorma": 10 }, - { "operationId": 2, "minutesNorma": 15 }, - { "operationId": 8, "minutesNorma": 10 }, - { "operationId": 6, "minutesNorma": 30 }, - { "operationId": 9, "minutesNorma": 25 }, - { "operationId": 44, "minutesNorma": 25 }, - { "operationId": 48, "minutesNorma": 20 }, - { "operationId": 75, "minutesNorma": 12 }, - { "operationId": 74, "minutesNorma": 8 } - ], - "operationsTypes": [ - { - "id": 1, - "name": "Поездная работа", - "operations": [ - { - "id": 1, - "name": "Ведение поезда по перегону", - "operationTypeId": 1 - }, - { - "id": 2, - "name": "Движение локомотива резервом по перегону", - "operationTypeId": 1 - } - ] - }, - { - "id": 2, - "name": "Маневровая работа", - "operations": [ - { - "id": 8, - "name": "Подача вагонов", - "operationTypeId": 2 - }, - { - "id": 6, - "name": "Перестановка вагонов", - "operationTypeId": 2 - }, - { - "id": 9, - "name": "Уборка вагонов", - "operationTypeId": 2 - } - ] - }, - { - "id": 8, - "name": "Ремонтные работы", - "operations": [ - { - "id": 44, - "name": "Замена неисправного локомотива (депо)", - "operationTypeId": 8 - }, - { - "id": 48, - "name": "Ремонт подвижного состава", - "operationTypeId": 8 - } - ] - }, - { - "id": 11, - "name": "Грузовые", - "operations": [ - { - "id": 75, - "name": "Погрузка", - "operationTypeId": 11 - }, - { - "id": 74, - "name": "Выгрузка", - "operationTypeId": 11 - } - ] - } - ], - "operationsList": [ - { - "id": 23121, - "visibleNumber": "23112", - "startDate": "2023-12-22T06:37:46.892Z", - "endDate": "2023-12-24T06:37:46.892Z", - "normalMinutesLength": 10, - "operationId": 8, - "operationName": "Подача вагонов", - "operationTypeId": 2, - "reasonId": 7, - "wagonsIds": [], - "departureStation": { "id": 1, "title": "Новокузнецк-Северный" }, - "departurePark": { - "id": 10, - "name": "ТЭЦ" - }, - "departureWay": { "id": 1, "name": "10A" }, - "destinationStation": { "id": 3, "title": "Курегеш" }, - "destinationPark": { - "id": 5423, - "name": "1k" - }, - "destinationWay": { "id": 232, "name": "29" }, - "supplyDirection": "LEFT", - "locomotivesList": [ - { - "id": 1, - "inventoryNumber": "422412", - "direction": "LEFT" - } - ], - "operationStatus": "ACTIVE", - "comment": "" - } - ], - "operationReasonsList": [ - { - "id": 1, - "title": "Формирование подачи под выгрузку" - }, - { - "id": 2, - "title": "Подбор по характеристикам вагонов" - }, - { - "id": 3, - "title": "Формирование маршрута" - }, - { - "id": 4, - "title": "Выработка больных вагонов" - }, - { - "id": 5, - "title": "Выработка грязных вагонов" - }, - { - "id": 6, - "title": "Выравнивание шапок" - }, - { - "id": 7, - "title": "Подбор по собственникам" - }, - { - "id": 8, - "title": "Формирование порожняка на отправление" - }, - { - "id": 9, - "title": "Подбор вагонов по качеству загруженного угля" - }, - { + "park": { "id": 10, - "title": "Переформирование маршрута" + "name": "ТЭЦ" }, - { - "id": 11, - "title": "Подбор инновационных вагонов" - } - ] - } - \ No newline at end of file + "way": { + "id": 1, + "name": "10A" + }, + "maxCarriagesCount": 28, + "locomotives": [ + { + "id": 1, + "inventoryNumber": "422412", + "direction": "LEFT" + }, + { + "id": 2, + "inventoryNumber": "123456", + "direction": "RIGHT" + } + ], + "wagons": [ + { + "id": 1, + "inventoryNumber": "53532685", + "position": 1, + "type": "HalfCarriage", + "isSick": false, + "isLaden": true, + "state": "StandingBy", + "idleDaysLength": 5, + "owner": "НТС", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 42992, + "isDirty": false, + "note1": "TestNote1", + "note2": "TestNote2" + }, + { + "id": 2, + "inventoryNumber": "214121", + "position": 2, + "type": "HalfCarriage", + "isSick": false, + "isLaden": true, + "state": "InProgress", + "idleDaysLength": 11, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 75, + "daysToRepair": 96, + "kilometersLeft": 42992, + "isDirty": false, + "note1": "TestNote1", + "note2": "Note2" + }, + { + "id": 3, + "inventoryNumber": "4523122", + "position": 3, + "type": "HalfCarriage", + "isSick": false, + "isLaden": "Empty", + "state": "Finished", + "idleDaysLength": 5, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 212, + "isDirty": false, + "note1": "", + "note2": "" + }, + { + "id": 4, + "inventoryNumber": "4523122", + "position": 4, + "type": "HalfCarriage", + "isSick": false, + "isLaden": "Empty", + "state": "NotArrived", + "idleDaysLength": 5, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 212, + "isDirty": false, + "note1": "", + "note2": "" + } + ] + }, + { + "station": { + "id": 1, + "name": "НОВОКУЗНЕЦК-СЕВЕРНЫЙ" + }, + "park": { + "id": 10, + "name": "ТЭЦ" + }, + "way": { + "id": 2, + "name": "8B" + }, + "maxCarriagesCount": 134, + "locomotives": [ + { + "id": 3, + "inventoryNumber": "432121", + "direction": "RIGHT" + } + ], + "wagons": [ + { + "id": 5, + "inventoryNumber": "321312", + "position": 1, + "type": "HalfCarriage", + "isSick": false, + "isLaden": true, + "state": "StandingBy", + "idleDaysLength": 5, + "owner": "НТС", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 42992, + "isDirty": false, + "note1": "TestNote1", + "note2": "TestNote2" + }, + { + "id": 6, + "inventoryNumber": "324123", + "position": 2, + "type": "HalfCarriage", + "isSick": false, + "isLaden": true, + "state": "InProgress", + "idleDaysLength": 11, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 75, + "daysToRepair": 96, + "kilometersLeft": 42992, + "isDirty": false, + "note1": "TestNote1", + "note2": "Note2" + }, + { + "id": 7, + "inventoryNumber": "453112", + "position": 3, + "type": "CoveredCarriage", + "isSick": false, + "isLaden": "Empty", + "state": "Finished", + "idleDaysLength": 5, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 212, + "isDirty": false, + "note1": "", + "note2": "" + }, + { + "id": 8, + "inventoryNumber": "786766", + "position": 4, + "type": "CoveredCarriage", + "isSick": false, + "isLaden": "Empty", + "state": "NotArrived", + "idleDaysLength": 5, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 212, + "isDirty": false, + "note1": "", + "note2": "" + } + ] + }, + { + "station": { + "id": 1, + "name": "НОВОКУЗНЕЦК-СЕВЕРНЫЙ" + }, + "park": { + "id": 10, + "name": "ТЭЦ" + }, + "way": { + "id": 2, + "name": "3С" + }, + "maxCarriagesCount": 134, + "locomotives": [ + { + "id": 3, + "inventoryNumber": "432121", + "direction": "RIGHT" + } + ], + "wagons": [ + { + "id": 9, + "inventoryNumber": "534287", + "position": 1, + "type": "Hopper", + "isSick": false, + "isLaden": true, + "state": "StandingBy", + "idleDaysLength": 5, + "owner": "НТС", + "isWithHatch": false, + "loadCapacity": 69.5, + "daysToRepair": 96, + "kilometersLeft": 42992, + "isDirty": false, + "note1": "TestNote1", + "note2": "TestNote2" + }, + { + "id": 10, + "inventoryNumber": "2212438", + "position": 2, + "type": "Tank", + "isSick": false, + "isLaden": true, + "state": "InProgress", + "idleDaysLength": 11, + "owner": "ГК", + "isWithHatch": false, + "loadCapacity": 75, + "daysToRepair": 96, + "kilometersLeft": 42992, + "isDirty": false, + "note1": "TestNote1", + "note2": "Note2" + } + ] + }, + { + "station": { + "id": 2, + "name": "Томусинская" + }, + "park": { + "id": 1231, + "name": "3D" + }, + "way": { + "id": 3123, + "name": "21" + }, + "maxCarriagesCount": 134, + "locomotives": [], + "wagons": [] + }, + { + "station": { + "id": 2, + "name": "Курегеш" + }, + "park": { + "id": 5423, + "name": "1f" + }, + "way": { + "id": 232, + "name": "8" + }, + "maxCarriagesCount": 134, + "locomotives": [], + "wagons": [] + } + ], + "wagonTypeList": [ + "HalfCarriage", + "Platform", + "CoveredCarriage", + "UsoPlatform", + "Hopper", + "Tank" + ], + "ownersList": [ + "НТС", + "ГК", + "АТЛ", + "ПГК", + "МОД", + "РЖД", + "НПК", + "ФГК", + "МЕЧ", + "Контрагенты", + "Прочие" + ], + "operationsTypesNorms": [ + { + "operationId": 1, + "minutesNorma": 10 + }, + { + "operationId": 2, + "minutesNorma": 15 + }, + { + "operationId": 8, + "minutesNorma": 10 + }, + { + "operationId": 6, + "minutesNorma": 30 + }, + { + "operationId": 9, + "minutesNorma": 25 + }, + { + "operationId": 44, + "minutesNorma": 25 + }, + { + "operationId": 48, + "minutesNorma": 20 + }, + { + "operationId": 75, + "minutesNorma": 12 + }, + { + "operationId": 74, + "minutesNorma": 8 + } + ], + "operationsTypes": [ + { + "id": 1, + "name": "Поездная работа", + "operations": [ + { + "id": 1, + "name": "Ведение поезда по перегону", + "operationTypeId": 1 + }, + { + "id": 2, + "name": "Движение локомотива резервом по перегону", + "operationTypeId": 1 + } + ] + }, + { + "id": 2, + "name": "Маневровая работа", + "operations": [ + { + "id": 8, + "name": "Подача вагонов", + "operationTypeId": 2 + }, + { + "id": 6, + "name": "Перестановка вагонов", + "operationTypeId": 2 + }, + { + "id": 9, + "name": "Уборка вагонов", + "operationTypeId": 2 + } + ] + }, + { + "id": 8, + "name": "Ремонтные работы", + "operations": [ + { + "id": 44, + "name": "Замена неисправного локомотива (депо)", + "operationTypeId": 8 + }, + { + "id": 48, + "name": "Ремонт подвижного состава", + "operationTypeId": 8 + } + ] + }, + { + "id": 11, + "name": "Грузовые", + "operations": [ + { + "id": 75, + "name": "Погрузка", + "operationTypeId": 11 + }, + { + "id": 74, + "name": "Выгрузка", + "operationTypeId": 11 + } + ] + } + ], + "operationsList": [ + { + "id": 23121, + "visibleNumber": "23112", + "startDate": "2023-12-22T06:37:46.892Z", + "endDate": "2023-12-24T06:37:46.892Z", + "normalMinutesLength": 10, + "operationId": 8, + "operationName": "Подача вагонов", + "operationTypeId": 2, + "reasonId": 7, + "wagonsIds": [], + "departureStation": { + "id": 1, + "title": "Новокузнецк-Северный" + }, + "departurePark": { + "id": 10, + "name": "ТЭЦ" + }, + "departureWay": { + "id": 1, + "name": "10A" + }, + "destinationStation": { + "id": 3, + "title": "Курегеш" + }, + "destinationPark": { + "id": 5423, + "name": "1k" + }, + "destinationWay": { + "id": 232, + "name": "29" + }, + "supplyDirection": "LEFT", + "locomotivesList": [ + { + "id": 1, + "inventoryNumber": "422412", + "direction": "LEFT" + } + ], + "operationStatus": "ACTIVE", + "comment": "" + } + ], + "operationReasonsList": [ + { + "id": 1, + "title": "Формирование подачи под выгрузку" + }, + { + "id": 2, + "title": "Подбор по характеристикам вагонов" + }, + { + "id": 3, + "title": "Формирование маршрута" + }, + { + "id": 4, + "title": "Выработка больных вагонов" + }, + { + "id": 5, + "title": "Выработка грязных вагонов" + }, + { + "id": 6, + "title": "Выравнивание шапок" + }, + { + "id": 7, + "title": "Подбор по собственникам" + }, + { + "id": 8, + "title": "Формирование порожняка на отправление" + }, + { + "id": 9, + "title": "Подбор вагонов по качеству загруженного угля" + }, + { + "id": 10, + "title": "Переформирование маршрута" + }, + { + "id": 11, + "title": "Подбор инновационных вагонов" + } + ] +} \ No newline at end of file diff --git a/src/app/coms/wagon.tsx b/src/app/coms/wagon.tsx index cce3b52..07289ed 100644 --- a/src/app/coms/wagon.tsx +++ b/src/app/coms/wagon.tsx @@ -1,7 +1,8 @@ import React from 'react'; +import { IWagon } from 'app/services/api/interfaces'; -export default function Wagon({ wagon }) { - const colors = { +export default function Wagon({ wagon }: { wagon: IWagon }) { + const colors: Record = { НТС: ['#BCF3FF', '#2988AE'], ГК: ['#C8F4C1', '#6EA566'], АТЛ: ['#FFB762', '#ED7817'], @@ -12,6 +13,7 @@ export default function Wagon({ wagon }) { ФГК: ['#FFF3B4', '#F69112'], МЕЧ: ['#ACCDFF', '#7086A9'], }; + let topFillColor = colors[wagon.owner] ? colors[wagon.owner][0] : 'white'; let topBorderColor = colors[wagon.owner] ? colors[wagon.owner][1] @@ -121,7 +123,7 @@ export default function Wagon({ wagon }) { ); } -function WagonTop(type, fillColor, strokeColor) { +function WagonTop(type: string, fillColor: string, strokeColor: string) { switch (type) { case 'Platform': { return ( diff --git a/src/app/routes/_index.tsx b/src/app/routes/_index.tsx index 35593c3..5d57582 100644 --- a/src/app/routes/_index.tsx +++ b/src/app/routes/_index.tsx @@ -15,13 +15,7 @@ export async function loader({ request }: LoaderFunctionArgs) { export default function Index() { return (
-
- - - - - -
+
); } diff --git a/src/app/services/api/json.api.ts b/src/app/services/api/json.api.ts index 9af0e4f..b1d2c1d 100644 --- a/src/app/services/api/json.api.ts +++ b/src/app/services/api/json.api.ts @@ -1,6 +1,12 @@ import fetch from 'node-fetch'; import { env } from 'node:process'; -import { INorm, IOperationType, IStation, IStationData } from './interfaces'; +import { + INorm, + IOperationType, + IStation, + IStationData, + IUser, +} from './interfaces'; const endpoints = { getStations: 'stationsList', @@ -9,10 +15,13 @@ const endpoints = { getOwnersList: 'ownersList', getNorms: 'operationsTypesNorms', getOperationsTypes: 'operationsTypes', + getUsers: 'users', }; export const api = new (class ApiDB { - private readonly url = 'localhost:' + (env.DB_PORT ?? 4000).toString(); + private readonly url = `http://localhost:${( + env.DB_PORT ?? 4000 + ).toString()}/`; constructor() {} async getStations(): Promise { const response = await fetch(this.url + endpoints.getStations, { @@ -54,4 +63,24 @@ export const api = new (class ApiDB { ); return (await response.json()) as IStationData[]; } + async getUsers() { + const response = await fetch(this.url + endpoints.getUsers, { + method: 'GET', + }); + return (await response.json()) as IUser[]; + } + async createUser(user: Omit) { + const response = await fetch(this.url + endpoints.getUsers, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(user), + }); + if (response.ok) { + return (await response.json()) as IUser; + } else { + throw new Error('Unknown answer from db'); + } + } })(); diff --git a/src/app/services/auth.server.ts b/src/app/services/auth.server.ts index 79ad5da..2e87e57 100644 --- a/src/app/services/auth.server.ts +++ b/src/app/services/auth.server.ts @@ -2,30 +2,34 @@ import { Authenticator } from 'remix-auth'; import { sessionStorage } from './session.server'; import { FormStrategy } from 'remix-auth-form'; import { hash, compare } from 'bcrypt'; +import { IUser } from './api/interfaces'; +import { api } from './api/json.api'; -interface User { - login: string; - password: string; -} - -export let authenticator = new Authenticator(sessionStorage); +export let authenticator = new Authenticator(sessionStorage); const saltRounds = 9; authenticator.use( new FormStrategy(async ({ form }) => { - let username = form.get('login'); + let login = form.get('login'); let password = form.get('password'); + const users = await api.getUsers(); - const user = { login: 'null', password: 'null' }; - - return user; + for (const user of users) { + if ( + user.login == login && + (await compare(password, user.password)) + ) { + return user; + } + } + throw new Error('Wrong credentials'); }), 'user-login', ); authenticator.use( - new FormStrategy(async ({ form, context }) => { - let username = form.get('login'); + new FormStrategy(async ({ form }) => { + let login = form.get('login'); let password = form.get('password'); let hashedPassword = ''; @@ -39,7 +43,8 @@ authenticator.use( if (!hashedPassword) { throw ''; } - const user = { login: 'null', password: 'null' }; + + const user = await api.createUser({ login, password: hashedPassword }); return user; }), diff --git a/tsconfig.json b/tsconfig.json index 95cefc2..5de0667 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"], + "exclude":["node_modules"], "compilerOptions": { "lib": ["DOM", "DOM.Iterable", "ES2022"], "isolatedModules": true, @@ -13,6 +14,7 @@ "forceConsistentCasingInFileNames": true, "baseUrl": "./src", "rootDir": "./src", - "noEmit": true + "noEmit": true, + "skipLibCheck":true } }