How to get the Firebase Realtime Database AuthToken
我从开发人员那里获得了基于Firabase Realtime DB的Ionic App的代码。 尝试启动应用程序时,我会收到此错误消息。
[ng] src / app / data-service.service.ts(14,36)中的错误:错误TS2339:类型'{apiKey:string;类型不存在属性'authToken' authDomain:字符串; databaseURL:字符串; projectId:字符串; storageBucket:字符串; messagesSenderId:字符串; }'。
不幸的是,我无法弄清楚如何从Firebase获取authToken。
数据服务
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { environment } from '../environments/environment'; import * as firebase from 'firebase/app'; @Injectable({ providedIn: 'root' }) export class DataServiceService { userId: any; userName: any; databaseUrl = environment.firebase.databaseURL; authToken = environment.firebase.authToken; constructor(private http: HttpClient) { console.log('Hello DataServiceService Provider'); console.log(this.databaseUrl); console.log(this.authToken); this.userId = localStorage.getItem('userId'); if (!localStorage.getItem('currentUserShift')) { localStorage.setItem('currentUserShift', JSON.stringify({ periods: {} })); } if (!localStorage.getItem('pendingShifts')) { localStorage.setItem('pendingShifts', JSON.stringify([])); } } setUserId(userId) { console.log('setUserId '); this.userId = userId; localStorage.setItem('userId', userId); } getUserId() { return this.userId; } getUserName() { return this.userName; } setUserName(userName) { this.userName = userName; } updateLocalCurrentUserShift(currentUserShift) { localStorage.setItem('currentUserShift', JSON.stringify(currentUserShift)); } getLocalCurrentUserShift() { return JSON.parse(localStorage.getItem('currentUserShift')); } async insertUserCurrentShiftToPendingShifts(currentShift) { const pendingShifts = JSON.parse(localStorage.getItem('pendingShifts')); pendingShifts.push(currentShift) localStorage.setItem('pendingShifts', JSON.stringify(pendingShifts)); this.updateLocalCurrentUserShift({ periods: {} }); await this.insertPendingShiftsToFirebase(); // localStorage.setItem('currentUserShift', JSON.stringify({})); } async insertPendingShiftsToFirebase() { const pendingShifts = JSON.parse(localStorage.getItem('pendingShifts')); console.log('pendingShifts:', pendingShifts); const failedToBeSentPendingShifts = []; for (let i = 0 ; i < pendingShifts.length ; i++) { try { const key = await this.insertUserCurrentShifttoHistory(pendingShifts[i]); console.log('key' , key); } catch (err) { console.log('Error while inserting into firebase', JSON.stringify(err)); failedToBeSentPendingShifts.push(pendingShifts[i]); } } localStorage.setItem('pendingShifts', JSON.stringify(failedToBeSentPendingShifts)); return true; } createFirebaseId() { return firebase.database().ref('/dummy').push().key; } getProducts() { return this.http.get(`${this.databaseUrl}/products/.json${this.authToken?('?auth='+this.authToken):''}`); } getOrganizations() { return this.http.get(`${this.databaseUrl}/organizations/.json${this.authToken?('?auth='+this.authToken):''}`); } insertUser(data) { return this.http.put(`${this.databaseUrl}/users/${data.uId}.json${this.authToken?('?auth='+this.authToken):''}`, data); } // an API to insert product views with firebase generated key insertProductViewAnalaytics(data) { return this.http.post(`${this.databaseUrl}/users/${this.userId}/product_views.json${this.authToken?('?auth='+this.authToken):''}`, data); } getProductViewsForUser() { return this.http.get(`${this.databaseUrl}/users/${this.userId}/product_views/.json${this.authToken?('?auth='+this.authToken):''}`); } getUserOrganization() { return this.http.get(`${this.databaseUrl}/users/${this.userId}/organization/.json${this.authToken?('?auth='+this.authToken):''}`); } fetchUserName() { return this.http.get(`${this.databaseUrl}/users/${this.userId}/userName/.json${this.authToken?('?auth='+this.authToken):''}`); } // updateUserName(data) { // return this.http.patch(`${this.databaseUrl}/users/${this.userId}/.json${this.authToken?('?auth='+this.authToken):''}`, data); // } // an API to insert product views with custom timestamp based key insertProductViewAnalayticsTimestamp(data) { return this.http.put(`${this.databaseUrl}/product_views_timestamp/${data.scannedAt}.json${this.authToken?('?auth='+this.authToken):''}`, data); } // insertUserCurrentShift(data) { // return this.http.patch(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`, data); // } // updateUserCurrentShift(data) { // return this.http.patch(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`, data); // } // getUserCurrentShiftStartTime() { // return this.http.get(`${this.databaseUrl}/users/${this.userId}/currentShift/currentPeriodStartedAt.json${this.authToken?('?auth='+this.authToken):''}`); // } // getUserCurrentShiftStatus() { // return this.http.get(`${this.databaseUrl}/users/${this.userId}/currentShift/status.json${this.authToken?('?auth='+this.authToken):''}`); // } // insertUserPeriods(data) { // return this.http.post(`${this.databaseUrl}/users/${this.userId}/currentShift/periods.json${this.authToken?('?auth='+this.authToken):''}`, data); // } // getUserCurrentShift() { // return this.http.get(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`); // } getPendingShifts() { return JSON.parse(localStorage.getItem('pendingShifts')); } async insertUserCurrentShifttoHistory(data) { data.userId = this.userId; // return firebase.database().ref('/shiftHistory').push(data).key; if (data.startedAt && data.endedAt) { return this.http.post(`${this.databaseUrl}/shiftHistory/.json${this.authToken ? ('?auth=' + this.authToken) : ''}`, data).toPromise(); } else { console.log('invalid data found, not inserting into firebase', JSON.stringify(data)); return null; } } // removeUserCurrentShift(){ // return this.http.delete(`${this.databaseUrl}/users/${this.userId}/currentShift.json${this.authToken?('?auth='+this.authToken):''}`); // } getUserWorkTimeHistory() { return this.http.get(`${this.databaseUrl}/shiftHistory.json${this.authToken?('?auth='+this.authToken):''}&orderBy="userId"&equalTo="${this.userId}"`); } insertUserCurrentLocation(data) { return this.http.post(`${this.databaseUrl}/users/${this.userId}/locationHistory.json${this.authToken?('?auth='+this.authToken):''}`, data); } } |
环境产品
1 2 3 | export const environment = { production: true }; |
环境
1 2 3 4 5 6 7 8 9 10 11 | export const environment = { production: false, firebase: { apiKey:"...", authDomain:"...", databaseURL:"...", projectId:"...", storageBucket:"...", messagingSenderId:"..." } }; |
提前致谢!
我发现这种方法:
1 2 3 4 5 6 7 | firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) { // Send token to your backend via HTTPS // ... }).catch(function(error) { // Handle error }); |