From 933c8157cf33785cfbbbf4cb10934c02b74e842c Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sun, 7 Jan 2024 20:10:38 +0400 Subject: [PATCH] 5_8 --- .../src/components/api/AuthService.js | 14 +++++++----- .../components/logins/context/AuthContex.jsx | 3 ++- .../src/components/logins/form/LoginForm.jsx | 22 +++++++++---------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Lab5/Bookfill/src/components/api/AuthService.js b/Lab5/Bookfill/src/components/api/AuthService.js index bc6e23a..db895f8 100644 --- a/Lab5/Bookfill/src/components/api/AuthService.js +++ b/Lab5/Bookfill/src/components/api/AuthService.js @@ -5,20 +5,22 @@ class AuthService extends ApiService { super('http://localhost:8081/users'); // Указываем базовый URL для аутентификации } - async registerUser(userInfo) { - return this.create(userInfo); - } - async loginUser(credentials) { - const users = await this.getAll(); // Получаем всех пользователей + const users = await this.getAll(); // eslint-disable-next-line max-len - const user = users.find((u) => u.email === credentials.email && u.password === credentials.password && u.name === credentials.name); + const user = users.find((u) => u.email === credentials.email && u.password === credentials.password); if (!user) { throw new Error('Invalid email or password'); } return user; } + async registerUser(userData) { + // Вероятно вы хотите здесь сохранять пользователя в базе данных? + const registeredUser = await this.create(userData); + return registeredUser; + } + async setRole(userId, role) { return this.update(userId, { role }); } diff --git a/Lab5/Bookfill/src/components/logins/context/AuthContex.jsx b/Lab5/Bookfill/src/components/logins/context/AuthContex.jsx index a8c3e67..0bde443 100644 --- a/Lab5/Bookfill/src/components/logins/context/AuthContex.jsx +++ b/Lab5/Bookfill/src/components/logins/context/AuthContex.jsx @@ -8,9 +8,10 @@ const initialState = { const AuthReducer = (state, action) => { switch (action.type) { case 'LOGIN': + case 'REGISTER': return { ...state, - user: action.payload, // Предполагается, что payload содержит данные пользователя. + user: action.payload, }; case 'LOGOUT': return { diff --git a/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx b/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx index 6249b4f..03eabc1 100644 --- a/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx +++ b/Lab5/Bookfill/src/components/logins/form/LoginForm.jsx @@ -15,31 +15,31 @@ const LoginForm = () => { const handleSubmit = async (e) => { e.preventDefault(); - const { user, token } = await AuthService.loginUser(credentials); - localStorage.setItem('auth_token', token); // Сохраняем токен в localStorage - dispatch({ - type: 'LOGIN', - payload: { user, token }, // Предполагается, что payload содержит объект с user и token - }); + try { + const user = await AuthService.loginUser(credentials); + dispatch({ + type: 'LOGIN', + payload: user, + }); + } catch (error) { + console.error('Login error', error); + } }; const handleRegister = async (e) => { e.preventDefault(); try { - const { user, token } = await AuthService.registerUser({ ...credentials, role: 'user' }); - localStorage.setItem('auth_token', token); + const user = await AuthService.registerUser({ ...credentials, role: 'user' }); dispatch({ type: 'REGISTER', - payload: { user, token }, // Предполагается, что payload содержит объект с user и token + payload: user, }); } catch (error) { console.error('Register error', error); - // Обработать ошибки регистрации } }; const handleLogout = () => { - localStorage.removeItem('auth_token'); dispatch({ type: 'LOGOUT' }); };