websockets implemented
This commit is contained in:
@@ -6,20 +6,64 @@ import UserParam from '../models/community/user_param.js';
|
||||
import UserParamType from '../models/type/user_param.js';
|
||||
import { sendAccountActivationEmail, sendPasswordResetEmail } from './emailService.js';
|
||||
import { sequelize } from '../utils/sequelize.js';
|
||||
import { Op } from 'sequelize';
|
||||
import { setUserSession, deleteUserSession } from '../utils/redis.js';
|
||||
import { encrypt } from '../utils/encryption.js';
|
||||
import { notifyUser } from '../utils/socket.js';
|
||||
import Friendship from '../models/community/friendship.js';
|
||||
|
||||
const saltRounds = 10;
|
||||
|
||||
const getFriends = async (userId) => {
|
||||
console.log('getFriends', userId);
|
||||
try {
|
||||
const friendships = await Friendship.findAll({
|
||||
where: {
|
||||
[Op.or]: [
|
||||
{ user1Id: userId },
|
||||
{ user2Id: userId },
|
||||
],
|
||||
accepted: true,
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: User,
|
||||
as: 'friendSender',
|
||||
attributes: ['hashedId', 'username'],
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: 'friendReceiver',
|
||||
attributes: ['hashedId', 'username'],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
console.log('did read out friends');
|
||||
return friendships.map((friendship) => {
|
||||
if (friendship.user1Id === userId) {
|
||||
return friendship.friendReceiver;
|
||||
} else {
|
||||
return friendship.friendSender;
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching friends:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const registerUser = async ({ email, username, password, language }) => {
|
||||
const encryptionKey = process.env.SECRET_KEY;
|
||||
const results = await sequelize.query(
|
||||
`SELECT * FROM community.user WHERE pgp_sym_decrypt(email::bytea, :key) = :email`,
|
||||
{
|
||||
replacements: { key: encryptionKey, email },
|
||||
type: sequelize.QueryTypes.SELECT
|
||||
}
|
||||
);
|
||||
if (results.length > 0) {
|
||||
const encryptedEmail = encrypt(email);
|
||||
const query = `
|
||||
SELECT id FROM community.user
|
||||
WHERE email = :encryptedEmail
|
||||
`;
|
||||
const existingUser = await sequelize.query(query, {
|
||||
replacements: { encryptedEmail },
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
});
|
||||
if (existingUser.length > 0) {
|
||||
throw new Error('emailinuse');
|
||||
}
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
@@ -59,6 +103,13 @@ export const loginUser = async ({ username, password }) => {
|
||||
const authCode = crypto.randomBytes(20).toString('hex');
|
||||
user.authCode = authCode;
|
||||
await user.save();
|
||||
const friends = await getFriends(user.id);
|
||||
for (const friend of friends) {
|
||||
await notifyUser(friend.hashedId, 'friendloginchanged', {
|
||||
userId: user.hashedId,
|
||||
status: 'online',
|
||||
});
|
||||
}
|
||||
const sessionData = {
|
||||
id: user.hashedId,
|
||||
username: user.username,
|
||||
@@ -86,14 +137,14 @@ export const loginUser = async ({ username, password }) => {
|
||||
id: user.hashedId,
|
||||
username: user.username,
|
||||
active: user.active,
|
||||
param: mappedParams,
|
||||
param: mappedParams,
|
||||
authCode
|
||||
};
|
||||
};
|
||||
|
||||
export const logoutUser = async (hashedUserId) => {
|
||||
try {
|
||||
const user = User.findOne({
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
hashedId: hashedUserId
|
||||
}
|
||||
@@ -101,8 +152,14 @@ export const logoutUser = async (hashedUserId) => {
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
const friends = await getFriends(user.id);
|
||||
for (const friend of friends) {
|
||||
await notifyUser(friend.hashedId, 'friendloginchanged', {
|
||||
userId: user.hashedId,
|
||||
status: 'online',
|
||||
});
|
||||
}
|
||||
await deleteUserSession(user.id);
|
||||
console.log('Benutzer erfolgreich aus Redis entfernt:', userId);
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Logout:', error);
|
||||
throw new Error('logoutfailed');
|
||||
|
||||
Reference in New Issue
Block a user