@@ -4,8 +4,9 @@ import type { ChannelAuthorizationData } from 'pusher-js/types/src/core/auth/opt
44import { type ConsolaInstance , createConsola } from 'consola'
55import type { FetchOptions } from 'ofetch'
66import { useEchoConfig } from './composables/useEchoConfig'
7- import type { Authentication , ModuleOptions , SupportedBroadcaster } from './types'
8- import { createError , defineNuxtPlugin , useCookie } from '#app'
7+ import type { Authentication , ModuleOptions , SupportedBroadcaster } from './types/options'
8+ import { useEchoAppConfig } from './composables/useEchoAppConfig'
9+ import { createError , defineNuxtPlugin , useCookie , updateAppConfig , type NuxtApp } from '#app'
910
1011// eslint-disable-next-line @typescript-eslint/no-explicit-any
1112const Pusher = ( PusherPkg as any ) . default || PusherPkg
@@ -26,6 +27,7 @@ function createEchoLogger(logLevel: number) {
2627const readCsrfCookie = ( name : string ) => useCookie ( name , { readonly : true } )
2728
2829function createFetchClient (
30+ app : NuxtApp ,
2931 authentication : Required < Authentication > ,
3032 logger : ConsolaInstance
3133) {
@@ -35,35 +37,55 @@ function createFetchClient(
3537 retry : false ,
3638
3739 async onRequest ( context ) {
38- let csrfToken = readCsrfCookie ( authentication . csrfCookie )
40+ if ( authentication . mode === 'cookie' ) {
41+ let csrfToken = readCsrfCookie ( authentication . csrfCookie )
3942
40- if ( ! csrfToken . value ) {
41- await $fetch ( authentication . csrfEndpoint , {
42- baseURL : authentication . baseUrl ,
43- credentials : 'include' ,
44- retry : false ,
45- } )
43+ if ( ! csrfToken . value ) {
44+ await $fetch ( authentication . csrfEndpoint , {
45+ baseURL : authentication . baseUrl ,
46+ credentials : 'include' ,
47+ retry : false ,
48+ } )
4649
47- csrfToken = readCsrfCookie ( authentication . csrfCookie )
48- }
50+ csrfToken = readCsrfCookie ( authentication . csrfCookie )
51+ }
4952
50- if ( ! csrfToken . value ) {
51- logger . warn ( `${ authentication . csrfCookie } cookie is missing, unable to set ${ authentication . csrfHeader } header` )
52- return
53+ if ( ! csrfToken . value ) {
54+ logger . warn ( `${ authentication . csrfCookie } cookie is missing, unable to set ${ authentication . csrfHeader } header` )
55+ return
56+ }
57+
58+ context . options . headers . set ( authentication . csrfHeader , csrfToken . value )
5359 }
5460
55- context . options . headers . set ( authentication . csrfHeader , csrfToken . value )
61+ if ( authentication . mode === 'token' ) {
62+ const { tokenStorage } = useEchoAppConfig ( )
63+
64+ if ( ! tokenStorage ) {
65+ throw createError ( 'Token storage is not defined' )
66+ }
67+
68+ const token = await tokenStorage . get ( app )
69+
70+ if ( ! token ) {
71+ logger . debug ( 'Authorization token is missing, unable to set header' )
72+ return
73+ }
74+
75+ context . options . headers . set ( 'Authorization' , `Bearer ${ token } ` )
76+ }
5677 } ,
5778 }
5879
5980 return $fetch . create ( fetchOptions )
6081}
6182
6283function createAuthorizer (
84+ app : NuxtApp ,
6385 authentication : Required < Authentication > ,
6486 logger : ConsolaInstance
6587) {
66- const client = createFetchClient ( authentication , logger )
88+ const client = createFetchClient ( app , authentication , logger )
6789
6890 return ( channel : Channel , _ : Options ) => {
6991 return {
@@ -86,12 +108,13 @@ function createAuthorizer(
86108 }
87109}
88110
89- function prepareEchoOptions ( config : ModuleOptions , logger : ConsolaInstance ) {
111+ function prepareEchoOptions ( app : NuxtApp , config : ModuleOptions , logger : ConsolaInstance ) {
90112 const forceTLS = config . scheme === 'https'
91113 const additionalOptions = config . properties || { }
92114
93115 const authorizer = config . authentication
94116 ? createAuthorizer (
117+ app ,
95118 config . authentication as Required < Authentication > ,
96119 logger
97120 )
@@ -127,12 +150,34 @@ function prepareEchoOptions(config: ModuleOptions, logger: ConsolaInstance) {
127150 }
128151}
129152
130- export default defineNuxtPlugin ( ( _nuxtApp ) => {
153+ async function setupDefaultTokenStorage ( nuxtApp : NuxtApp , logger : ConsolaInstance ) {
154+ logger . debug (
155+ 'Token storage is not defined, switch to default cookie storage' ,
156+ )
157+
158+ const defaultStorage = await import ( './storages/cookieTokenStorage' )
159+
160+ nuxtApp . runWithContext ( ( ) => {
161+ updateAppConfig ( {
162+ echo : {
163+ tokenStorage : defaultStorage . cookieTokenStorage ,
164+ } ,
165+ } )
166+ } )
167+ }
168+
169+ export default defineNuxtPlugin ( async ( _nuxtApp ) => {
170+ const nuxtApp = _nuxtApp as NuxtApp
131171 const config = useEchoConfig ( )
172+ const appConfig = useEchoAppConfig ( )
132173 const logger = createEchoLogger ( config . logLevel )
133174
175+ if ( config . authentication ?. mode === 'token' && ! appConfig . tokenStorage ) {
176+ await setupDefaultTokenStorage ( nuxtApp , logger )
177+ }
178+
134179 window . Pusher = Pusher
135- window . Echo = new Echo ( prepareEchoOptions ( config , logger ) )
180+ window . Echo = new Echo ( prepareEchoOptions ( nuxtApp , config , logger ) )
136181
137182 logger . debug ( 'Laravel Echo client initialized' )
138183
0 commit comments