Last active
January 30, 2019 11:53
-
-
Save PsyGik/4e02569d5af259e2b1cbd6fd9389ad10 to your computer and use it in GitHub Desktop.
The Build Series: E1 - Contact Form powered by Firebase Functions
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
| import * as functions from 'firebase-functions'; | |
| const { google } = require('googleapis'); | |
| const express = require('express'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| // Automatically allow cross-origin requests | |
| app.use(cors({ origin: true })); | |
| const privatekey = require('./private-key.json'); | |
| // configure a JWT auth client | |
| const jwtClient = new google.auth.JWT(privatekey.client_email, null, privatekey.private_key, ['https://www.googleapis.com/auth/spreadsheets']); | |
| const spreadsheetId = 'SHEET_ID'; | |
| const sheetName = 'Sheet1' | |
| const sheets = google.sheets('v4'); | |
| const authorizeAndDoNext = async () => { | |
| await jwtClient.authorize(); | |
| console.log('Successfully Conected'); | |
| } | |
| export const ping = (request, response) => { | |
| response.send('Hello from the other side'); | |
| } | |
| /** | |
| * Write data to sheet | |
| * @param data {name,email,message} | |
| */ | |
| export const writeToSheet = async (data) => { | |
| return await sheets.spreadsheets.values.append({ | |
| auth: jwtClient, | |
| spreadsheetId: spreadsheetId, | |
| range: sheetName, | |
| valueInputOption: 'RAW', | |
| insertDataOption: 'INSERT_ROWS', | |
| resource: { | |
| values: [ | |
| [data.name, data.email, data.message] | |
| ], | |
| }, | |
| }); | |
| } | |
| /** | |
| * The secret sauce. | |
| */ | |
| export const contact = async (request, response) => { | |
| try { | |
| await authorizeAndDoNext(); | |
| const result = await writeToSheet(request.body); | |
| response.status(200).send(result.data); | |
| } catch (error) { | |
| console.log('Error', error); | |
| response.status(400).send(error); | |
| } | |
| } | |
| app.get('/ping', (req, res) => res.send('Hello from the other side')); | |
| app.post('/contact', contact); | |
| export const api = functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment