-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad2674a
commit ae666c8
Showing
13 changed files
with
415 additions
and
30 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
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ typings/ | |
|
||
# Node.js dependency directory | ||
node_modules/ | ||
|
||
.env |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -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'; |
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,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'); | ||
} | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); |
Oops, something went wrong.