From ccc69709404b3ff363fb430a7094a69da20ae146 Mon Sep 17 00:00:00 2001 From: arttu Date: Tue, 9 Jul 2024 21:20:27 +0300 Subject: [PATCH] Fix login-related issues, show new/delete buttons only to privileged users --- backend/src/middlewares/authenticator.ts | 2 +- backend/src/routes/user.ts | 5 +++-- frontend/src/components/Login.tsx | 3 ++- frontend/src/components/NavBar.tsx | 2 +- frontend/src/components/TableView/TableView.tsx | 2 +- frontend/src/redux/userReducer.ts | 7 ++++++- frontend/src/types.ts | 14 +++++++------- shared/types.d.ts | 6 ------ 8 files changed, 21 insertions(+), 20 deletions(-) diff --git a/backend/src/middlewares/authenticator.ts b/backend/src/middlewares/authenticator.ts index 5bc41349..524356f2 100644 --- a/backend/src/middlewares/authenticator.ts +++ b/backend/src/middlewares/authenticator.ts @@ -30,7 +30,7 @@ const verify = async (token: string, secret: Secret) => { }) } -const getRole = (userGroup: string) => { +export const getRole = (userGroup: string) => { const userGroupToRole = { su: Role.Admin, eu: Role.EditUnrestricted, diff --git a/backend/src/routes/user.ts b/backend/src/routes/user.ts index c0100a27..8dcd241c 100644 --- a/backend/src/routes/user.ts +++ b/backend/src/routes/user.ts @@ -5,6 +5,7 @@ import { SECRET, LOGIN_VALID_MS, USER_CREATION_SECRET } from '../utils/config' import * as bcrypt from 'bcrypt' import { nowDb } from '../utils/db' import { getAllUsers, getUserDetails } from '../services/user' +import { getRole } from '../middlewares/authenticator' const router = Router() @@ -14,7 +15,7 @@ router.post('/login', async (req, res) => { where: { user_name: username, }, - select: { user_name: true, password: true, user_id: true }, + select: { user_name: true, password: true, user_id: true, now_user_group: true }, }) const passwordMatches = result && (await bcrypt.compare(password as string, result.password as string)) @@ -25,7 +26,7 @@ router.post('/login', async (req, res) => { expiresIn: LOGIN_VALID_MS, }) - return res.status(200).send({ token, username: result.user_name }) + return res.status(200).send({ token, username: result.user_name, role: getRole(result.now_user_group ?? '') }) }) router.get('/all', async (_req, res) => { diff --git a/frontend/src/components/Login.tsx b/frontend/src/components/Login.tsx index cb1f95cf..86e06bc3 100644 --- a/frontend/src/components/Login.tsx +++ b/frontend/src/components/Login.tsx @@ -4,6 +4,7 @@ import { useDispatch } from 'react-redux' import { Box, Button, CircularProgress, Container, Stack, TextField, Typography } from '@mui/material' import { useNavigate } from 'react-router-dom' import { api } from '../redux/api' +import { Role } from '@/types' export const Login = () => { const [username, setUsername] = useState('') @@ -32,7 +33,7 @@ export const Login = () => { useEffect(() => { if (data && data.token) { - dispatch(setUser({ username: data.username, token: data.token })) + dispatch(setUser({ username: data.username, token: data.token, role: data.role || Role.ReadOnly })) // Reset api cache, so that we won't show guest data to logged user dispatch(api.util.resetApiState()) navigate('/') diff --git a/frontend/src/components/NavBar.tsx b/frontend/src/components/NavBar.tsx index 063c4fb2..d0ba1ce4 100644 --- a/frontend/src/components/NavBar.tsx +++ b/frontend/src/components/NavBar.tsx @@ -107,7 +107,7 @@ export const NavBar = () => { ) } - + console.log({ user }) return ( diff --git a/frontend/src/components/TableView/TableView.tsx b/frontend/src/components/TableView/TableView.tsx index 4677320f..f070497b 100644 --- a/frontend/src/components/TableView/TableView.tsx +++ b/frontend/src/components/TableView/TableView.tsx @@ -117,7 +117,7 @@ export const TableView = ({ }, [table, columnFilters, sorting]) if (!data) return - console.log({ user }) + return ( {user && diff --git a/frontend/src/redux/userReducer.ts b/frontend/src/redux/userReducer.ts index 22ab162f..d21907fa 100644 --- a/frontend/src/redux/userReducer.ts +++ b/frontend/src/redux/userReducer.ts @@ -1,9 +1,14 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { api } from '@/redux/api' -import { FrontendUser } from '@shared/types' import { PersonDetailsType } from '@/backendTypes' import { Role } from '@/types' +export type FrontendUser = { + token: string + username: string + role: Role +} + type LoginData = { username: string; password: string } const userApi = api.injectEndpoints({ diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 9065eb5c..5d4fd16f 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -1,9 +1,9 @@ export enum Role { - Admin, - EditUnrestricted, - EditRestricted, - NowOffice, - Project, - ProjectPrivate, - ReadOnly, + Admin = 1, + EditUnrestricted = 2, + EditRestricted = 3, + NowOffice = 4, + Project = 5, + ProjectPrivate = 6, + ReadOnly = 7, } diff --git a/shared/types.d.ts b/shared/types.d.ts index 5da7e55e..e69de29b 100644 --- a/shared/types.d.ts +++ b/shared/types.d.ts @@ -1,6 +0,0 @@ - -export type FrontendUser = { - token: string - username: string - role: Role -}