Last active
July 12, 2017 22:04
-
-
Save harrylincoln/6dd7273062a4fd6307a441b40888d7f4 to your computer and use it in GitHub Desktop.
Angularfire injectable provider
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 {Injectable} from "@angular/core"; | |
import { Observable } from 'rxjs/Observable'; | |
import { FirebaseObjectFactoryOpts } from "angularfire2/interfaces"; | |
import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database'; | |
import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth'; | |
import * as firebase from 'firebase/app'; | |
import { Headers, Http, RequestOptions } from '@angular/http'; | |
import 'rxjs/add/operator/toPromise'; | |
@Injectable() | |
export class AF { | |
public messages: FirebaseListObservable<any>; | |
public users: FirebaseListObservable<any>; | |
public displayName: string; | |
public email: string; | |
public user: FirebaseListObservable<any>; | |
private beTokenUrl = 'http://localhost:8081/get-token'; | |
private verifyTokenUrl = 'http://localhost:8081/verifyIdToken'; | |
private authApiUrl = 'http://localhost:8081/api/hello'; | |
private createUserUrl = 'http://localhost:8081/create-user'; | |
public clientToken: string; | |
private tokenID: string; | |
constructor(public afAuth: AngularFireAuth, public db: AngularFireDatabase, private http: Http) { | |
this.afAuth.authState.subscribe(res => { | |
if (res && res.uid) { | |
console.log('user is logged in', res); | |
this.user = db.list('users/' + res.uid); | |
console.log('this.user', this.user); | |
} else { | |
console.log('user not logged in'); | |
} | |
}); | |
} | |
createNewUser(email, password) { | |
this.http.post(this.createUserUrl, {emailAddress: email, pass: password}) | |
.toPromise() | |
.then(function (res) { | |
console.log('createNewUser success', res.json()); | |
}) | |
.catch(function(error) { | |
console.log('createNewUser error', error); | |
}); | |
} ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment