本文摘自PHP中文网,作者藏色散人,侵删。
Firebase提供了一种非常简单的方法来实现设置身份验证。本篇文章将给大家介绍如何使用AngularFire2为Angular 2+应用程序设置一个简单的电子邮件/密码注册和认证工作流。首先创建一个新的Firebase项目,并在Firebase控制台的Authentication(身份验证)部分启用电子邮件/密码登录方法。
让我们从使用npm安装必要的包开始,添加Firebase SDK, AngularFire2和promise-polyfill依赖项到你的项目:
1 | $ npm install firebase angularfire2 --save
|
1 | $ npm install promise-polyfill --save-exact
|
现在,让我们将Firebase API和项目凭据添加到项目的环境变量中。如果你点击添加Firebase到你的web应用程序,你可以在Firebase控制台中找到这些值:
src/environments/environment.ts
1 2 3 4 5 6 7 8 9 10 11 | export const environment = {
production: false,
firebase: {
apiKey: 'XXXXXXXXXXX' ,
authDomain: 'XXXXXXXXXXX' ,
databaseURL: 'XXXXXXXXXXX' ,
projectId: 'XXXXXXXXXXX' ,
storageBucket: 'XXXXXXXXXXX' ,
messagingSenderId: 'XXXXXXXXXXX'
}
};
|
然后我们将使用AngularFireModule和AngularFireAuthModule配置我们的app模块。还要注意,我们正在导入并提供AuthService,接下来将创建该服务:
app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import { AngularFireModule } from 'angularfire2' ;
import { environment } from '../environments/environment' ;
import { AngularFireAuthModule } from 'angularfire2/auth' ;
import { AuthService } from './auth.service' ;
@NgModule({
declarations: [
AppComponent
],
imports: [
AngularFireModule.initializeApp(environment.firebase),
AngularFireAuthModule
],
providers: [AuthService],
bootstrap: [AppComponent]
})
export class AppModule { }
|
创建Auth服务
我们的服务将是一个允许我们登录、注册或注销用户的中心位置,因此我们将为这三个操作定义方法。所有的身份验证逻辑都将在服务中,这将允许我们创建不需要实现任何auth逻辑的组件,并帮助保持组件的简单性。
使用Angular CLI为服务创建骨架,如下命令:
下面是该服务的实现,使用AngularFireAuth:
auth.service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import { Injectable } from '@angular/core' ;
import { AngularFireAuth } from 'angularfire2/auth' ;
import * as firebase from 'firebase/app' ;
import { Observable } from 'rxjs/Observable' ;
@Injectable()
export class AuthService {
user: Observable<firebase.User>;
constructor( private firebaseAuth: AngularFireAuth) {
this.user = firebaseAuth.authState;
}
signup(email: string, password: string) {
this.firebaseAuth
.auth
.createUserWithEmailAndPassword(email, password)
.then(value => {
console.log( 'Success!' , value);
})
. catch (err => {
console.log( 'Something went wrong:' ,err.message);
});
}
login(email: string, password: string) {
this.firebaseAuth
.auth
.signInWithEmailAndPassword(email, password)
.then(value => {
console.log( 'Nice, it worked!' );
})
. catch (err => {
console.log( 'Something went wrong:' ,err.message);
});
}
logout() {
this.firebaseAuth
.auth
.signOut();
}
}
|
你会注意到AngularFireAuth.auth上可用的方法都返回promise,因此我们可以使用then和catch分别处理成功和错误状态。
我们在这里使用createUserWithEmailAndPassword和signInWithEmailAndPassword。
组件类和模板
现在我们的auth服务已经就绪,创建一个允许登录、注册或注销的组件也很简单:
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import { Component } from '@angular/core' ;
import { AuthService } from './auth.service' ;
@Component({ ... })
export class AppComponent {
email: string;
password: string;
constructor( public authService: AuthService) {}
signup() {
this.authService.signup(this.email, this.password);
this.email = this.password = '' ;
}
login() {
this.authService.login(this.email, this.password);
this.email = this.password = '' ;
}
logout() {
this.authService.logout();
}
}
|
我们将服务注入组件的构造函数中,然后定义本地方法,这些方法调用auth服务上的等效方法。我们使用public关键字而不是private注入服务,这样我们也可以直接从模板访问服务属性。
模板非常简单,使用authService的user对象上的async管道来确定是否有登录用户:
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <h1 *ngIf= "authService.user | async" >Welcome {{ (authService.user | async)?.email }}!</h1>
<div *ngIf= "!(authService.user | async)" >
<input type= "text" [(ngModel)]= "email" placeholder= "email" >
<input type= "password" [(ngModel)]= "password" placeholder= "email" >
<button (click)= "signup()" [disabled]= "!email || !password" >
注册
</button>
<button (click)= "login()" [disabled]= "!email || !password" >
登录
</button>
</div>
<button (click)= "logout()" *ngIf= "authService.user | async" >
注销
</button>
|
我们的输入字段使用ngModel和框架语法中的banana,双向绑定到组件类中的电子邮件和密码值。
相关推荐:《angularjs教程》
本篇文章就是关于Firebase身份验证的相关介绍,希望对需要的朋友有所帮助!
以上就是Angular中的Firebase身份验证(代码示例)的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
20个优秀的Angular开源项目,你了解几个呢?
浅谈Angular如何编译打包?如何使用docker发布?
了解Angularjs中的“模块”
详解Angular根模块与特性模块
5种Angular中组件通信的方法介绍
浅谈Angular项目中如何引入第三方ui库(Angular material)
详解Angular中为html元素添加css类的几种方式
Angular脏值检测与vue数据劫持的区别是什么
12个关于Angular的经典面试题
详解Angular中的observable(可观察对象)
更多相关阅读请进入《Angular》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » Angular中的Firebase身份验证(代码示例)