Config AngularFire, we need database and auth module from firebase.

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule, Routes} from '@angular/router';
import {AngularFireModule, FirebaseAppConfig} from 'angularfire2';
import {AngularFireAuthModule} from 'angularfire2/auth';
import {AngularFireDatabaseModule} from 'angularfire2/database';
import {SharedModule} from './shared/shared.module'; export const ROUTES: Routes = [
{
path: 'auth',
children: [
{path: '', pathMatch: 'full', redirectTo: 'login'},
{path: 'login', loadChildren: './login/login.module#LoginModule'},
{path: 'register', loadChildren: './register/register.module#RegisterModule'}
]
}
]; export const forebaseConfig: FirebaseAppConfig = {
apiKey: "xxxxxxxx",
authDomain: "fitness-app-a26ed.firebaseapp.com",
databaseURL: "https://fitness-app-a26ed.firebaseio.com",
projectId: "fitness-app-a26ed",
storageBucket: "fitness-app-a26ed.appspot.com",
messagingSenderId: "781493219422"
}; @NgModule({
imports: [
CommonModule,
AngularFireModule.initializeApp(forebaseConfig),
AngularFireAuthModule,
AngularFireDatabaseModule,
SharedModule,
RouterModule.forChild(ROUTES)
]
})
export class AuthModule {}

For the SharedModule:

import {ModuleWithProviders, NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ReactiveFormsModule} from '@angular/forms'; import { AuthFormComponent } from './components/auth-form/auth-form.component';
import {AuthService} from './services/auth/auth.service'; @NgModule({
imports: [
CommonModule,
ReactiveFormsModule
],
declarations: [
AuthFormComponent
],
exports: [
AuthFormComponent
]
})
export class SharedModule { // We don't want multi instance for AuthService, so we need forRoot method
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [
AuthService
]
}
}
}

We use forRoot method to register our AuthSerivce, so there won't be multi instances for it.

But in the AuthModule, we need to change a little bit:

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule, Routes} from '@angular/router';
import {AngularFireModule, FirebaseAppConfig} from 'angularfire2';
import {AngularFireAuthModule} from 'angularfire2/auth';
import {AngularFireDatabaseModule} from 'angularfire2/database';
import {SharedModule} from './shared/shared.module'; export const ROUTES: Routes = [
{
path: 'auth',
children: [
{path: '', pathMatch: 'full', redirectTo: 'login'},
{path: 'login', loadChildren: './login/login.module#LoginModule'},
{path: 'register', loadChildren: './register/register.module#RegisterModule'}
]
}
]; export const forebaseConfig: FirebaseAppConfig = {
apiKey: "xxxxxxxx",
authDomain: "fitness-app-a26ed.firebaseapp.com",
databaseURL: "https://fitness-app-a26ed.firebaseio.com",
projectId: "fitness-app-a26ed",
storageBucket: "fitness-app-a26ed.appspot.com",
messagingSenderId: "781493219422"
}; @NgModule({
imports: [
CommonModule,
AngularFireModule.initializeApp(forebaseConfig),
AngularFireAuthModule,
AngularFireDatabaseModule,
SharedModule.forRoot(),
RouterModule.forChild(ROUTES)
]
})
export class AuthModule {}

AuthService is the serivce which talk to Firebase Auth Module:

import {Injectable} from '@angular/core';
import {AngularFireAuth} from 'angularfire2/auth'; @Injectable()
export class AuthService {
constructor(
private af: AngularFireAuth
) { } createUser(email: string, password: string) {
return this.af.auth.createUserWithEmailAndPassword(email, password);
} loginUser(email: string, password: string) {
return this.af.auth.signInWithEmailAndPassword(email, password)
}
}

Register user:

import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {AuthService} from '../../../shared/services/auth/auth.service';
@Component({
selector: 'register',
template: `
<div>
<auth-form (submitted)="registerUser($event)">
<h1>Register</h1>
<a routerLink="/auth/login">Already have an account?</a>
<button type="submit">Create account</button>
<div class="error" *ngIf="error">
{{error}}
</div>
</auth-form>
</div>
`
})
export class RegisterComponent { error: string; constructor(
private authService: AuthService
) { } async registerUser(event: FormGroup) {
const {email, password} = event.value;
try {
await this.authService.createUser(email, password);
} catch(err) {
this.error = err.message;
}
}
}

login user:

