Fix login-related issues, show new/delete buttons only to privileged users

This commit is contained in:
arttu
2024-07-09 21:20:27 +03:00
parent b74a85e0ce
commit ccc6970940
8 changed files with 21 additions and 20 deletions
+1 -1
View File
@@ -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,
+3 -2
View File
@@ -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) => {
+2 -1
View File
@@ -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('/')
+1 -1
View File
@@ -107,7 +107,7 @@ export const NavBar = () => {
</MenuItem>
)
}
console.log({ user })
return (
<AppBar position="static">
<Stack spacing={10} direction="row" marginLeft={2} justifyContent="space-between">
@@ -117,7 +117,7 @@ export const TableView = <T extends MRT_RowData>({
}, [table, columnFilters, sorting])
if (!data) return <CircularProgress />
console.log({ user })
return (
<Box>
{user &&
+6 -1
View File
@@ -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({
+7 -7
View File
@@ -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,
}
-6
View File
@@ -1,6 +0,0 @@
export type FrontendUser = {
token: string
username: string
role: Role
}