Skip to content

Commit

Permalink
add auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Dec 28, 2024
1 parent e8f9832 commit 6a5e242
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
10 changes: 7 additions & 3 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PageNotFound from './pages/PageNotFound.jsx';
import MainPage from './pages/MainPage.jsx';
import LoginPage from './pages/LoginPage.jsx';
import router from './utils/routes.js';
import AuthProvider from './context/AuthProvider.jsx';
import AuthProvider, { PrivateRoute } from './context/AuthProvider.jsx';

const App = () => {
const { t } = useTranslation();
Expand All @@ -22,8 +22,12 @@ const App = () => {
</Navbar>
<AuthProvider>
<Routes>
<Route path={router.mainPath} element={<MainPage />} />
<Route path={router.loginPath} element={<LoginPage />} />
<Route path={router.mainPath} element={(
<PrivateRoute>
<MainPage />
</PrivateRoute>)} />
<Route path={router.main} element={(<MainPage />)} />
<Route path={router.login} element={<LoginPage />} />
<Route path="*" element={<PageNotFound />} />
</Routes>
</AuthProvider>
Expand Down
Binary file added frontend/src/assets/404-error.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed frontend/src/assets/notFound.jpg
Binary file not shown.
5 changes: 3 additions & 2 deletions frontend/src/components/MessageForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const MessageForm = () => {
return (
<div className="mt-auto px-5 py-3">
<Form className="py-1 border rounded-2">
<Form.Group className="input-group" hasValidation>
<Form.Group className="input-group">
<Form.Control
type="text"
placeholder="Введите сообщение..."
className="border-0 p-0 ps-2"
value=""
value=""
// onChange={handleChange}
/>
<Button type="submit" variant="outline-secondary" disabled readOnly className="btn-group-vertical">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/context/AuthProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import useAuth from '../hooks/index.jsx';
import AuthContext from '../context/index.jsx';
import router from '../utils/routes.js';

// eslint-disable-next-line react/prop-types
const AuthProvider = ({ children }) => {
Expand All @@ -18,4 +21,13 @@ const AuthProvider = ({ children }) => {
);
};

export const PrivateRoute = ({ children }) => {
const auth = useAuth();
const navigate = useNavigate();

return (
auth.loggedIn ? children : navigate(router.mainPath)
);
};

export default AuthProvider;
4 changes: 1 addition & 3 deletions frontend/src/pages/LoginPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const LoginPage = () => {
console.log(res);
localStorage.setItem('token', res.data.token);
auth.logIn();
navigate(routes.mainPath);
navigate(router.main);
} catch (err) {
formik.setSubmitting(false);
if (axios.isAxiosError(err) && err.response.status === 401) {
Expand Down Expand Up @@ -65,7 +65,6 @@ const LoginPage = () => {
autoComplete="username"
required
placeholder={t('loginForm.username')}
id="username"
onChange={formik.handleChange}
value={formik.values.username}
isInvalid={authFailed}
Expand All @@ -79,7 +78,6 @@ const LoginPage = () => {
autoComplete="current-password"
required
placeholder={t('loginForm.password')}
id="password"
onChange={formik.handleChange}
value={formik.values.password}
isInvalid={authFailed}
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/pages/PageNotFound.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import pageNotFound from '../assets/notFound.jpg';
import pageNotFound from '../assets/404-error.jpg';

const PageNotFound = () => (
<div className="text-center">
<img src={pageNotFound} alt="Страница не найдена" className="img-fluid h-15" />
<img
src={pageNotFound}
alt="Страница не найдена"
className="img-fluid h-15"
/>
<h1 className="h4 text-muted">Страница не найдена</h1>
<p className="text-muted">
Но вы можете перейти
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/utils/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// const apiPath = '/api/v1';

export default {
login: '/login',
loginPath: '/api/v1/login',
mainPath: '/',
mainPath: '/api/v1/channels',
main: '/',
};

0 comments on commit 6a5e242

Please sign in to comment.