Skip to content

Commit

Permalink
Merge pull request #1876 from Giveth/fix/email_verification_check
Browse files Browse the repository at this point in the history
separated checking for solana and etherium users
  • Loading branch information
kkatusic authored Nov 26, 2024
2 parents 04b3a19 + b77a500 commit 4c4ae45
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/resolvers/projectResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5529,6 +5529,7 @@ function editProjectUpdateTestCases() {
walletAddress: generateRandomEtheriumAddress(),
loginType: 'wallet',
firstName: 'testEditProjectUpdateFateme',
isEmailVerified: true,
}).save();
const accessToken = await generateTestAccessToken(user.id);
const projectUpdateCount = await ProjectUpdate.count();
Expand Down Expand Up @@ -5644,6 +5645,7 @@ function deleteProjectUpdateTestCases() {
walletAddress: generateRandomEtheriumAddress(),
loginType: 'wallet',
firstName: 'testDeleteProjectUpdateFateme',
isEmailVerified: true,
}).save();
const accessToken = await generateTestAccessToken(user.id);
const projectUpdateCount = await ProjectUpdate.count();
Expand Down
25 changes: 25 additions & 0 deletions src/resolvers/projectResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,11 @@ export class ProjectResolver {
if (!owner)
throw new Error(i18n.__(translationErrorMessagesKeys.USER_NOT_FOUND));

// Check if user email is verified
if (owner && !owner.isEmailVerified) {
throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED));
}

const project = await findProjectById(projectId);

if (!project)
Expand Down Expand Up @@ -1631,6 +1636,16 @@ export class ProjectResolver {
);
}

const owner = await findUserById(user.userId);

if (!owner)
throw new Error(i18n.__(translationErrorMessagesKeys.USER_NOT_FOUND));

// Check if user email is verified
if (owner && !owner.isEmailVerified) {
throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED));
}

const update = await ProjectUpdate.findOne({ where: { id: updateId } });
if (!update)
throw new Error(
Expand Down Expand Up @@ -1663,6 +1678,16 @@ export class ProjectResolver {
i18n.__(translationErrorMessagesKeys.AUTHENTICATION_REQUIRED),
);

const owner = await findUserById(user.userId);

if (!owner)
throw new Error(i18n.__(translationErrorMessagesKeys.USER_NOT_FOUND));

// Check if user email is verified
if (owner && !owner.isEmailVerified) {
throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED));
}

const update = await ProjectUpdate.findOne({ where: { id: updateId } });
if (!update)
throw new Error(
Expand Down
48 changes: 42 additions & 6 deletions src/resolvers/userResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { getOrttoPersonAttributes } from '../adapters/notifications/Notification
import { retrieveActiveQfRoundUserMBDScore } from '../repositories/qfRoundRepository';
import { getLoggedInUser } from '../services/authorizationServices';
import { generateRandomNumericCode } from '../utils/utils';
import { isSolanaAddress } from '../utils/networks';

@ObjectType()
class UserRelatedAddressResponse {
Expand Down Expand Up @@ -173,11 +174,11 @@ export class UserResolver {
if (location !== undefined) {
dbUser.location = location;
}
// Check if user email is verified and it's not the first update
// Check if user email is verified
if (!dbUser.isEmailVerified) {
throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED));
}
// Check if old email is verified and user entered new one and it's not the first update
// Check if old email is verified and user entered new one
if (dbUser.isEmailVerified && email !== dbUser.email) {
throw new Error(i18n.__(translationErrorMessagesKeys.EMAIL_NOT_VERIFIED));
}
Expand Down Expand Up @@ -289,11 +290,46 @@ export class UserResolver {
}

// Check do we have an email already in the database and is it verified
const isEmailAlreadyUsed = await User.findOne({
where: { email: email, isEmailVerified: true },
});
// We need here to check if user wallet solana address or not
// User can have sam email for solana end ethereum wallet
const isSolanaAddressCheck = user?.walletAddress
? isSolanaAddress(user.walletAddress)
: false;
let isEmailAlreadyUsed;
if (isSolanaAddressCheck) {
const rawQuery = `
SELECT *
FROM public."user"
WHERE "email" = $1
AND "isEmailVerified" = true
AND (
"walletAddress" = LEFT("walletAddress", 43) OR
"walletAddress" = LEFT("walletAddress", 44)
)
AND "walletAddress" ~ '^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$'
LIMIT 1
`;

isEmailAlreadyUsed = await User.query(rawQuery, [email]);
} else {
const rawQuery = `
SELECT *
FROM public."user"
WHERE "email" = $1
AND "isEmailVerified" = true
AND "walletAddress" = LEFT("walletAddress", 42)
AND "walletAddress" ~ '^0x[0-9a-fA-F]{40}$'
LIMIT 1
`;

isEmailAlreadyUsed = await User.query(rawQuery, [email]);
}

if (isEmailAlreadyUsed && isEmailAlreadyUsed.id !== user.id) {
if (
isEmailAlreadyUsed &&
isEmailAlreadyUsed.length > 0 &&
isEmailAlreadyUsed.id !== user.id
) {
return 'EMAIL_EXIST';
}

Expand Down

0 comments on commit 4c4ae45

Please sign in to comment.