-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PROTO-1599] Add OTP support (#7193)
- Loading branch information
1 parent
39448b8
commit 8d031a4
Showing
23 changed files
with
711 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
363 changes: 363 additions & 0 deletions
363
packages/identity-service/src/notifications/emails/otp.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { getOtpEmail } = require('../notifications/emails/otp') | ||
|
||
const OTP_CHARS = '0123456789' | ||
const OTP_REDIS_PREFIX = 'otp' | ||
const OTP_EXPIRATION_SECONDS = 600 | ||
|
||
const generateOtp = () => { | ||
let OTP = '' | ||
for (let i = 0; i < 6; i++) { | ||
OTP += OTP_CHARS[Math.floor(Math.random() * OTP_CHARS.length)] | ||
} | ||
return OTP | ||
} | ||
|
||
const getEmail = ({ otp }) => { | ||
const title = 'Your Audius Verification Code is:' | ||
const expire = 'This code will expire in 10 minutes.' | ||
const copyrightYear = new Date().getFullYear().toString() | ||
const formattedOtp = `${otp.substring(0, 3)} ${otp.substring(3, 6)}` | ||
return getOtpEmail({ title, otp: formattedOtp, expire, copyrightYear }) | ||
} | ||
|
||
const validateOtp = async ({ email, otp, redis }) => { | ||
const storedOtp = await redis.get(`${OTP_REDIS_PREFIX}:${email}`) | ||
return otp === storedOtp | ||
} | ||
|
||
const sendOtp = async ({ email, redis, sendgrid }) => { | ||
const otp = generateOtp() | ||
const html = getEmail({ | ||
otp | ||
}) | ||
|
||
const emailParams = { | ||
from: 'The Audius Team <team@audius.co>', | ||
to: email, | ||
subject: 'Your Audius Verification Code', | ||
html, | ||
asm: { | ||
groupId: 26666 // id of unsubscribe group at https://mc.sendgrid.com/unsubscribe-groups | ||
} | ||
} | ||
await redis.set( | ||
`${OTP_REDIS_PREFIX}:${email}`, | ||
otp, | ||
'EX', | ||
OTP_EXPIRATION_SECONDS | ||
) | ||
if (sendgrid) { | ||
await sendgrid.send(emailParams) | ||
} | ||
} | ||
|
||
module.exports = { | ||
validateOtp, | ||
sendOtp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.