@@ -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 } from './types'
8- import { createError , defineNuxtPlugin , useCookie } from '#app'
7+ import type { Authentication , ModuleOptions } 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,7 +37,6 @@ function createFetchClient(
3537 retry : false ,
3638
3739 async onRequest ( context ) {
38- // todo: move this to interceptors
3940 if ( authentication . mode === 'cookie' ) {
4041 let csrfToken = readCsrfCookie ( authentication . csrfCookie )
4142
@@ -57,11 +58,21 @@ function createFetchClient(
5758 context . options . headers . set ( authentication . csrfHeader , csrfToken . value )
5859 }
5960
60- // todo: move this to interceptors
6161 if ( authentication . mode === 'token' ) {
62- const { tokenStorage } = useAppConfig ( ) . echo . authentication
63- const token = await tokenStorage . get ( )
64- context . options . headers . set ( 'Authorization' , 'Bearer ' + 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 } ` )
6576 }
6677 } ,
6778 }
@@ -70,10 +81,11 @@ function createFetchClient(
7081}
7182
7283function createAuthorizer (
84+ app : NuxtApp ,
7385 authentication : Required < Authentication > ,
7486 logger : ConsolaInstance
7587) {
76- const client = createFetchClient ( authentication , logger )
88+ const client = createFetchClient ( app , authentication , logger )
7789
7890 return ( channel : Channel , _ : Options ) => {
7991 return {
@@ -96,12 +108,13 @@ function createAuthorizer(
96108 }
97109}
98110
99- function prepareEchoOptions ( config : ModuleOptions , logger : ConsolaInstance ) {
111+ function prepareEchoOptions ( app : NuxtApp , config : ModuleOptions , logger : ConsolaInstance ) {
100112 const forceTLS = config . scheme === 'https'
101113 const additionalOptions = config . properties || { }
102114
103115 const authorizer = config . authentication
104116 ? createAuthorizer (
117+ app ,
105118 config . authentication as Required < Authentication > ,
106119 logger
107120 )
@@ -147,25 +160,24 @@ async function setupDefaultTokenStorage(nuxtApp: NuxtApp, logger: ConsolaInstanc
147160 nuxtApp . runWithContext ( ( ) => {
148161 updateAppConfig ( {
149162 echo : {
150- authentication : {
151- tokenStorage : defaultStorage . cookieTokenStorage ,
152- }
163+ tokenStorage : defaultStorage . cookieTokenStorage ,
153164 } ,
154165 } )
155166 } )
156167}
157168
158169export default defineNuxtPlugin ( async ( _nuxtApp ) => {
170+ const nuxtApp = _nuxtApp as NuxtApp
159171 const config = useEchoConfig ( )
160- const appConfig = useAppConfig ( )
172+ const appConfig = useEchoAppConfig ( )
161173 const logger = createEchoLogger ( config . logLevel )
162174
163- if ( config . authentication ?. mode === 'token' && ! appConfig . echo ?. authentication ?. tokenStorage ) {
164- await setupDefaultTokenStorage ( _nuxtApp , logger )
175+ if ( config . authentication ?. mode === 'token' && ! appConfig . tokenStorage ) {
176+ await setupDefaultTokenStorage ( nuxtApp , logger )
165177 }
166178
167179 window . Pusher = Pusher
168- window . Echo = new Echo ( prepareEchoOptions ( config , logger ) )
180+ window . Echo = new Echo ( prepareEchoOptions ( nuxtApp , config , logger ) )
169181
170182 logger . debug ( 'Laravel Echo client initialized' )
171183
0 commit comments