Skip to content

Commit fcf0846

Browse files
committed
feat(auth): added onIdTokenChanged() handlers to auth.initialize
Added onIdTokenChanged() handlers to auth.initialize re #411
1 parent 0691660 commit fcf0846

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

docs/content/en/service-options/auth.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ Set firebase auth persistence, see [here](https://firebase.google.com/docs/auth/
3030

3131
## initialize
3232

33-
This sets up an `onAuthStateChanged()` listener and hooks it up to the vuex store.
33+
This sets up an `onAuthStateChanged()` and/or `onIdTokenChanged()` listener and hooks them up to the vuex store.
3434

3535
Just add a mutation/action to your vuex store ([as seen below](#onauthstatechangedmutation)) that handles what to do with the authUser object (e.g. save it to the state or get user data from FireStore) and then define the name of the action/mutation in the `firebase.services.auth.initialize` configuration as above.
3636

3737
<alert type="info">You can also use namespaces for your store actions/mutations like so: `onAuthStateChangedAction: 'namespaceName/actionName'`.</alert>
3838

39-
When onAuthStateChanged() gets triggered by Firebase, the defined mutation/action will be called with the `authUser` and `claims` attributes as [as seen below](#onauthstatechangedmutation)
39+
When `onAuthStateChanged()` or `onIdTokenChanged()` get triggered by Firebase, the defined mutation/action will be called with the `authUser` and `claims` attributes as [as seen below](#onauthstatechangedmutation)
4040

41-
To unsubscribe from the listener simply call the `$fireAuthStore.unsubscribe()` function.
41+
To unsubscribe from both listeners simply call the `$fireAuthStore.unsubscribe()` function.
4242

4343
<alert type="warning">This does not work in *lazy-mode*, since auth is not initialized. If you want to use this option in lazy-mode, call the *authReady()* function in a separate plugin.</alert>
4444

@@ -86,6 +86,21 @@ onAuthStateChangedAction: (ctx, { authUser, claims }) => {
8686
}
8787
```
8888

89+
### onIdTokenChangedAction
90+
91+
Same as `onAuthStateChangedAction`, but also gets triggered when the idToken changes (e.g. expires).
92+
93+
<alert type="warning">
94+
The Firebase SDK automatically refreshed your id token, so this option shall only be used if you use the idToken for custom authentication scenarios.
95+
</alert>
96+
97+
### onIdTokenChangedMutation
98+
99+
Same as `onAuthStateChangedMutation`, but also gets triggered when the idToken changes (e.g. expires).
100+
101+
<alert type="warning">
102+
The Firebase SDK automatically refreshed your id token, so this option shall only be used if you use the idToken for custom authentication scenarios.
103+
</alert>
89104
### subscribeManually
90105

91106
By settings `subscribeManually: true`, the `onAuthStateChanged()` listener won't be set up until you call it manually:

lib/plugins/services/auth.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<% const { serviceMapping, serviceOptions, writeImportStatement } = options %>
22

3+
<% const hasOnAuthStateChangedMutation = (typeof serviceOptions.initialize.onAuthStateChangedMutation === 'string') %>
4+
<% const hasOnAuthStateChangedAction = (typeof serviceOptions.initialize.onAuthStateChangedAction === 'string') %>
5+
<% const hasOnAuthStateChanged = (hasOnAuthStateChangedMutation || hasOnAuthStateChangedAction) %>
6+
<% const onIdTokenChangedMutation = (typeof serviceOptions.initialize.onIdTokenChangedMutation === 'string') %>
7+
<% const onIdTokenChangedAction = (typeof serviceOptions.initialize.onIdTokenChangedAction === 'string') %>
8+
<% const hasIdTokenChanged = (onIdTokenChangedMutation || onIdTokenChangedAction) %>
9+
310
export default async function (session, firebase, ctx, inject) {
411

512
<% if (!serviceOptions.static) { %>
@@ -42,32 +49,52 @@ export default async function (session, firebase, ctx, inject) {
4249
// Sets up a listener, mutations and action for every onAuthStateChanged by Firebase.
4350
const fireAuthStore = {
4451
unsubscribe() {
45-
if (this.off) {
46-
this.off()
47-
delete this.off
52+
if (this.unsubscribeAuthStateListener) {
53+
this.unsubscribeAuthStateListener()
54+
delete this.unsubscribeAuthStateListener
55+
}
56+
if (this.unsubscribeIdTokenListener) {
57+
this.unsubscribeIdTokenListener()
58+
delete this.unsubscribeIdTokenListener
4859
}
4960
},
5061
subscribe() {
51-
if (this.off) {
52-
// already subscribed
53-
return
54-
}
5562

63+
<% if (hasOnAuthStateChanged && !this.unsubscribeAuthStateListener) { %>
5664
return new Promise(resolve => {
57-
this.off = authService.onAuthStateChanged(async authUser => {
65+
this.unsubscribeAuthStateListener = authService.onAuthStateChanged(async authUser => {
5866
const claims = authUser ? (await authUser.getIdTokenResult(true)).claims : null
5967

60-
<% if (typeof serviceOptions.initialize.onAuthStateChangedMutation === 'string') { %>
61-
ctx.store.commit(<%= serialize(serviceOptions.initialize.onAuthStateChangedMutation) %>, { authUser, claims })
68+
<% if (hasOnAuthStateChangedMutation) { %>
69+
ctx.store.commit(<%= serialize(serviceOptions.initialize.onAuthStateChangedMutation) %>, { authUser, claims })
6270
<% } %>
6371

64-
<% if (typeof serviceOptions.initialize.onAuthStateChangedAction === 'string') { %>
72+
<% if (hasOnAuthStateChangedAction) { %>
6573
await ctx.store.dispatch(<%= serialize(serviceOptions.initialize.onAuthStateChangedAction) %>, { authUser, claims })
6674
<% } %>
6775

6876
resolve()
6977
})
7078
})
79+
<% } %>
80+
81+
<% if (hasIdTokenChanged && !this.unsubscribeIdTokenListener) { %>
82+
return new Promise(resolve => {
83+
this.unsubscribeIdTokenListener = authService.onIdTokenChanged(async authUser => {
84+
const claims = authUser ? (await authUser.getIdTokenResult(true)).claims : null
85+
86+
<% if (onIdTokenChangedMutation) { %>
87+
ctx.store.commit(<%= serialize(serviceOptions.initialize.onIdTokenChangedMutation) %>, { authUser, claims })
88+
<% } %>
89+
90+
<% if (onIdTokenChangedAction) { %>
91+
await ctx.store.dispatch(<%= serialize(serviceOptions.initialize.onIdTokenChangedAction) %>, { authUser, claims })
92+
<% } %>
93+
94+
resolve()
95+
})
96+
})
97+
<% } %>
7198
}
7299
}
73100
inject('fireAuthStore', fireAuthStore)

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export interface AuthServiceConfig extends ServiceConfig {
3838
initialize?: {
3939
onAuthStateChangedMutation?: string
4040
onAuthStateChangedAction?: string
41+
onIdTokenChangedMutation?: string
42+
onIdTokenChangedAction?: string
4143
subscribeManually?: boolean
4244
}
4345

0 commit comments

Comments
 (0)