Skip to content

Commit

Permalink
wip contact form-recaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcatbuzz committed May 16, 2024
1 parent ad2674a commit ae666c8
Show file tree
Hide file tree
Showing 13 changed files with 415 additions and 30 deletions.
2 changes: 1 addition & 1 deletion functions/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
"import",
],
rules: {
"quotes": ["error", "double"],
"quotes": ["error", "single"],
"import/no-unresolved": 0,
"indent": ["error", 2],
},
Expand Down
2 changes: 2 additions & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ typings/

# Node.js dependency directory
node_modules/

.env
121 changes: 119 additions & 2 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
},
"main": "lib/index.js",
"dependencies": {
"@sendgrid/mail": "^8.1.3",
"dotenv": "^16.4.5",
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1"
},
Expand Down
24 changes: 6 additions & 18 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
/**
* Import function triggers from their respective submodules:
*
* import {onCall} from "firebase-functions/v2/https";
* import {onDocumentWritten} from "firebase-functions/v2/firestore";
*
* See a full list of supported triggers at https://firebase.google.com/docs/functions
*/
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('dotenv').config();
import {initializeApp} from 'firebase-admin/app';
initializeApp();

import {onRequest} from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";

// Start writing functions
// https://firebase.google.com/docs/functions/typescript

// export const helloWorld = onRequest((request, response) => {
// logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
export {sendContactMessage} from './sendgrid';
export {checkRecaptcha} from './recaptcha';
34 changes: 34 additions & 0 deletions functions/src/recaptcha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {onRequest} from 'firebase-functions/v2/https';
import axios from 'axios';
import * as cors from 'cors';
const corsHandler = cors({origin: true});
const USER_ERROR_CODES = ['missing-input-response', 'invalid-input-response'];
const SECRET_KEY = process.env.SECRET_KEY;

export const checkRecaptcha = onRequest((req, res) => {
corsHandler(req, res, async () => {
// 'http://localhost:8080'
res.set('Access-Control-Allow-Origin', 'https://anthonybuzzelli.dev');
res.setHeader('Content-Type', 'application/json');
const token = req.query.token;
console.log(token, 'what is here');
try {
const response = await axios.get(`https://recaptcha.google.com/recaptcha/api/siteverify?secret=${SECRET_KEY}&response=${token}`);

const data = response.data;
console.log('response data: ', data);
if (data.success) {
return res.status(200).send({score: data.score});
}

const errorCodes = data['error-codes'];
if (errorCodes.length == 1 && USER_ERROR_CODES.includes(errorCodes[0])) {
return res.status(400).send('Invalid Input');
}
return res.status(500).send('Internal Error');
} catch (error) {
console.log('error: ', error);
return res.status(500).send('Internal Error');
}
});
});
34 changes: 34 additions & 0 deletions functions/src/sendgrid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {onValueWritten} from 'firebase-functions/v2/database';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const sgMail = require('@sendgrid/mail');
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
sgMail.setApiKey(SENDGRID_API_KEY);

export const sendContactMessage = onValueWritten(
'messages/{pushkey}', async (change) => {
const dataAfterChange = change.data.after.val();
if (change.data.before.val() || !dataAfterChange.subject) {
return;
}

const val = dataAfterChange;

const msg = {
to: 'tomcatbuzz@yahoo.com',
from: 'firebase@anthonybuzzelli.dev',
subject: 'You have a new contact request',
text: `Your message content. \n
Sender's Name: ${val.name} \n
Sender's Email: ${val.email} \n
Subject: ${val.subject} \n
Content: ${val.message}`,
};
await sgMail.send(msg)
.then(() => {
console.log('Email sent');
})
.catch((error: Error) => {
console.error('Error Sending email', error);
});
});
Loading

0 comments on commit ae666c8

Please sign in to comment.