1
0
Fork 0

fixed api

This commit is contained in:
minicx 2023-12-22 15:54:04 +03:00
parent 655f8f441c
commit 0f7c29ecdc
7 changed files with 598 additions and 517 deletions

3
.gitignore vendored
View File

@ -7,4 +7,5 @@ src/database/prisma/client
*.sqlite
package-lock.json
.vscode
*.db*
*.db*
db.json

File diff suppressed because it is too large Load Diff

View File

@ -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<string, string[]> = {
НТС: ['#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 (

View File

@ -15,13 +15,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
export default function Index() {
return (
<div id="control-form">
<div id="control-header" className="flex items-end">
<Wagon wagon={{ type: 'Hoppers', isSick: true }} />
<Wagon wagon={{ type: 'Platform' }} />
<Wagon wagon={{ type: 'CoveredCarriage' }} />
<Wagon wagon={{ type: 'Hopper' }} />
<Wagon wagon={{ type: 'Tank', owner: 'НТС' }} />
</div>
<div id="control-header" className="flex items-end"></div>
</div>
);
}

View File

@ -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<IStation[]> {
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<IUser, 'id'>) {
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');
}
}
})();

View File

@ -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<User>(sessionStorage);
export let authenticator = new Authenticator<IUser>(sessionStorage);
const saltRounds = 9;
authenticator.use(
new FormStrategy(async ({ form }) => {
let username = <string>form.get('login');
let login = <string>form.get('login');
let password = <string>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 = <string>form.get('login');
new FormStrategy(async ({ form }) => {
let login = <string>form.get('login');
let password = <string>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;
}),

View File

@ -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
}
}