浏览代码

20-255 Adding basic auth

tags/before_ngrx
Nils Dittberner 8 年前
父节点
当前提交
7e31ce83b7
共有 2 个文件被更改,包括 24 次插入4 次删除
  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 查看文件

@@ -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;
}
}

+ 8
- 3
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<Recipe[]>(this.baseUrl + 'recipes.json').subscribe(
const token = this.authService.getToken();
this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json?auth=' + token).subscribe(
recipes => {
this.recipeService.replaceRecipes(recipes);
}


正在加载...
取消
保存