Skip to content

Instantly share code, notes, and snippets.

View himanshusaraswat's full-sized avatar

Himanshu Saraswat himanshusaraswat

View GitHub Profile
@himanshusaraswat
himanshusaraswat / UseFunctionOnLocal.ts
Created April 13, 2020 14:55
A simple trick to be sure you always target the cloud function on your local environment while being in development mode.
constructor(private angularFireFunctions: AngularFireFunctions) {
if (!environment.production) {
this.angularFireFunctions.functions.useFunctionsEmulator('http://localhost:5000');// Your emulator or serve URL
}
}
@himanshusaraswat
himanshusaraswat / app-routing-firebase.ts
Created April 9, 2020 17:25
@angular/fire comes in with two handy methods which helps us implement auth-guard in angular quickly.
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PasswordLessLoginComponent } from './login/password-less-login/password-less-login.component';
import { SendEmailComponent } from './email/send-email/send-email.component';
import { AngularFireAuthGuard, redirectLoggedInTo, redirectUnauthorizedTo } from '@angular/fire/auth-guard';
const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['login']);
const redirectLoggedInToSendEmail = () => redirectLoggedInTo(['send-email']);
const routes: Routes = [{
@himanshusaraswat
himanshusaraswat / signInWithEmailLink.ts
Created April 9, 2020 15:10
Lays down the steps to send an email link for sign-in (passwordless sign-in). Removes the hassle of remembering passwords for your end-users & for you as developers to implement "forgot password" or "verify-email" workflows.
@himanshusaraswat
himanshusaraswat / sendGridCloudFunction.ts
Last active April 12, 2020 23:41
Cloud function for Firebase to send emails using SendGrid's dynamic template. Being a HTTP callable function we mitigate the hassle of verifying authenticated users, thereby writing more secure code.
// Firebase Config
import * as functions from 'firebase-functions';
// Sendgrid Config
import * as sgMail from '@sendgrid/mail';
import { onlyAlpha, emailPattern } from './constants/constants';
const API_KEY = functions.config().sendgrid.key;
const templateId = functions.config().sendgrid.template;
const from = functions.config().sendgrid.from;