Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add is verified #1118

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/api/src/controllers/v2/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from 'express';
import { getAddress } from '@ethersproject/address';

import { AttestationType, send, verify } from '~services/attestation';
import { ListUserNotificationsRequestSchema } from '~validators/user';
import { RequestWithUser } from '~middlewares/core';
import { ValidatedRequest } from '~utils/queryValidator';
Expand Down Expand Up @@ -336,6 +337,38 @@ class UserController {
.then(r => standardResponse(res, 200, true, r))
.catch(e => standardResponse(res, 400, false, '', { error: e.message }));
};
public requestVerify = (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 401, false, '', {
error: {
name: 'USER_NOT_FOUND',
message: 'User not identified!'
}
});
return;
}
const { plainTextIdentifier, type } = req.body;

send(plainTextIdentifier, type, req.user.userId)
.then(r => standardResponse(res, 200, true, r, {}))
.catch(e => standardResponse(res, 400, false, '', { error: e.message }));
};
public verify = (req: RequestWithUser, res: Response) => {
if (req.user === undefined) {
standardResponse(res, 401, false, '', {
error: {
name: 'USER_NOT_FOUND',
message: 'User not identified!'
}
});
return;
}
const { plainTextIdentifier, code } = req.body;

verify(plainTextIdentifier, AttestationType.EMAIL_LINK, code!, req.user.userId)
.then(r => standardResponse(res, 200, true, r, {}))
.catch(e => standardResponse(res, 400, false, '', { error: e.message }));
};
}

export default UserController;
3 changes: 3 additions & 0 deletions packages/api/src/routes/v2/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,7 @@ export default (app: Router): void => {
userSendPushNotifications,
userController.sendPushNotifications
);

route.post('/request-verify', authenticateToken, userController.requestVerify);
route.post('/verify', authenticateToken, userController.verify);
};
61 changes: 59 additions & 2 deletions packages/api/src/services/attestation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ interface IERC20Contract extends Contract {
increaseAllowance(spender: string, value: BigNumber): Promise<TransactionResponse>;
}

enum AttestationType {
export enum AttestationType {
PHONE_NUMBER = 0,
EMAIL = 1
EMAIL = 1,
EMAIL_LINK = 2
}

/**
Expand Down Expand Up @@ -145,6 +146,13 @@ export const verify = async (plainTextIdentifier: string, type: AttestationType,
},
{ where: { id: userId } }
);
} else if (type === AttestationType.EMAIL_LINK) {
await database.models.appUser.update(
{
emailValidated: true
},
{ where: { id: userId } }
);
}

return obfuscatedIdentifier;
Expand Down Expand Up @@ -224,6 +232,55 @@ export const send = async (plainTextIdentifier: string, type: AttestationType, u
text: body
});

await database.models.appUser.update(
{
email: plainTextIdentifier
},
{ where: { id: userId } }
);
} else if (type === AttestationType.EMAIL_LINK) {
code = randomBytes(4).toString('hex');

const response = await prismic.getAllByType('push_notifications_data', {
lang: locale || 'en-US'
});
let emailValidationSubject: string | undefined;
let emailValidationBody: string | undefined;

if (response.length > 0) {
const data = response[0].data;
emailValidationSubject = data['email-link-validation-message-subject'];
emailValidationBody = data['email-link-validation-message-body'];
}
if (!emailValidationSubject || !emailValidationBody) {
const response = await prismic.getAllByType('push_notifications_data', {
lang: 'en-US'
});
const data = response[0].data;
emailValidationSubject = data['email-link-validation-message-subject'];
emailValidationBody = data['email-link-validation-message-body'];
}

const user = await database.models.appUser.findOne({
attributes: ['address'],
where: { id: userId }
});
const uri = `https://app.impactmarket.com/user/verify?code=${code}&address=${user?.address}&email=${plainTextIdentifier}`;

const body = emailValidationBody!.replace('{{link}}', `<a href=${uri}>Click Me!<a>`);
sendEmail({
to: plainTextIdentifier,
// TODO: move to env
from: 'no-reply@impactmarket.com',
subject: emailValidationSubject,
content: [
{
type: 'text/html',
value: body
}
]
});

await database.models.appUser.update(
{
email: plainTextIdentifier
Expand Down
Loading