import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {AuthService} from '../../../shared/services/auth/auth.service'; @Component({
selector: 'login',
template: `
<div>
<auth-form (submitted)="loginUser($event)">
<h1>Login</h1>
<a routerLink="/auth/register">Not registered?</a>
<button type="submit">Login</button>
<div class="error" *ngIf="error">
{{error}}
</div>
</auth-form>
</div>
`
})
export class LoginComponent { error: string; constructor(
private authService: AuthService
) { } async loginUser(event: FormGroup) {
const {email, password} = event.value;
try {
await this.authService.loginUser(email, password);
} catch(err) {
this.error = err.message;
}
}
}

[Angular] AuthService and AngularFire integration的更多相关文章

  1. [AngularFire 2 ] Hello World - How To Write your First Query using AngularFire 2 List Observables ?

    In this lesson we are going to use AngularFire 2 for the first time. We are going to configure the A ...

  2. Angular 2 技能图谱skill-map

    # Angular 2 技能图谱 ## 模块 ### 自定义模块 - 根模块 - 特性模块 - 共享模块 - 核心模块 ### 内置模块 - ApplicationModule 模块 - Common ...

  3. [Angular] Reactive Store and AngularFire Observables

    A simple store implemenet: import { Observable } from 'rxjs/Observable'; import { BehaviorSubject } ...

  4. [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor

    The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...

  5. angular单元测试与自动化UI测试实践

    关于本文:介绍通过karma与jsmine框架对angular开发的应用程序进行单元与E2E测试. angular单元测试与集成测试实践 先决条件 创建项目 webstorm中创建空白web项目 创建 ...

  6. Angular+Grunt+Bower+Karma+Protractor (Atom)

    1. 配置bower 1.安装bower npm install -g bower 2.创建.bowerrc文件 { "directory": "src/bower&qu ...

  7. angular测试-Karma + Jasmine配置

    首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...

  8. angular run()运行块

    和配置块不同,运行块在注入器创建之后被执行,它是所有AngularJS应用中第一个被执行的方法. 运行块是AngularJS中与main方法最接近的概念.运行块中的代码块通常很难进行单元测试,它是和应 ...

  9. Redux你的Angular 2应用--ngRx使用体验

    Angular2和Rx的相关知识可以看我的Angular 2.0 从0到1系列第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2 ...

随机推荐

  1. vim中使用正則表達式

    一.使用正則表達式的命令 使用正則表達式的命令最常见的就是 / (搜索)命令. 其格式例如以下: /正則表達式 还有一个非常实用的命令就是 :s(替换)命令,将第一个//之间的正則表達式替换成第二个/ ...

  2. Java 关于运算结果

    今天发现一个自己的一个知识误区 原来有些自己以为会发生异常的运算,并没有异常发生,只要运算符两端存在至少一个浮点数 比如: public class Demo { /** * @param args ...

  3. solrj简介

    SolrJ基于httpClient: 使用SolrJ操作Solr会比利用httpClient来操作Solr要简单. SolrJ是封装了httpClient方法,来操作solr的API的. SolrJ底 ...

  4. 00081_List接口

    1.List接口介绍 (1)有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元 ...

  5. ubuntu-通配符

    ubuntu下的通配符主要有三个 1.*  这个是匹配任意一个或多个字符 ab1.txt ab2.txt ab3.txt abc.txt 执行命令以及结果如下 zhangshuli@zhangshul ...

  6. php如何读写excel

    php如何读写excel 一.总结 一句话总结:PHP操作Excel最好的方法是使用PHPExcel类, 可以到官网下载PHPExcel类库 http://phpexcel.codeplex.com ...

  7. 【单词】常见单词含义的辨异(emulator/simulator、hardware/firmware)

    1. emulator 与 simulator The Simulator tries to duplicate the behavior of the device.(仿真的是行为): The Em ...

  8. C#截取中英文混合字符串分行显示

    private int GetStrByteLength(string str) { return System.Text.Encoding.Default.GetByteCount(str); } ...

  9. CF741DArpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(DSU on tree)

    题目大意: 给定一个以1为根的树,每条路径上都有一个字符(a~v共22个)询问对于每一个子树内最长的路径上字母经排序后可以形成回文串的最长路径多长 解题思路: 假定给你一个字符串,如何判定其经打乱能否 ...

  10. Scala中的“=>”和“<-”

    “=>”符号大概可以看做是创建函数实例的语法糖,例如 args.foreach(arg => println(arg)) 大概可以看做 args.foreach(Function(arg) ...