|
1 | 1 | <% const { serviceMapping, serviceOptions, writeImportStatement } = options %> |
2 | 2 |
|
| 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 | + |
3 | 10 | export default async function (session, firebase, ctx, inject) { |
4 | 11 |
|
5 | 12 | <% if (!serviceOptions.static) { %> |
@@ -42,32 +49,52 @@ export default async function (session, firebase, ctx, inject) { |
42 | 49 | // Sets up a listener, mutations and action for every onAuthStateChanged by Firebase. |
43 | 50 | const fireAuthStore = { |
44 | 51 | 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 |
48 | 59 | } |
49 | 60 | }, |
50 | 61 | subscribe() { |
51 | | - if (this.off) { |
52 | | - // already subscribed |
53 | | - return |
54 | | - } |
55 | 62 |
|
| 63 | + <% if (hasOnAuthStateChanged && !this.unsubscribeAuthStateListener) { %> |
56 | 64 | return new Promise(resolve => { |
57 | | - this.off = authService.onAuthStateChanged(async authUser => { |
| 65 | + this.unsubscribeAuthStateListener = authService.onAuthStateChanged(async authUser => { |
58 | 66 | const claims = authUser ? (await authUser.getIdTokenResult(true)).claims : null |
59 | 67 |
|
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 }) |
62 | 70 | <% } %> |
63 | 71 |
|
64 | | - <% if (typeof serviceOptions.initialize.onAuthStateChangedAction === 'string') { %> |
| 72 | + <% if (hasOnAuthStateChangedAction) { %> |
65 | 73 | await ctx.store.dispatch(<%= serialize(serviceOptions.initialize.onAuthStateChangedAction) %>, { authUser, claims }) |
66 | 74 | <% } %> |
67 | 75 |
|
68 | 76 | resolve() |
69 | 77 | }) |
70 | 78 | }) |
| 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 | + <% } %> |
71 | 98 | } |
72 | 99 | } |
73 | 100 | inject('fireAuthStore', fireAuthStore) |
|
0 commit comments