[英]how to get email authenticate with Gmail in angular and firebase?
我想在 angular 和 firebase 中對 gmail 進行電子郵件身份驗證,我創建了該功能
getAuth (){ return this.afAuth.authState.pipe (map (auth => auth));}
.
auth-client.service.ts
import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app'
@Injectable({
providedIn: 'root'
})
export class AuthClientService {
constructor(private afAuth:AngularFireAuth) { }
getAuth(){
return this.afAuth.authState.pipe(map(auth=>auth));
}
}
我將電子郵件身份驗證分配給 navbar.component.ts (userLoggedIn) 中的一個變量。
導航欄.component.ts
import { Router } from '@angular/router';
import { FlashMessagesService } from 'angular2-flash-messages';
import { AuthClientService } from './../../services/auth-client.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
isLoggedIn:boolean=false;
userLoggedIn:string;
constructor(
private authService:AuthClientService,
private flashMessage:FlashMessagesService,
private route:Router
) { }
ngOnInit() {
this.authService.getAuth().subscribe(auth=>{
if(auth){
return this.isLoggedIn=true;
this.userLoggedIn = auth.email;
}else{
this.isLoggedIn=false;
}
})
}
}
最后我想在 navbar.component.html 中顯示它,但是要顯示。 導航欄.component.html
<li class="nav-item">
<a routerLink="/login" class="nav-link">{{ userLoggedIn }} </a>
</li>
改變這個:
ngOnInit() {
this.authService.getAuth().subscribe(auth=>{
if(auth){
return this.isLoggedIn=true;
this.userLoggedIn = auth.email;
}else{
this.isLoggedIn=false;
}
})
}
進入這個:
ngOnInit() {
this.authService.getAuth().subscribe(auth=>{
if(auth){
this.userLoggedIn = auth.email;
this.isLoggedIn = true;
}else{
this.isLoggedIn = false;
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.