add webauthn routes
This commit is contained in:
@@ -17,3 +17,13 @@ export async function generateToken(): Promise<string> {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function generateOTP() {
|
||||
let otp = "";
|
||||
|
||||
for (let i = 0; i < 6; i++) {
|
||||
otp += crypto.randomInt(10);
|
||||
}
|
||||
|
||||
return parseInt(otp);
|
||||
}
|
||||
|
||||
59
src/utils/mail.ts
Normal file
59
src/utils/mail.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import axios from "axios";
|
||||
|
||||
let token = {
|
||||
value: "",
|
||||
expiry: new Date(),
|
||||
};
|
||||
|
||||
export async function getToken() {
|
||||
if (token.value != "" && new Date() < token.expiry) {
|
||||
return token.value;
|
||||
}
|
||||
|
||||
const res = await axios({
|
||||
url: `https://accounts.zoho.com/oauth/v2/token?refresh_token=${process.env.ZOHO_REFRESH_TOKEN}&grant_type=refresh_token&client_id=${process.env.ZOHO_CLIENT_ID}&client_secret=${process.env.ZOHO_CLIENT_SECRET}`,
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
if (res.data.status && res.data.status.code != 200) {
|
||||
console.dir(res.data, { depth: null });
|
||||
throw "error fetching token";
|
||||
}
|
||||
|
||||
token.value = res.data.access_token;
|
||||
token.expiry = new Date(Date.now() + res.data.expires_in * 1000);
|
||||
|
||||
return token.value;
|
||||
}
|
||||
|
||||
export async function sendMail(
|
||||
to: string,
|
||||
subject: string,
|
||||
body: string
|
||||
): Promise<Boolean> {
|
||||
try {
|
||||
const token = await getToken();
|
||||
|
||||
const res = await axios({
|
||||
url: `https://mail.zoho.com/api/accounts/${process.env.ZOHO_ACCOUNT_ID}/messages`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Zoho-oauthtoken ${token}`,
|
||||
},
|
||||
data: {
|
||||
fromAddress: "akhil.reddy@qualyval.com",
|
||||
toAddress: to,
|
||||
subject,
|
||||
content: body,
|
||||
mailFormat: "plaintext",
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
if (err.response) console.log(err.response.data);
|
||||
else console.log(err);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user