diff --git a/platforms/evoting-api/src/services/PollService.ts b/platforms/evoting-api/src/services/PollService.ts index c9ac7ccd..8911b567 100644 --- a/platforms/evoting-api/src/services/PollService.ts +++ b/platforms/evoting-api/src/services/PollService.ts @@ -29,24 +29,18 @@ export class PollService { // Get user's group memberships if userId is provided let userGroupIds: string[] = []; if (userId) { - const user = await this.userRepository.findOne({ - where: { id: userId }, - relations: ['followers', 'following'] // This will include group memberships - }); + // Get groups where user is a member, admin, or participant + const groupRepository = AppDataSource.getRepository(Group); + const userGroups = await groupRepository + .createQueryBuilder('group') + .leftJoin('group.members', 'member') + .leftJoin('group.admins', 'admin') + .leftJoin('group.participants', 'participant') + .where('member.id = :userId OR admin.id = :userId OR participant.id = :userId', { userId }) + .getMany(); - if (user) { - // Get groups where user is a member, admin, or participant - const groupRepository = AppDataSource.getRepository(Group); - const userGroups = await groupRepository - .createQueryBuilder('group') - .leftJoin('group.members', 'member') - .leftJoin('group.admins', 'admin') - .leftJoin('group.participants', 'participant') - .where('member.id = :userId OR admin.id = :userId OR participant.id = :userId', { userId }) - .getMany(); - - userGroupIds = userGroups.map(group => group.id); - } + userGroupIds = userGroups.map(group => group.id); + console.log(`User ${userId} is member/admin/participant in groups:`, userGroupIds); } const [allPolls, total] = await this.pollRepository.findAndCount({ diff --git a/platforms/group-charter-manager-api/src/controllers/AuthController.ts b/platforms/group-charter-manager-api/src/controllers/AuthController.ts index 32551294..fcb4a42c 100644 --- a/platforms/group-charter-manager-api/src/controllers/AuthController.ts +++ b/platforms/group-charter-manager-api/src/controllers/AuthController.ts @@ -61,7 +61,7 @@ export class AuthController { } const { user, token } = - await this.userService.findOrCreateUser(ename); + await this.userService.findUserByEname(ename); const data = { user: { @@ -76,6 +76,18 @@ export class AuthController { res.status(200).send(); } catch (error) { console.error("Error during login:", error); + + // Provide more specific error messages + if (error instanceof Error) { + if (error.message.includes("not found")) { + return res.status(404).json({ + error: "User not found", + message: error.message, + details: "Please ensure you have the correct ename or contact support to create an account." + }); + } + } + res.status(500).json({ error: "Internal server error" }); } }; diff --git a/platforms/group-charter-manager-api/src/services/UserService.ts b/platforms/group-charter-manager-api/src/services/UserService.ts index 17035b5a..57b7719f 100644 --- a/platforms/group-charter-manager-api/src/services/UserService.ts +++ b/platforms/group-charter-manager-api/src/services/UserService.ts @@ -21,21 +21,54 @@ export class UserService { return await this.userRepository.save(user); } - async findOrCreateUser( + async findUserByEname( ename: string ): Promise<{ user: User; token: string }> { - // Strip @ prefix from ename if present (database stores without @) - const cleanEname = ename.startsWith('@') ? ename.slice(1) : ename; + let user: User | null = null; - let user = await this.userRepository.findOne({ - where: { ename: cleanEname }, + console.log(`🔍 Looking for user with ename: '${ename}'`); + + // Try to find user with the exact ename as provided + user = await this.userRepository.findOne({ + where: { ename: ename }, }); - + + if (user) { + console.log(`✅ Found user with exact ename: '${ename}'`); + } else { + // If not found and ename starts with @, try without @ + if (ename.startsWith('@')) { + const enameWithoutAt = ename.slice(1); + console.log(`🔍 Trying without @ prefix: '${enameWithoutAt}'`); + user = await this.userRepository.findOne({ + where: { ename: enameWithoutAt }, + }); + if (user) { + console.log(`✅ Found user without @ prefix: '${enameWithoutAt}'`); + } + } + + // If not found and ename doesn't start with @, try with @ + if (!user && !ename.startsWith('@')) { + const enameWithAt = `@${ename}`; + console.log(`🔍 Trying with @ prefix: '${enameWithAt}'`); + user = await this.userRepository.findOne({ + where: { ename: enameWithAt }, + }); + if (user) { + console.log(`✅ Found user with @ prefix: '${enameWithAt}'`); + } + } + } + + // If still no user found, throw an error - never create new users if (!user) { - user = await this.createBlankUser(cleanEname); + console.log(`❌ No user found for ename: '${ename}' (tried with/without @ prefix)`); + throw new Error(`User with ename '${ename}' not found. Cannot create new users automatically.`); } const token = signToken({ userId: user.id }); + console.log(`🎉 Successfully authenticated user: ${user.ename} (ID: ${user.id})`); return { user, token }; }