Created
December 12, 2023 23:50
-
-
Save drbh/fb05074aa507839f3c099eabc6dd8038 to your computer and use it in GitHub Desktop.
simple OTP email login
This file contains hidden or 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
const SEND_IN_BLUE_KEY = "..." | |
const randomString = (length) => { | |
const bytes = new Uint8Array(length); | |
window.crypto.getRandomValues(bytes); | |
let result = ''; | |
bytes.forEach((b) => { | |
// Convert each byte to a digit (0-9) | |
result += (b % 10).toString(); | |
}); | |
return result; | |
}; | |
export const sendEmailOTP = async ({ email, receiver }: any) => { | |
const title = 'Login fom Acme'; | |
const code = randomString(6) | |
const message = `Your code is: ${code}`; | |
if (!email) { | |
return new Response('No email provided', { status: 400 }); | |
} | |
const response = await fetch('https://api.sendinblue.com/v3/smtp/email', { | |
method: 'POST', | |
headers: { | |
accept: 'application/json', | |
'api-key': SEND_IN_BLUE_KEY, | |
'content-type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
sender: { | |
name: 'Acme', | |
email: '[email protected]' | |
}, | |
to: [{ email: email, name: receiver }], | |
subject: title, | |
htmlContent: `<html><head></head><body><p>Hello,</p>${message}</p></body></html>` | |
}) | |
}); | |
const body = await response.json(); | |
return body; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment