【问题标题】:Getting junk values within response from firebase APIs while calling getIdToken在调用 getIdToken 时从 firebase API 的响应中获取垃圾值
【发布时间】:2020-05-31 17:15:31
【问题描述】:

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 token =(await this.afAuth.currentUser).getIdToken();

console.log(token); ==> "D {a: 0, i: undefined, c: D, b: null, f: null, ...} " 得到一个额外的 D ,因此无法从这个结构中单独提取令牌,附加服务调用多于。提前致谢,

如果有什么需要请告诉我:)

【问题讨论】:

    标签: firebase oauth google-api firebase-authentication


    【解决方案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']);
      }
    
    
    }

    【讨论】:

    • 你能说得更具体些吗:)
    【解决方案2】:

    这对我有用

    const token =(await this.afAuth.currentUser).getIdToken().then(function(idToken){ localStorage.setItem('token',idToken); console.log(idToken) });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      相关资源
      最近更新 更多