Skip to content

Commit cf58543

Browse files
committed
Fixes refresh issues with Firebase Auth
firebase/firebase-js-sdk#2985
1 parent 0810ac0 commit cf58543

File tree

6 files changed

+111
-52
lines changed

6 files changed

+111
-52
lines changed

src/capturoo-client/index.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const CAPTUROO_ENDPOINT = 'https://api-staging.capturoo.com'
7777
class CapturooClient {
7878
static client
7979
endpoint: string
80+
user: firebase.User
8081
claimAccountId: string
8182
claimRole: string | undefined
8283
claimUid: string | undefined
@@ -116,6 +117,31 @@ class CapturooClient {
116117
}
117118
}
118119

120+
setFirebaseUser(user: firebase.User) {
121+
this.user = user
122+
}
123+
124+
setJWT(jwt: string) {
125+
this.jwt = jwt
126+
}
127+
128+
setClaims(claims: any) {
129+
this.claimAccountId = undefined
130+
if ('cap_aid' in claims) {
131+
this.claimAccountId = claims['cap_aid']
132+
}
133+
134+
this.claimRole = undefined
135+
if ('cap_role' in claims) {
136+
this.claimRole = claims['cap_role']
137+
}
138+
139+
this.claimUid = undefined
140+
if ('user_id' in claims) {
141+
this.claimUid = claims['user_id']
142+
}
143+
}
144+
119145
async get(url: string, query?: URLSearchParams) {
120146
if (!query) {
121147
query = null
@@ -131,17 +157,20 @@ class CapturooClient {
131157
return this.do(url, 'DELETE', null, null);
132158
}
133159

134-
private async do(url: string, method: string, query: URLSearchParams | null, body: object | null, noAuth?: boolean) : Promise<Response> {
135-
let opts : any = {
160+
private async do(url: string, method: string, query: URLSearchParams | null, body: object | null, noAuth?: boolean): Promise<Response> {
161+
const opts : any = {
136162
method,
137163
headers: {
138164
'Accept': 'application/json',
139-
'Authorization': noAuth ? '' : `Bearer ${this.jwt}`,
165+
140166
},
141167
mode: 'cors'
142168
}
143-
if (noAuth) {
144-
delete opts.headers['Authorization']
169+
console.log('a')
170+
if (noAuth === false) {
171+
console.log('adding auth header')
172+
const { token } = await this.user.getIdTokenResult()
173+
opts.headers['Authorization'] = `Bearer ${token}`
145174
}
146175

147176
if ((method === 'POST') && (!body)) {
@@ -179,27 +208,6 @@ class CapturooClient {
179208
}
180209
}
181210

182-
setJWT(jwt: string) {
183-
this.jwt = jwt
184-
}
185-
186-
setClaims(claims: any) {
187-
this.claimAccountId = undefined
188-
if ('cap_aid' in claims) {
189-
this.claimAccountId = claims['cap_aid']
190-
}
191-
192-
this.claimRole = undefined
193-
if ('cap_role' in claims) {
194-
this.claimRole = claims['cap_role']
195-
}
196-
197-
this.claimUid = undefined
198-
if ('user_id' in claims) {
199-
this.claimUid = claims['user_id']
200-
}
201-
}
202-
203211
async getFirebaseConfig(): Promise<FirebaseConfig> {
204212
if (!this.firebaseConfig) {
205213
const response = await this.do('/autoconf', 'GET', null, null, true)

src/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import capturoo from './capturoo-client'
2525
// init capturoo
2626
capturoo.initializeApp({
2727
endpoint: hostnameToEndpoint(),
28-
debug: false
28+
debug: true
2929
})
3030
const firebaseConfig = await capturoo.admin().getFirebaseConfig()
3131
firebase.initializeApp(firebaseConfig)
@@ -35,8 +35,12 @@ import capturoo from './capturoo-client'
3535
firebase.auth().onAuthStateChanged(async (user) => {
3636
if (user) {
3737
store.commit('setUser', user)
38+
39+
// https://github.com/firebase/firebase-js-sdk/issues/2985
40+
capturoo.admin().setFirebaseUser(user)
41+
3842
const idTokenResult = await user.getIdTokenResult()
39-
capturoo.admin().setJWT(idTokenResult.token)
43+
// capturoo.admin().setJWT(idTokenResult.token)
4044
capturoo.admin().setClaims(idTokenResult.claims)
4145
} else {
4246
store.commit('setUser', undefined)

src/store/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ Vue.use(Vuex)
88
export default new Vuex.Store({
99
state: {
1010
account: null,
11+
bucket: null,
1112
buckets: [],
1213
user: null
1314
},
1415
getters: {
1516
account(state) {
1617
return state.account
1718
},
19+
bucket(state) {
20+
return state.bucket
21+
},
1822
buckets(state) {
1923
return state.buckets
2024
},
@@ -32,6 +36,9 @@ export default new Vuex.Store({
3236
setUser(state, user: firebase.User) {
3337
state.user = user
3438
},
39+
setBucket(state, bucket) {
40+
state.bucket = bucket
41+
},
3542
setBuckets(state, buckets) {
3643
state.buckets = buckets
3744
},
@@ -99,15 +106,27 @@ export default new Vuex.Store({
99106
throw err
100107
}
101108
},
102-
async getBuckets() {
109+
async getBucket({ commit }, { bucketId }) {
110+
try {
111+
const bucket = await capturoo.admin().getBucket(bucketId)
112+
commit('setBucket', bucket)
113+
return bucket
114+
} catch (err) {
115+
throw err
116+
}
117+
},
118+
async getBuckets({ commit }) {
103119
try {
104120
const buckets = await capturoo.admin().getBuckets()
105-
this.commit('setBuckets', buckets)
121+
commit('setBuckets', buckets)
106122
return buckets
107123
} catch (err) {
108124
throw err
109125
}
110126
},
127+
resetBucket({ commit }) {
128+
commit('setBucket', null)
129+
},
111130
async deleteBucket({ commit, state }, { bucketId }) {
112131
const results = state.buckets.filter(b => {
113132
return b.bucketId === bucketId

src/views/account/AccountSettings.vue

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<v-tab>Developer Keys</v-tab>
1212
</v-tabs>
1313

14-
<v-tabs-items v-model="tab">
14+
<v-tabs-items v-if="account" v-model="tab">
1515
<v-tab-item :transition="false" :reverse-transition="false">
1616
<v-card flat>
1717
<v-card-text>
@@ -58,18 +58,13 @@ export default {
5858
}
5959
},
6060
beforeMount: async function() {
61-
this.loading = true;
62-
console.log('loading account')
63-
const account = await this.getBuckets();
64-
console.log('account loaded')
65-
console.log(account)
61+
if (!this.$store.getters.account) {
62+
await this.getAccount();
63+
}
6664
},
6765
methods: {
68-
async getBuckets() {
69-
const account = await this.$store.dispatch('getAccount')
70-
console.log('method: getBuckets')
71-
console.log(account)
72-
return account
66+
async getAccount() {
67+
await this.$store.dispatch('getAccount')
7368
},
7469
async deleteConfirm(bucketId) {
7570
await this.$store.dispatch('deleteBucket', { bucketId })

src/views/buckets/BucketDetails.vue

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<v-divider></v-divider>
2020

21-
<v-container fluid>
21+
<v-container v-if="bucket" fluid>
2222
<v-row>
2323
<v-col cols="12" x="6" md="6">
2424
<v-icon
@@ -27,24 +27,54 @@
2727
color="success"
2828
class="pr-4 pb-1"
2929
>mdi-cloud-check</v-icon>
30-
<span class="font-weight-light title">skincare</span>
30+
<span class="font-weight-light title">{{ bucket.resourceName }}</span>
3131
</v-col>
3232
</v-row>
3333

3434
<v-row>
3535
<v-col cols="12" xs="6" md="6">
3636
<div class="caption grey--text">Bucket name</div>
37-
<div>My skincare campaign</div>
37+
<div>{{ bucket.bucketName }}</div>
3838
</v-col>
3939
</v-row>
4040

4141
<v-row>
4242
<v-col cols="12" xs="6" md="6">
4343
<div class="caption grey--text">Public API Key</div>
44-
<div>ObGsEG4x</div>
44+
<div>{{ bucket.publicApiKey }}</div>
4545
</v-col>
4646
</v-row>
4747

48+
<v-row>
49+
<v-col cols="12" xs="6" md="6">
50+
<div class="caption grey--text">Created</div>
51+
<div><p class="grey--text text--darken-1 pt-0 body-2">{{ bucket.created }}</p></div>
52+
</v-col>
53+
</v-row>
4854
</v-container>
4955
</div>
5056
</template>
57+
58+
<script lang="ts">
59+
export default {
60+
computed: {
61+
bucket() {
62+
return this.$store.getters.bucket
63+
}
64+
},
65+
beforeMount: async function() {
66+
await this.getBucket(this.$route.params.bucketId);
67+
},
68+
beforeDestroy() {
69+
this.$store.dispatch('resetBucket')
70+
},
71+
methods: {
72+
async getBucket(bucketId) {
73+
await this.$store.dispatch('getBucket', { bucketId })
74+
},
75+
async deleteConfirm(bucketId) {
76+
await this.$store.dispatch('deleteBucket', { bucketId })
77+
}
78+
}
79+
}
80+
</script>

src/views/buckets/Buckets.vue

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<v-container fluid>
1616
<p class="text--secondary">Buckets provide containers for collections of leads.</p>
17-
<v-simple-table class="mt-4">
17+
<v-simple-table v-if="buckets" class="mt-4">
1818
<template v-slot:default>
1919
<thead>
2020
<tr>
@@ -31,7 +31,7 @@
3131
<tr v-for="bucket in buckets" :key="bucket.resourceName">
3232
<td>
3333
<router-link
34-
:to="{ name: 'view-bucket', params: { bucketId: 'xyz' }}"
34+
:to="{ name: 'view-bucket', params: { bucketId: bucket.bucketId }}"
3535
>
3636
{{ bucket.resourceName }}
3737
</router-link>
@@ -73,7 +73,7 @@
7373
</div>
7474
</template>
7575

76-
<script>
76+
<script lang="ts">
7777
import ConfirmDialog from '@/components/ConfirmDialog.vue'
7878
7979
export default {
@@ -82,9 +82,12 @@ export default {
8282
},
8383
computed: {
8484
buckets() {
85-
return this.$store.getters.buckets.map(b => {
86-
return b.data()
87-
})
85+
if (this.$store.getters.buckets) {
86+
return this.$store.getters.buckets.map(b => {
87+
return b.data()
88+
})
89+
}
90+
return null
8891
}
8992
},
9093
data() {
@@ -98,7 +101,7 @@ export default {
98101
},
99102
methods: {
100103
async getBuckets() {
101-
const buckets = await this.$store.dispatch('getBuckets')
104+
await this.$store.dispatch('getBuckets')
102105
},
103106
async deleteConfirm(bucketId) {
104107
await this.$store.dispatch('deleteBucket', { bucketId })

0 commit comments

Comments
 (0)