Browse Source

20-255 Adding basic auth

tags/before_ngrx
Nils Dittberner 8 years ago
parent
commit
7e31ce83b7
2 changed files with 24 additions and 4 deletions
  1. +16
    -1
      src/app/auth/auth.service.ts
  2. +8
    -3
      src/app/shared/data-storage.service.ts

+ 16
- 1
src/app/auth/auth.service.ts View File

@@ -1,6 +1,8 @@
import * as firebase from 'firebase'; import * as firebase from 'firebase';


export class AuthService { export class AuthService {
token: string;

signupUser(email: string, password: string) { signupUser(email: string, password: string) {
firebase.auth().createUserWithEmailAndPassword(email, password).catch( firebase.auth().createUserWithEmailAndPassword(email, password).catch(
error => console.log(error) error => console.log(error)
@@ -10,10 +12,23 @@ export class AuthService {
signinUser(email: string, password: string) { signinUser(email: string, password: string) {
firebase.auth().signInWithEmailAndPassword(email, password) firebase.auth().signInWithEmailAndPassword(email, password)
.then( .then(
response => console.log(response)
response => {
firebase.auth().currentUser.getIdToken()
.then(
(token: string) => this.token = token
);
}
) )
.catch( .catch(
error => console.log(error) error => console.log(error)
); );
} }

getToken() {
firebase.auth().currentUser.getIdToken()
.then(
(token: string) => this.token = token
);
return this.token;
}
} }

+ 8
- 3
src/app/shared/data-storage.service.ts View File

@@ -3,19 +3,24 @@ import { HttpClient } from '@angular/common/http'


import { RecipeService } from '../recipes/recipe.service'; import { RecipeService } from '../recipes/recipe.service';
import { Recipe } from '../recipes/recipe.model'; import { Recipe } from '../recipes/recipe.model';
import { AuthService } from '../auth/auth.service';


@Injectable() @Injectable()
export class DataStorageService { export class DataStorageService {
readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; 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() { 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() { fetchRecipes() {
this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json').subscribe(
const token = this.authService.getToken();
this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json?auth=' + token).subscribe(
recipes => { recipes => {
this.recipeService.replaceRecipes(recipes); this.recipeService.replaceRecipes(recipes);
} }


Loading…
Cancel
Save