@@ -2,7 +2,8 @@ import type { H3Event } from 'h3'
22import { eventHandler , getQuery , sendRedirect } from 'h3'
33import { withQuery } from 'ufo'
44import { defu } from 'defu'
5- import { handleMissingConfiguration , handleAccessTokenErrorResponse , getOAuthRedirectURL , requestAccessToken , handleState , handlePkceVerifier } from '../utils'
5+ import type { RequestAccessTokenOptions } from '../utils'
6+ import { handleMissingConfiguration , handleAccessTokenErrorResponse , getOAuthRedirectURL , requestAccessToken , handleState , handlePkceVerifier , handleInvalidState } from '../utils'
67import { useRuntimeConfig , createError } from '#imports'
78import type { OAuthConfig } from '#auth-utils'
89
@@ -12,6 +13,11 @@ export interface OAuthZitadelConfig {
1213 * @default process.env.NUXT_OAUTH_ZITADEL_CLIENT_ID
1314 */
1415 clientId ?: string
16+ /**
17+ * ZITADEL OAuth Client Secret
18+ * @default process.env.NUXT_OAUTH_ZITADEL_CLIENT_SECRET
19+ */
20+ clientSecret ?: string
1521 /**
1622 * ZITADEL OAuth Domain
1723 * @example <your-zitadel-instance>.zitadel.cloud
@@ -90,15 +96,25 @@ export function defineOAuthZitadelEventHandler({ config, onSuccess, onError }: O
9096 handleInvalidState ( event , 'zitadel' , onError )
9197 }
9298
93- const tokens = await requestAccessToken ( tokenURL , {
99+ const request : RequestAccessTokenOptions = {
94100 body : {
95101 grant_type : 'authorization_code' ,
96102 client_id : config . clientId ,
97103 redirect_uri : redirectURL ,
98104 code : query . code ,
99105 code_verifier : verifier . code_verifier ,
100106 } ,
101- } )
107+ }
108+
109+ if ( config . clientSecret ) {
110+ const basicAuthorization = Buffer . from ( `${ config . clientId } :${ config . clientSecret } ` ) . toString ( 'base64' )
111+ request . headers = {
112+ 'Authorization' : `Basic ${ basicAuthorization } ` ,
113+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
114+ }
115+ }
116+
117+ const tokens = await requestAccessToken ( tokenURL , request )
102118
103119 if ( tokens . error ) {
104120 return handleAccessTokenErrorResponse ( event , 'zitadel' , tokens , onError )
0 commit comments