fix: fix login data problem and add workspace handler

This commit is contained in:
moonrailgun 2023-09-03 21:30:19 +08:00
parent ef1801f531
commit 2ccb084959
4 changed files with 43 additions and 7 deletions

View File

@ -3,8 +3,23 @@ import { Outlet } from 'react-router-dom';
import { NavItem } from '../components/NavItem'; import { NavItem } from '../components/NavItem';
import { UserOutlined } from '@ant-design/icons'; import { UserOutlined } from '@ant-design/icons';
import { Button, Dropdown } from 'antd'; import { Button, Dropdown } from 'antd';
import { useUserStore } from '../store/user';
export const Layout: React.FC = React.memo(() => { export const Layout: React.FC = React.memo(() => {
const workspaces = useUserStore((state) => {
const userInfo = state.info;
if (userInfo) {
return userInfo.workspaces.map((w) => ({
id: w.workspace.id,
name: w.workspace.name,
role: w.role,
current: userInfo.currentWorkspace.id === w.workspace.id,
}));
}
return [];
});
return ( return (
<div className="flex flex-col h-full"> <div className="flex flex-col h-full">
<div className="flex items-center bg-gray-100 px-4"> <div className="flex items-center bg-gray-100 px-4">
@ -24,6 +39,15 @@ export const Layout: React.FC = React.memo(() => {
placement="bottomRight" placement="bottomRight"
menu={{ menu={{
items: [ items: [
{
key: 'workspaces',
label: 'Workspaces',
children: workspaces.map((w) => ({
key: w.id,
label: `${w.name}${w.current ? '(current)' : ''}`,
disabled: w.current,
})),
},
{ {
key: 'logout', key: 'logout',
label: 'Logout', label: 'Logout',

View File

@ -25,7 +25,15 @@ export const useUserStore = create<UserState>(() => ({
info: null, info: null,
})); }));
export function setUserInfo(info: UserInfo) { export function setUserInfo(info: UserLoginInfo) {
if (!info.currentWorkspace && info.workspaces[0]) {
// Make sure currentWorkspace existed
info.currentWorkspace = {
id: info.workspaces[0].workspace.id,
name: info.workspaces[0].workspace.name,
};
}
useUserStore.setState({ useUserStore.setState({
info, info,
}); });

View File

@ -2,10 +2,12 @@ import { findUser } from '../model/user';
import passport from 'passport'; import passport from 'passport';
import { Handler } from 'express'; import { Handler } from 'express';
import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt'; import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
import { nanoid } from 'nanoid';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
import { hashUuid } from '../utils/common';
import dayjs from 'dayjs';
export const jwtSecret = process.env.JWT_SECRET || nanoid(); export const jwtSecret =
process.env.JWT_SECRET || hashUuid(dayjs().format('YYYYMMDD'));
export const jwtIssuer = process.env.JWT_ISSUER || 'tianji.msgbyte.com'; export const jwtIssuer = process.env.JWT_ISSUER || 'tianji.msgbyte.com';
export const jwtAudience = process.env.JWT_AUDIENCE || 'msgbyte.com'; export const jwtAudience = process.env.JWT_AUDIENCE || 'msgbyte.com';

View File

@ -55,7 +55,7 @@ export async function createAdminUser(username: string, password: string) {
); );
} }
const user = await prisma.user.create({ let user = await prisma.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -77,13 +77,14 @@ export async function createAdminUser(username: string, password: string) {
}); });
if (user.workspaces[0]) { if (user.workspaces[0]) {
prisma.user.update({ user = await prisma.user.update({
where: { where: {
id: user.id, id: user.id,
}, },
data: { data: {
currentWorkspaceId: user.workspaces[0].workspace.id, currentWorkspaceId: user.workspaces[0].workspace.id,
}, },
select: createUserSelect,
}); });
} }
@ -101,7 +102,7 @@ export async function createUser(username: string, password: string) {
throw new Error('User already exists'); throw new Error('User already exists');
} }
const user = await prisma.user.create({ let user = await prisma.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
@ -123,13 +124,14 @@ export async function createUser(username: string, password: string) {
}); });
if (user.workspaces[0]) { if (user.workspaces[0]) {
prisma.user.update({ user = await prisma.user.update({
where: { where: {
id: user.id, id: user.id,
}, },
data: { data: {
currentWorkspaceId: user.workspaces[0].workspace.id, currentWorkspaceId: user.workspaces[0].workspace.id,
}, },
select: createUserSelect,
}); });
} }