2017-02-25 61 views
0

我正在嘗試使用angularfire 2創建應用程序。無法找到限制成員區域的完美方式,這意味着只有經過身份驗證的成員才能訪問該區域。這是我的「login.component.ts」文件在Angular Fire中創建受限制的成員區域2

import { Component, OnInit, HostBinding } from '@angular/core'; 
import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2'; 
import { FormControl, FormGroup, Validators } from '@angular/forms'; 
import { Router } from '@angular/router'; 

@Component({ 
    selector: 'app-login', 
    templateUrl: './login.component.html', 
    styleUrls: ['./login.component.css'], 
}) 

export class LoginComponent { 

    state: string = ''; 
    error: any; 
    login: FormGroup; 

    constructor(
     public af: AngularFire, 
     private router: Router 
     ) { 
     //official angfire 2 app example 
     //this.af.auth.subscribe(auth => console.log(auth)); 
     } 

ngOnInit() { 
this.login = new FormGroup({ 
    username: new FormControl('', Validators.required), 
    password: new FormControl('', Validators.required) 
}); 

} 

onSubmit() { 
    //console.log(this.login.value, this.login.valid); 
    var value = this.login.value; 
    //console.log(value.username); 
    //console.log(value.password); 
     this.af.auth.login({ 
     email: value.username, 
     password: value.password, 
    }, 
    { 
     provider: AuthProviders.Password, 
     method: AuthMethods.Password, 
    }).then(
     (success) => { 
     console.log(success); 
     this.router.navigate(['/members']); 
    }).catch(
     (err) => { 
     console.log(err); 
     this.error = err; 
    }) 
    } 

} 

和members.component.ts文件

import { Component, OnInit } from '@angular/core'; 
import { AngularFire, AuthProviders, AuthMethods } from 'angularfire2'; 
import { Router } from '@angular/router'; 

@Component({ 
selector: 'app-other', 
templateUrl: './members.component.html', 
styleUrls: ['./members.component.css'] 
}) 

export class MembersComponent implements OnInit { 
//name: ""; 
//state: string = ''; 

constructor(
    public af: AngularFire, 
    private router: Router 
) { 

    this.af.auth.subscribe(auth => { 
    if(auth) { 
     console.log(auth); 
    } 
    }); 

} 

logout() { 
    this.af.auth.logout(); 
    console.log('logged out'); 
    this.router.navigate(['/login']); 
} 


ngOnInit() { 
} 

} 

我知道我的代碼可以看起來像一個愚蠢的一個,但實際上我想研究這個。但是我沒有足夠的文檔來解決我的問題。提前致謝。

回答