mirror of
https://github.com/philipredstone/relnet.git
synced 2025-07-08 22:56:42 +02:00
change ui
This commit is contained in:
26
src/app.ts
26
src/app.ts
@ -7,14 +7,35 @@ import networkRoutes from './routes/network.routes';
|
||||
import peopleRoutes from './routes/people.routes';
|
||||
import relationshipRoutes from './routes/relationship.routes';
|
||||
import path from 'node:path';
|
||||
import helmet from "helmet";
|
||||
import helmet from 'helmet';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app: Application = express();
|
||||
|
||||
// Middleware
|
||||
app.use(helmet());
|
||||
// Apply Helmet to API routes only
|
||||
app.use(
|
||||
'/api',
|
||||
helmet({
|
||||
contentSecurityPolicy: {
|
||||
directives: {
|
||||
defaultSrc: ["'self'"],
|
||||
scriptSrc: ["'self'", "'unsafe-inline'", "'unsafe-eval'"],
|
||||
styleSrc: ["'self'", "'unsafe-inline'"],
|
||||
imgSrc: ["'self'", 'data:'],
|
||||
connectSrc: ["'self'", 'http://localhost:*', 'ws://localhost:*'],
|
||||
fontSrc: ["'self'", 'data:'],
|
||||
objectSrc: ["'none'"],
|
||||
mediaSrc: ["'self'"],
|
||||
frameSrc: ["'none'"],
|
||||
},
|
||||
},
|
||||
crossOriginResourcePolicy: { policy: 'cross-origin' },
|
||||
crossOriginEmbedderPolicy: false,
|
||||
})
|
||||
);
|
||||
|
||||
app.use(express.json());
|
||||
app.use(cookieParser());
|
||||
app.use(
|
||||
@ -30,7 +51,6 @@ app.use('/api/networks', networkRoutes);
|
||||
app.use('/api/networks', peopleRoutes);
|
||||
app.use('/api/networks', relationshipRoutes);
|
||||
|
||||
|
||||
app.use(express.static(path.join(__dirname, '../frontend/dist/')));
|
||||
|
||||
app.use((req, res, next) => {
|
||||
|
@ -1,8 +1,12 @@
|
||||
import { Request, Response } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import User, { IUser } from '../models/user.model';
|
||||
import Network from '../models/network.model';
|
||||
import Person from '../models/person.model';
|
||||
import Relationship from '../models/relationship.model';
|
||||
import { UserRequest } from '../types/express';
|
||||
import { validationResult } from 'express-validator';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
// JWT secret from environment variables
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'your_jwt_secret_key_change_this';
|
||||
@ -62,6 +66,10 @@ export const register = async (req: Request, res: Response): Promise<void> => {
|
||||
// Save user to database
|
||||
await user.save();
|
||||
|
||||
// Create a sample demo network
|
||||
// Fix: Ensure user._id is treated as ObjectId
|
||||
await createSampleDemoNetwork(user._id);
|
||||
|
||||
// Generate JWT token
|
||||
const token = generateToken(user);
|
||||
|
||||
@ -163,3 +171,73 @@ export const getCurrentUser = async (req: UserRequest, res: Response): Promise<v
|
||||
res.status(500).json({ message: 'Server error' });
|
||||
}
|
||||
};
|
||||
|
||||
// Create a sample demo network for new users
|
||||
// Fix: Update parameter type to accept both string and ObjectId
|
||||
const createSampleDemoNetwork = async (userId: mongoose.Types.ObjectId | string): Promise<void> => {
|
||||
try {
|
||||
// Ensure userId is an ObjectId
|
||||
const userObjectId = typeof userId === 'string' ? new mongoose.Types.ObjectId(userId) : userId;
|
||||
|
||||
// Create a demo network
|
||||
const network = new Network({
|
||||
name: 'My Sample Network',
|
||||
description: 'A demo network to help you get started',
|
||||
owner: userObjectId,
|
||||
isPublic: false,
|
||||
});
|
||||
|
||||
await network.save();
|
||||
|
||||
// Create sample people with better spacing
|
||||
const people = [
|
||||
{ firstName: 'John', lastName: 'Smith', position: { x: 200, y: 200 } },
|
||||
{ firstName: 'Emma', lastName: 'Johnson', position: { x: 600, y: 200 } },
|
||||
{ firstName: 'Michael', lastName: 'Williams', position: { x: 200, y: 600 } },
|
||||
{ firstName: 'Sarah', lastName: 'Brown', position: { x: 600, y: 600 } },
|
||||
{ firstName: 'David', lastName: 'Jones', position: { x: 800, y: 400 } },
|
||||
{ firstName: 'Lisa', lastName: 'Garcia', position: { x: 400, y: 400 } },
|
||||
];
|
||||
|
||||
// Fix: Update the type to accept string or ObjectId
|
||||
const savedPeople: { [key: string]: mongoose.Types.ObjectId | string } = {};
|
||||
|
||||
// Create each person
|
||||
for (const person of people) {
|
||||
const newPerson = new Person({
|
||||
firstName: person.firstName,
|
||||
lastName: person.lastName,
|
||||
network: network._id,
|
||||
position: person.position,
|
||||
});
|
||||
|
||||
await newPerson.save();
|
||||
savedPeople[`${person.firstName}${person.lastName}`] = newPerson._id;
|
||||
}
|
||||
|
||||
// Create relationships between people
|
||||
const relationships = [
|
||||
{ source: 'JohnSmith', target: 'EmmaJohnson', type: 'freund' },
|
||||
{ source: 'EmmaJohnson', target: 'MichaelWilliams', type: 'familie' },
|
||||
{ source: 'MichaelWilliams', target: 'SarahBrown', type: 'arbeitskolleg' },
|
||||
{ source: 'SarahBrown', target: 'DavidJones', type: 'freund' },
|
||||
{ source: 'DavidJones', target: 'LisaGarcia', type: 'partner' },
|
||||
{ source: 'JohnSmith', target: 'DavidJones', type: 'arbeitskolleg' },
|
||||
];
|
||||
|
||||
// Create each relationship
|
||||
for (const rel of relationships) {
|
||||
const newRelationship = new Relationship({
|
||||
source: savedPeople[rel.source],
|
||||
target: savedPeople[rel.target],
|
||||
type: rel.type,
|
||||
network: network._id,
|
||||
});
|
||||
|
||||
await newRelationship.save();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating sample network:', error);
|
||||
// Don't throw the error, just log it so that registration can continue
|
||||
}
|
||||
};
|
||||
|
@ -66,7 +66,7 @@ export const addPerson = async (req: UserRequest, res: Response): Promise<void>
|
||||
lastName,
|
||||
birthday: birthday || undefined,
|
||||
network: networkId,
|
||||
position: position || { x: 100 + Math.random() * 500, y: 100 + Math.random() * 400 },
|
||||
position: position || { x: 100 + Math.random() * 800, y: 100 + Math.random() * 600 },
|
||||
});
|
||||
|
||||
await person.save();
|
||||
|
Reference in New Issue
Block a user