在调用getIdToken的同时从firebase API的响应中获取垃圾值

时间:2020-05-31 17:15:31

标签: firebase oauth google-api firebase-authentication

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
// import { User } from './user.model.ts'; // optional

import { auth } from 'firebase/app';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';

import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { User } from './user';
import { HttpClient } from '@angular/common/http';
import { UserService } from './user.service';

@Injectable({ providedIn: 'root' })
export class AuthService {

  user$: Observable<User>;

  constructor(
    private userServe: UserService,
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore,
    private router: Router
  ) {

    this.user$ = this.afAuth.authState.pipe(
      switchMap(user => {
        // Logged in
        if (user) {
          this.router.navigate(["user_home"]);
          return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
        } else {
          // Logged out
          return of(null);
        }
      })
    )
  }
  async googleSignin() {
    const provider = new auth.GoogleAuthProvider();
    const credential = await this.afAuth.signInWithPopup(provider);
   
   const token =(await this.afAuth.currentUser).getIdToken();
   
   console.log(token);
    // localStorage.setItem('token',JSON.stringify(token));
    // var token = credential.accessToken;
    // alert(JSON.stringify(token));
    return this.updateUserData(credential.user);
  }

  private updateUserData(user) {
    // Sets user data to firestore on login
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);

    const data = {
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      photoURL: user.photoURL,
      username: user.displayName,
      password: '',
      phone: 0,
      state: '',
      district: '',
      city: '',
      pincode: 0,
    }
    this.userServe.postUserData(data)
      .subscribe(data => {
        console.log("Account added");
      }, error => {
        console.log(error);
      }
      );
    return userRef.set(data, { merge: true })

  }

  async signOut() {
    await this.afAuth.signOut();
    // this.router.navigate(['login']);
  }


}

我在通过google进行Firebase身份验证时,从服务中调用了api 得到如下格式的值 const令牌=(等待this.afAuth.currentUser).getIdToken();

console.log(令牌); ==>“ D {a:0,i:undefined,c:D,b:null,f:null,…}”“获得了额外的D,因此无法单独从此结构中提取令牌,因此附加了服务调用以上。预先感谢,

请让我知道更多需要的信息:)

2 个答案:

答案 0 :(得分:1)

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
// import { User } from './user.model.ts'; // optional

import { auth } from 'firebase/app';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';

import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { User } from './user';
import { HttpClient } from '@angular/common/http';
import { UserService } from './user.service';

@Injectable({ providedIn: 'root' })
export class AuthService {

  user$: Observable<User>;

  constructor(
    private userServe: UserService,
    private afAuth: AngularFireAuth,
    private afs: AngularFirestore,
    private router: Router
  ) {

    this.user$ = this.afAuth.authState.pipe(
      switchMap(user => {
        // Logged in
        if (user) {
          this.router.navigate(["user_home"]);
          return this.afs.doc<User>(`users/${user.uid}`).valueChanges();
        } else {
          // Logged out
          return of(null);
        }
      })
    )
  }
  async googleSignin() {
    const provider = new auth.GoogleAuthProvider();
    const credential = await this.afAuth.signInWithPopup(provider);
   
   const token =(await this.afAuth.currentUser).getIdToken();
   
   console.log(token);
    // localStorage.setItem('token',JSON.stringify(token));
    // var token = credential.accessToken;
    // alert(JSON.stringify(token));
    return this.updateUserData(credential.user);
  }

  private updateUserData(user) {
    // Sets user data to firestore on login
    const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);

    const data = {
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      photoURL: user.photoURL,
      username: user.displayName,
      password: '',
      phone: 0,
      state: '',
      district: '',
      city: '',
      pincode: 0,
    }
    this.userServe.postUserData(data)
      .subscribe(data => {
        console.log("Account added");
      }, error => {
        console.log(error);
      }
      );
    return userRef.set(data, { merge: true })

  }

  async signOut() {
    await this.afAuth.signOut();
    // this.router.navigate(['login']);
  }


}

<路由器>

答案 1 :(得分:0)

这对我有用

常量令牌=(等待this.afAuth.currentUser).getIdToken()。then(function(idToken){ localStorage.setItem('token',idToken); console.log(idToken)});