diff --git a/src/components/header.jsx b/src/components/header.jsx
index 14ca77a..6075eb9 100644
--- a/src/components/header.jsx
+++ b/src/components/header.jsx
@@ -54,7 +54,7 @@ const links = [
const HeaderSearch = () => {
const { classes } = useStyles();
- const { role } = useAuth();
+ const { user } = useAuth();
const navigate = useNavigate();
const items = links.map((link) => (
@@ -78,9 +78,13 @@ const HeaderSearch = () => {
{items}
-
+
-
+ {user ? (
+
+ ) : (
+
+ )}
diff --git a/src/pages/auth.jsx b/src/pages/auth.jsx
index 1a653ba..5887701 100644
--- a/src/pages/auth.jsx
+++ b/src/pages/auth.jsx
@@ -2,32 +2,50 @@ import { TextInput, PasswordInput, Checkbox, Anchor, Paper, Title, Container, Gr
import { isNotEmpty, useForm } from '@mantine/form';
import { useAuth } from '../tools/auth-provider';
import { useNavigate } from 'react-router-dom';
-import { useEffect } from 'react';
+import { useEffect, useState } from 'react';
+import setNotification from './errors/error-notification';
+import API from '../services/api';
const Authentication = () => {
- const { setRole } = useAuth();
+ const { user, setUser } = useAuth();
const navigate = useNavigate();
+ const [isLoading, setIsLoading] = useState(false);
+
+ useEffect(() => {
+ if (user) navigate('/');
+ }, [user]);
+
const form = useForm({
initialValues: {
- name: '',
+ login: '',
password: '',
remember: false,
},
validate: {
- name: isNotEmpty('Name is required'),
+ login: isNotEmpty('Login is required'),
password: isNotEmpty('Password is required'),
},
});
- useEffect(() => {
- localStorage.setItem('role', form.values.name);
- }, [form.values.name]);
-
const handleLogin = (e) => {
e.preventDefault();
if (form.validate().hasErrors) return;
- setRole(form.values.name);
- navigate('/');
+ setIsLoading(true);
+ API.login(form.values)
+ .then((res) => {
+ if (res.status === 200) {
+ setUser(res.data);
+ localStorage.setItem('user', res.data);
+ navigate('/');
+ } else {
+ setNotification(true, res.message);
+ setIsLoading(false);
+ }
+ })
+ .catch((err) => {
+ setNotification(true, err.message);
+ setIsLoading(false);
+ });
};
return (
@@ -41,7 +59,7 @@ const Authentication = () => {
diff --git a/src/pages/playlists/playlist-view-editor.jsx b/src/pages/playlists/playlist-view-editor.jsx
index 24fd6df..19736c2 100644
--- a/src/pages/playlists/playlist-view-editor.jsx
+++ b/src/pages/playlists/playlist-view-editor.jsx
@@ -5,7 +5,6 @@ import { useState } from 'react';
const PlaylistViewEditor = ({ item, handler, buttonText, APICall }) => {
const handleClose = (playlist) => {
form.reset();
- console.log('call jiezf');
handler(playlist);
};
diff --git a/src/services/api.js b/src/services/api.js
index 8f26a5e..392a8fa 100644
--- a/src/services/api.js
+++ b/src/services/api.js
@@ -7,11 +7,14 @@ const caller = (url = '/api') => {
};
const API = {
+ login(data) {
+ return caller().post('/auth/login', data);
+ },
listPlaylists(data) {
return caller().get('/playlist', data);
},
createPlaylist(data) {
- return caller().put('/playlist', data);
+ return caller().post('/playlist', data);
},
updatePlaylist(playlistId, data) {
return caller().post(`/playlist/${playlistId}/update`, data);
diff --git a/src/tools/auth-provider.js b/src/tools/auth-provider.js
index 38e931b..b2e8dd5 100644
--- a/src/tools/auth-provider.js
+++ b/src/tools/auth-provider.js
@@ -3,13 +3,13 @@ import { createContext, useState, useContext } from 'react';
const AuthContext = createContext();
const AuthProvider = ({ children }) => {
- const [role, setRole] = useState(localStorage.getItem('role') ?? 'user');
+ const [user, setUser] = useState(localStorage.getItem('user'));
- return {children};
+ return {children};
};
const logout = () => {
- localStorage.removeItem('role');
+ localStorage.removeItem('user');
window.location.href = '/auth';
};
diff --git a/src/tools/grant-access.jsx b/src/tools/grant-access.jsx
index 1e1291d..baab5df 100644
--- a/src/tools/grant-access.jsx
+++ b/src/tools/grant-access.jsx
@@ -1,8 +1,8 @@
import { useAuth } from './auth-provider';
const GrantAccess = ({ roles, children }) => {
- const { role } = useAuth();
- return roles.includes(role) ? children : null;
+ const { user } = useAuth();
+ return roles.includes(user) ? children : null;
};
export default GrantAccess;