Skip to content

Instantly share code, notes, and snippets.

View javebratt's full-sized avatar
🏠
Working from home

Jorge Vergara javebratt

🏠
Working from home
View GitHub Profile
@javebratt
javebratt / email-validator.ts
Last active September 1, 2017 11:24
Email Validator what works for NG2 and Ionic 2 apps.
import {FormControl} from '@angular/forms';
export class EmailValidator {
static isValid(control: FormControl){
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(control.value);
if (re){
return null;
// Is there any performance difference on this 2 approaches:
// Injecting on the Page itself:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
@Component({
templateUrl: 'build/pages/home/home.html',
})
// Before: When the alert gets called it interrupts the loading component, so it causes a navigation issue:
export class LoginPage {
loginUser(){
this.submitAttempt = true;
if (!this.loginForm.valid){
console.log(this.loginForm.value);
} else {
var ngTemplate = require('../dist/plugins/ng-template').ngTemplate;
var nodeResolve = require('rollup-plugin-node-resolve');
var commonjs = require('rollup-plugin-commonjs');
// https://github.com/rollup/rollup/wiki/JavaScript-API
module.exports = {
/**
* entry: The bundle's starting point. This file will
* be included, along with the minimum necessary code
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import firebase from 'firebase';
@javebratt
javebratt / firebase-storage.ts
Created December 26, 2016 15:20
Quick example of uploading and retrieving images from Firebase storage.
// You create a reference for the picture:
this.profilePictureRef.child(newGuest.key).child('profilePicture.png')
// Then you upload it:
.putString(guestPicture, 'base64', {contentType: 'image/png'})
// Since it returns a promise, you save the downloadURL somewhere to use later:
.then((savedPicture) => {
this.eventList.child(eventId).child('guestList').child(newGuest.key).child('profilePicture')
.set(savedPicture.downloadURL);
@javebratt
javebratt / firebase-storage.ts
Last active September 13, 2017 19:12
Firebase Storage example
// Create a reference to Firebase Storage
this.driverProfilePictureRef = firebase.storage().ref('/driverProfilePicture/');
// Upload the picture to Firebase Storage
const uploadPicture = this.driverProfilePictureRef.child("driver's id")
.child('profilePicture.png').putString(pictureInBase64StringHere, 'base64', {contentType: 'image/png'});
// After the picture successfully uploads, you'll want to add that picture's url to the drivers node
uploadPicture.then( profilePicture => {
// You can create a function in your authentication provider that resolves the value of admin:
isAsdmin(): Promise<any> {
return new Promise( (resolve, reject ) => {
firebase.database().ref(`userProfile/${userId}/admin`).once('value', adminSnapshot => {
resolve(adminSnapshot.val());
});
});
}
// FUNCTION 1
createEvent(eventName: string, other_params, eventMainPicture = null): any {
return this.eventList.push({
name: eventName,
... // All the other information you're adding.
...
...
}).then( newEvent => {
// When the event is created we pass in the base64 string that represents the picture to the
// The pictures I'm guessing are in Firebase Storage, but they need to be linked (through the download URL) to the database,
// so something like:
userProfile: {
user1: {
name: "Jorge",
profilePicture: "https://link_to_picture"
}
}