diff --git a/Lab5/Bookfill/data.json b/Lab5/Bookfill/data.json index dcd846a..1fe10c4 100644 --- a/Lab5/Bookfill/data.json +++ b/Lab5/Bookfill/data.json @@ -13,13 +13,6 @@ "name": "Pek", "role": "user", "id": 2 - }, - { - "email": "Goka.2004@yandex.ru", - "password": "1111", - "name": "Admin1", - "role": "user", - "id": 3 } ], "types": [ diff --git a/Lab5/Bookfill/src/components/api/ApiService.js b/Lab5/Bookfill/src/components/api/ApiService.js index 3fe309c..df38a43 100644 --- a/Lab5/Bookfill/src/components/api/ApiService.js +++ b/Lab5/Bookfill/src/components/api/ApiService.js @@ -24,6 +24,10 @@ class ApiService { async delete(id) { return ApiClient.delete(`${this.url}/${id}`); } + + async getOneByName(name) { + return ApiClient.get(`${this.url}?name=${name}`); + } } export default ApiService; diff --git a/Lab5/Bookfill/src/components/api/AuthService.js b/Lab5/Bookfill/src/components/api/AuthService.js index 95d04ea..2b28d02 100644 --- a/Lab5/Bookfill/src/components/api/AuthService.js +++ b/Lab5/Bookfill/src/components/api/AuthService.js @@ -6,13 +6,19 @@ class AuthService extends ApiService { } async loginUser(credentials) { - const users = await this.getAll(); - // eslint-disable-next-line max-len - const user = users.find((u) => u.email === credentials.email && u.password === credentials.password); + const user = await this.getOneByName(credentials.name); if (!user) { + throw new Error('Invalid name'); + } + const item = user[0]; + console.log(item.name); + if (item && item.password !== credentials.password) { throw new Error('Invalid email or password'); } - return user; + if (item && item.email !== credentials.email) { + throw new Error('Invalid email or password'); + } + return item; } async registerUser(userData) { diff --git a/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx b/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx index 03eabc1..a2a6b2f 100644 --- a/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx +++ b/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx @@ -1,6 +1,7 @@ import { useContext, useState } from 'react'; import { Form, Button } from 'react-bootstrap'; import { Link } from 'react-router-dom'; +import toast from 'react-hot-toast'; import AuthService from '../../api/AuthService'; import { AuthContext } from '../context/AuthContex.jsx'; @@ -22,7 +23,7 @@ const LoginForm = () => { payload: user, }); } catch (error) { - console.error('Login error', error); + toast.error('Entry ERROR'); } }; @@ -35,7 +36,7 @@ const LoginForm = () => { payload: user, }); } catch (error) { - console.error('Register error', error); + toast.error('Register ERROR'); } };