Skip to content

Instantly share code, notes, and snippets.

@drbh
Created December 12, 2023 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbh/fb05074aa507839f3c099eabc6dd8038 to your computer and use it in GitHub Desktop.
Save drbh/fb05074aa507839f3c099eabc6dd8038 to your computer and use it in GitHub Desktop.
simple OTP email login
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: 'me@drbh.xyz'
},
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