diff --git a/src/app/auth/auth.service.ts b/src/app/auth/auth.service.ts index 693db07..bfea68f 100644 --- a/src/app/auth/auth.service.ts +++ b/src/app/auth/auth.service.ts @@ -1,6 +1,8 @@ import * as firebase from 'firebase'; export class AuthService { + token: string; + signupUser(email: string, password: string) { firebase.auth().createUserWithEmailAndPassword(email, password).catch( error => console.log(error) @@ -10,10 +12,23 @@ export class AuthService { signinUser(email: string, password: string) { firebase.auth().signInWithEmailAndPassword(email, password) .then( - response => console.log(response) + response => { + firebase.auth().currentUser.getIdToken() + .then( + (token: string) => this.token = token + ); + } ) .catch( error => console.log(error) ); } + + getToken() { + firebase.auth().currentUser.getIdToken() + .then( + (token: string) => this.token = token + ); + return this.token; + } } \ No newline at end of file diff --git a/src/app/shared/data-storage.service.ts b/src/app/shared/data-storage.service.ts index 37760de..09c7280 100644 --- a/src/app/shared/data-storage.service.ts +++ b/src/app/shared/data-storage.service.ts @@ -3,19 +3,24 @@ import { HttpClient } from '@angular/common/http' import { RecipeService } from '../recipes/recipe.service'; import { Recipe } from '../recipes/recipe.model'; +import { AuthService } from '../auth/auth.service'; @Injectable() export class DataStorageService { readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; - constructor(private httpClient: HttpClient, private recipeService: RecipeService) {} + constructor(private httpClient: HttpClient, + private recipeService: RecipeService, + private authService: AuthService) {} storeRecipes() { - return this.httpClient.put(this.baseUrl + 'recipes.json', this.recipeService.getRecipes()); + const token = this.authService.getToken(); + return this.httpClient.put(this.baseUrl + 'recipes.json?auth=' + token, this.recipeService.getRecipes()); } fetchRecipes() { - this.httpClient.get(this.baseUrl + 'recipes.json').subscribe( + const token = this.authService.getToken(); + this.httpClient.get(this.baseUrl + 'recipes.json?auth=' + token).subscribe( recipes => { this.recipeService.replaceRecipes(recipes); }