Browse Source

Add a Auth quickstart with include tags.

Change-Id: Ia17dd0c5bad3addf0af6eb9fca99388358f00da2
ryanpbrewster-patch-1
Nicolas Garnier 8 years ago
parent
commit
e7ab8e2d57
  1. 18
      README.md
  2. 39
      new-user-email/README.md
  3. 46
      quickstarts/email-users/README.md
  4. 0
      quickstarts/email-users/database.rules.json
  5. 0
      quickstarts/email-users/firebase.json
  6. 45
      quickstarts/email-users/functions/index.js
  7. 0
      quickstarts/email-users/functions/package.json
  8. 0
      quickstarts/email-users/main.css
  9. 0
      quickstarts/email-users/public/firebase-logo.png
  10. 13
      quickstarts/email-users/public/index.html
  11. 0
      quickstarts/email-users/public/main.css
  12. 0
      quickstarts/email-users/public/main.js

18
README.md

@ -14,19 +14,23 @@ This repository contains the following samples:
### [Realtime database trigger quickstart: Uppercaser](/quickstarts/uppercase)
This quickstart sample demonstrates using **Cloud Functions** with a **Firebase Realtime Database** trigger. The function will uppercase messages written to the datastore.
This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Realtime Database events**. The function uppercase messages written to the datastore.
### [HTTPS trigger quickstart: Time Server](/quickstarts/time)
This quickstart sample demonstrates using **Cloud Functions** triggered by **HTTPS requests**. The function will return the current server time and allows for date time formatting.
This quickstart sample demonstrates using **Cloud Functions** triggered by **HTTPS requests**. The function returns the current server time and allows for date time formatting.
### [Cloud Storage trigger quickstart: Thumbnail generator](/quickstarts/thumbnails)
This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Storage events**. The function will generate a thumbnail of uploaded images.
This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Storage events**. The function generates a thumbnail of uploaded images.
### [Auth trigger quickstart: Welcome Email](/quickstarts/email-users)
This quickstart sample demonstrates using **Cloud Functions** triggered by **Firebase Auth events**. The functions sends a Welcome email when user accounts are created (or when users signs-in using an Identity Provider for the first time) and sends a Goodbye email when user accounts ares deleted.
### [PubSub trigger quickstart: Hello World](/quickstarts/thumbnails)
This quickstart sample demonstrates using **Cloud Functions** triggered by **PubSub events**. The function will log the PubSub payload in a Hello world message.
This quickstart sample demonstrates using **Cloud Functions** triggered by **PubSub events**. The functions log the PubSub payload in a Hello world message.
### [Send FCM notifications](fcm-notifications)
@ -129,12 +133,6 @@ Deletes all associated user data that's on the Realtime database when a user del
Uses an Auth trigger.
### [Welcome Email](/new-user-email)
Sends a Welcome email when a user signs-in for the first time.
Uses an Auth trigger.
### [Delete Inactive Users Accounts Cron](/delete-unused-accounts-cron)
Periodically deletes the accounts of users who have not signed in the last month.

39
new-user-email/README.md

@ -1,39 +0,0 @@
# New user email
This sample shows how to send welcome email when a new user signs up and a goodbye email when the user deletes his account.
## Functions Code
See file [functions/index.js](functions/index.js) for the email sending code.
Sending emails is performed using [nodemailer](https://www.npmjs.com/package/bad-words) a node based Email client with comprehensive EMail server setup. In this sample we're showing how to send email through SMTP using a Gmail account. Be aware that Gmail has an [email sending quota](). If you are planning on sending a large number of emails you should use a professional email sending platform such as Sendgrid
The dependencies are listed in [functions/package.json](functions/package.json).
## Trigger rules
The function triggers on account creation and account deletion.
## Setting up the sample
Set the `gmail.email` and `gmail.password` Google Cloud environment variables to match the email and password of the Gmail account used to send emails. For this use:
```bash
firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword"
```
This sample comes with a web-based UI for testing the function. To set it up:
- Create a Firebase Project using the Firebase Developer Console
- Enable Google Provider in the Auth section
- Import and configure Firebase in the `public/index.html` where the `TODO` is located
## Deploy and test
This sample comes with a web-based UI for testing the function. To test it out:
- Deploy your project using `firebase deploy`
- Open the app using `firebase open`, Sign in using Google Sign-In and delete your accounts and you should receive email confirmations.

46
quickstarts/email-users/README.md

@ -0,0 +1,46 @@
# Firebase SDK for Cloud Functions Quickstart - Auth triggers
This quickstart demonstrates how to setup an Auth triggered Cloud Function using the **Firebase SDK for Cloud Functions**.
## Introduction
We'll deploy Auth triggered Functions that send a welcome email when a new user signs up and a goodbye email when user accounts are deleted.
Further reading: [Firebase SDK for Cloud Functions](https://firebase.google.com/docs/functions/)
## Functions Code
See file [functions/index.js](functions/index.js) for the Functions trigger and the email sending code.
Sending emails is performed using [nodemailer](https://www.npmjs.com/package/nodemailer) a node based Email client with comprehensive EMail server setup. For simplicity, in this sample we're showing how to send email through SMTP using a Gmail account. Be aware that Gmail has an [email sending quota](https://support.google.com/mail/answer/22839). If you are planning on sending a large number of emails you should use a professional email sending platform such as [Sendgrid](https://console.cloud.google.com/launcher/details/sendgrid-app/sendgrid-email), [Mailjet](https://www.mailjet.com/google) or [Mailgun](http://www.mailgun.com/google).
The dependencies are listed in [functions/package.json](functions/package.json).
This sample comes with a simple web-based UI which code is in [public](public) directory that lets you easily sign-in Firebase and delete your account for purposes of testing the Functions.
## Setting up the sample
1. Create a Firebase Project using the [Firebase Console](https://console.firebase.google.com).
1. Enable the **Google** Provider in the **Auth** section.
1. Clone or download this repo and open the `quickstarts/pubsub-helloworld` directory.
1. Paste the Web initialization snippet from: **Firebase Console > Overview > Add Firebase to your web app** in the `public/index.html` where the `TODO` is located.
1. You must have the Firebase CLI installed. If you don't have it install it with `npm install -g firebase-tools` and then configure it with `firebase login`.
1. Configure the CLI locally by using `firebase use --add` and select your project in the list.
1. Install dependencies locally by running: `cd functions; npm install; cd -`
1. Set the `gmail.email` and `gmail.password` Google Cloud environment variables to match the email and password of the Gmail account used to send emails. For this use:
```bash
firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword"
```
## Deploy and test
This sample comes with a web-based UI for testing the function. To test it out:
1. Deploy your project using `firebase deploy`
1. Open the app using `firebase open hosting:site`, this will open a browser.
1. Sign in the web app in the browser using Google Sign-In and delete your account using the button on the web app. You should receive email confirmations for each actions.

0
new-user-email/database.rules.json → quickstarts/email-users/database.rules.json

0
new-user-email/firebase.json → quickstarts/email-users/firebase.json

45
new-user-email/functions/index.js → quickstarts/email-users/functions/index.js

@ -18,33 +18,50 @@
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// See: https://nodemailer.com/
// For other types of transports (Amazon SES, Sendgrid...) see https://nodemailer.com/2-0-0-beta/setup-transporter/
// TODO: Make sure you configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = encodeURIComponent(functions.config().gmail.email);
const gmailPassword = encodeURIComponent(functions.config().gmail.password);
const mailTransport = nodemailer.createTransport(
`smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`);
// Your company name to include in the emails
// TODO: Change this to your company name and also customize the email sent.
const COMPANY_NAME = 'MyCompany';
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = 'Cloud Storage for Firebase quickstart';
// Sends a welcome email to new user.
// [START sendWelcomeEmail]
/**
* Sends a welcome email to new user.
*/
// [START onCreateTrigger]
exports.sendWelcomeEmail = functions.auth.user().onCreate(event => {
const email = event.data.email;
const displayName = event.data.displayName;
// [END onCreateTrigger]
// [START eventAttributes]
const user = event.data; // The Firebase user.
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
// [END eventAttributes]
return sendWelcomeEmail(email, displayName);
});
// [END sendWelcomeEmail]
// Send an account deleted email confirmation to users who delete their accounts.
// [START sendByeEmail]
/**
* Send an account deleted email confirmation to users who delete their accounts.
*/
// [START onDeleteTrigger]
exports.sendByeEmail = functions.auth.user().onDelete(event => {
const email = event.data.email;
const displayName = event.data.displayName;
// [END onDeleteTrigger]
const user = event.data;
const email = user.email;
const displayName = user.displayName;
return sendGoodbyEmail(email, displayName);
});
// [END sendByeEmail]
// Sends a welcome email to the given user.
function sendWelcomeEmail(email, displayName) {
@ -54,8 +71,8 @@ function sendWelcomeEmail(email, displayName) {
};
// The user unsubscribed to the newsletter.
mailOptions.subject = `Welcome to ${COMPANY_NAME}!`;
mailOptions.text = `Hey ${displayName}!, Welcome to ${COMPANY_NAME}. I hope you will enjoy our service.`;
mailOptions.subject = `Welcome to ${APP_NAME}!`;
mailOptions.text = `Hey ${displayName}!, Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
return mailTransport.sendMail(mailOptions).then(() => {
console.log('New welcome email sent to:', email);
});
@ -70,7 +87,7 @@ function sendGoodbyEmail(email, displayName) {
// The user unsubscribed to the newsletter.
mailOptions.subject = `Bye!`;
mailOptions.text = `Hey ${displayName}!, We confirm that we have deleted your ${COMPANY_NAME} account.`;
mailOptions.text = `Hey ${displayName}!, We confirm that we have deleted your ${APP_NAME} account.`;
return mailTransport.sendMail(mailOptions).then(() => {
console.log('Account deletion confirmation email sent to:', email);
});

0
new-user-email/functions/package.json → quickstarts/email-users/functions/package.json

0
new-user-email/main.css → quickstarts/email-users/main.css

0
new-user-email/public/firebase-logo.png → quickstarts/email-users/public/firebase-logo.png

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

13
new-user-email/public/index.html → quickstarts/email-users/public/index.html

@ -72,6 +72,19 @@
* TODO(DEVELOPER): Paste the initialization snippet from: Firebase Console > Overview > Add Firebase to your web app. *
*********************************************************************************************************************** -->
<script src="https://www.gstatic.com/firebasejs/3.6.4/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyAu14Qhq_t4GbyZlRwVlFVLellEo4Gjmzs",
authDomain: "functions-email-confirmation.firebaseapp.com",
databaseURL: "https://functions-email-confirmation.firebaseio.com",
storageBucket: "functions-email-confirmation.appspot.com",
messagingSenderId: "207174568617"
};
firebase.initializeApp(config);
</script>
<script src="main.js"></script>
</body>
</html>

0
new-user-email/public/main.css → quickstarts/email-users/public/main.css

0
new-user-email/public/main.js → quickstarts/email-users/public/main.js

Loading…
Cancel
Save