[Angular] AuthService and AngularFire integration
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的更多相关文章
- [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 ...
- Angular 2 技能图谱skill-map
# Angular 2 技能图谱 ## 模块 ### 自定义模块 - 根模块 - 特性模块 - 共享模块 - 核心模块 ### 内置模块 - ApplicationModule 模块 - Common ...
- [Angular] Reactive Store and AngularFire Observables
A simple store implemenet: import { Observable } from 'rxjs/Observable'; import { BehaviorSubject } ...
- [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, ...
- angular单元测试与自动化UI测试实践
关于本文:介绍通过karma与jsmine框架对angular开发的应用程序进行单元与E2E测试. angular单元测试与集成测试实践 先决条件 创建项目 webstorm中创建空白web项目 创建 ...
- Angular+Grunt+Bower+Karma+Protractor (Atom)
1. 配置bower 1.安装bower npm install -g bower 2.创建.bowerrc文件 { "directory": "src/bower&qu ...
- angular测试-Karma + Jasmine配置
首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...
- angular run()运行块
和配置块不同,运行块在注入器创建之后被执行,它是所有AngularJS应用中第一个被执行的方法. 运行块是AngularJS中与main方法最接近的概念.运行块中的代码块通常很难进行单元测试,它是和应 ...
- Redux你的Angular 2应用--ngRx使用体验
Angular2和Rx的相关知识可以看我的Angular 2.0 从0到1系列第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2 ...
随机推荐
- hdu1856 More is better (并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- Bounding Volume Hierarchy BVH in OpenCASCADE
Bounding Volume Hierarchy BVH in OpenCASCADE eryar@163.com Abstract. Bounding Volume Hierarchy(BVH) ...
- IOS打包发布APP的所有详细流程
其他一些不错的参考:点击打开链接 一.申请苹果开发者账号 首先需要申请苹果开发者账号才能在APP store 里发布应用. 开发者账号分为:(1)个人开发者账号 (2)企业开发者账号 主要的区 ...
- Kinect 开发 —— 进阶指引 (下)
运动识别 利用运动识别(motion detection)来进行近景识别是最有意思的一种方式.实现运动识别的基本原理是设置一个起始的基准RGB图像,然后将从摄像头获取的每一帧影像和这个基准图像进行比较 ...
- [置顶]
Docker学习总结(5)——超实用Docker入门学习教程
Docker是什么 Docker是一种容器技术,它可以将应用和环境等进行打包,形成一个独立的,类似于iOS的APP形式的"应用",这个应用可以直接被分发到任意一个支持Docker的 ...
- actionBar-双行字体大小修改
<style name="BackupRestore.Theme.Person" parent="@style/BackupRestore.Theme"& ...
- stm32与arm7比较(经典)
http://wenku.baidu.com/link?url=LIVcT1AQL0IgVF1xan5Zy9rXarCBo66hj7OXSxM1ap7FpssO4c3sd1pZd8azfBPr3PBy ...
- Spring中提供的集合工具类util CollectionUtils
转自:https://blog.csdn.net/fangwenzheng88/article/details/78457850 CollectionUtils类 /* * Copyright 200 ...
- less中混合
@charset "UTF-8"; //1 普通混合 //2 不带输出的混合:加() .font_hx(){ font-size: 28px; color: red; } h1{ ...
- 原生JavaScript 封装ajax
原生JavaScript 封装ajax function myajax(options){ //新建一个局部对象 用来存放用户输入的各种参数 var opt={ type:options.type ...