@@ -18,27 +18,37 @@ export default defineNuxtPlugin((nuxtApp) => {
1818 VueFireAuth ( nuxtApp . payload . vuefireUser ) ( firebaseApp , nuxtApp . vueApp )
1919 const auth = getAuth ( firebaseApp )
2020 // send a post request to the server when auth state changes to mint a cookie
21- beforeAuthStateChanged ( auth , mintCookie , ( ) => {
22- // rollback the auth state
23- mintCookie ( auth . currentUser )
24- } )
21+ beforeAuthStateChanged (
22+ auth ,
23+ // if this fails, we rollback the auth state
24+ mintCookie ,
25+ ( ) => {
26+ // rollback the auth state
27+ mintCookie ( auth . currentUser )
28+ }
29+ )
2530
2631 // we need both callback to avoid some race conditions
2732 onIdTokenChanged ( auth , mintCookie )
2833} )
2934
35+ // TODO: should this be throttled to avoid multiple calls
3036/**
3137 * Sends a post request to the server to mint a cookie based auth session. The name of the cookie is defined in the
3238 * api.session.ts file.
3339 *
3440 * @param user - the user to mint a cookie for
3541 */
3642async function mintCookie ( user : User | null ) {
37- const jwtToken = await user ?. getIdToken ( )
43+ const jwtToken = await user ?. getIdToken ( /* forceRefresh */ true )
3844 // throws if the server returns an error so that beforeAuthStateChanged can catch it to cancel
39- await $fetch ( '/api/_vuefire/auth' , {
40- method : 'POST' ,
41- // if the token is undefined, the server will delete the cookie
42- body : { token : jwtToken } ,
43- } )
45+ await $fetch (
46+ // '/api/__session-server',
47+ '/api/__session' ,
48+ {
49+ method : 'POST' ,
50+ // if the token is undefined, the server will delete the cookie
51+ body : { token : jwtToken } ,
52+ }
53+ )
4454}
0 commit comments